08. Adding multiple players

Introduction

Beefball is intended to be a competitive multiplayer game. So far we only have one PlayerBall instance, so let’s add some more PlayerBall instances.

Instances vs. Lists

Instead of simply adding a second PlayerBall Instance directly in Glue, let’s make a List of PlayerBalls. This allows us to have any number of PlayerBall instances, and allows us to generalize our code.

We’ll create a list for PlayerBalls, similar to the Walls list we created in an earlier tutorial.

Creating a List

To add a list to the GameScreen:

  1. Expand GameScreen in Glue
  2. Right-click on GameScreen’s Objects item
  3. Select “Add Object”
  4. Select “FlatRedBall or Custom Type”
  5. Select “PositionedObjectList<T>” as the type
  6. Select “Entities\PlayerBall”
  7. Name the object “PlayerBallList” and click OK

PlayerBallListGlue.png

Adding to a List

First, we’ll add our existing PlayerBallInstance to the newly-created List. To do this:

  1. Click+hold on the PlayerBallInstance object in the GameScreen’s Objects
  2. Drag to the PlayerBallList and release
    PlayerBallDragList.png

You should now see the PlayerBallInstance embedded under the PlayerBallList
EmbeddedInList.png

Creating a new instance in a list is also very easy. To do this:

  1. Right-click on the PlayerBallList
  2. Select “Add Object”
  3. Enter “PlayerBallInstance2” as the name and click OK
  4. Change the newly-created PlayerBallInstance2’s X value to -180

You should now see two PlayerBall instances under the PlayerBallList. GlueView will also display both PlayerBall instances:
TwoPlayerBallInstances.png.

Modifying collision code

If you run your game you’ll notice that the second PlayerBall instance doesn’t collide with anything in the Screen. This can be easily fixed by modifying the collision code.

We’ll create a loop for all PlayerBalls in the PlayerBallList and collide against the walls and PuckInstance. Your code should look like this:

Player vs. Player collision

PlayerBall instances should also collide against each other. To add this, modify the body of your CollisionActivity code so it looks like this:

If you run you game now, the two PlayerBall instances will collide against each other.

Adding input for Player 2

We’ll assign input on PlayerBall2Instance with code similar to the input-assigning code for PlayerBallInstance. To do this, modify the GameScreen’s AssignInput as follows:

Now each PlayerBall uses a different Xbox360GamePad or set of keys.

Conclusion

Now that we have multiple PlayerBall instances, we have a game that is playable, but it’s missing scoring and game rules. The next tutorial will add in the ability to score goals.

<- Creating the Puck EntityScoring and Restarting Rounds ->