# Multiple Players

### Introduction

This tutorial will cover how to add multiple players to the GameScreen. Fortunately we have programmed almost everything to support multiple MainShips from the beginning, so this tutorial will not require too much work.

### Detecting number of players

Most games include two requirements for a new player to join the game:

1. A controller must be connected for the player
2. The player must perform some action (such as pressing a button in a join screen)

To keep the tutorial shorter than it would otherwise be, we will assume that if a controller is connected, then the player intends to play. If you are developing a game which you intend to distribute to others, you should consider giving the player more freedom such as being able to drop out of a game and to require explicit action to join.

### Adding additional players

To add additional players we will detect how many controllers are connected, and create additional players as necessary. To do this:

1. Open GameScreen.cs in Visual Studio
2. Find the **CustomInitialize** function
3. Modify this function as shown in the following code snippet:

```csharp
void CustomInitialize()
{
 this.TextInstance.Text = "0";
 AddAdditionalShips();
}
```

Next we'll need to implement AddAdditionalShips. Add the following function to GameScreen.cs:

```csharp
private void AddAdditionalShips()
{
 // We assume that the first player (player at index 0) 
 // is already part of the game. Let's start with index 1:
 for (int i = 1; i < InputManager.Xbox360GamePads.Length; i++)
 {

  if (gamepad.IsConnected)
  {
   var player = new Player();
   player.SetSpriteTexture(i);
   player.TurningInput = gamepad.LeftStick.Horizontal;
   player.ShootingInput = gamepad.GetButton(Xbox360GamePad.Button.A);
   PlayerList.Add(player);
  }
 }

 const float spacingBetweenPlayers = 60;
 const float startingX = -90;
 // Reposition all players
 for (int i = 0; i < PlayerList.Count; i++)
 {
  PlayerList[i].X = -startingX + i * spacingBetweenPlayers;
 }
}
```

Now that we're calling SetSpriteTexture, we'll need to make a public function for it

1. Open Player.cs in Visual Studio
2. Add the following to Player.cs:

```csharp
public void SetSpriteTexture(int index)
{
 switch (index)
 {
  case 0:
   this.SpriteInstance.Texture = MainShip1;
   break;
  case 1:
   this.SpriteInstance.Texture = MainShip2;
   break;
  case 2:
   this.SpriteInstance.Texture = MainShip3;
   break;
  case 3:
   this.SpriteInstance.Texture = MainShip4;
   break;
 }
}
```

![RockBlasterDifferentShipTextures.png](/files/EsReUCKd8KQntjkHvzB5)

### Conclusion

Now each ship will use a different texture so that players will be able to tell each other apart. The next tutorial will improve on the way player death is handled by adding the concept of player health and a health bar.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.flatredball.com/flatredball/tutorials/rock-blaster/tutorials-multiple-players.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
