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.
Most games include two requirements for a new player to join the game:
A controller must be connected for the player
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.
To add additional players we will detect how many controllers are connected, and create additional players as necessary. To do this:
Open GameScreen.cs in Visual Studio
Find the CustomInitialize function
Modify this function as shown in the following code snippet:
Next we'll need to implement AddAdditionalShips. Add the following function to GameScreen.cs:
Now that we're calling SetSpriteTexture, we'll need to make a public function for it
Open Player.cs in Visual Studio
Add the following to Player.cs:
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.