Adding a second player

Driving around the circuit on your own is fun but not very challenging. It would be much better if you could invite a friend to compete against. In this step, we will introduce a second player character. Both characters will be visible on the same screen and will be controlled with one keyboard. We're effectively building a simple form of multiplayer.

Engage thrusters

To create a second player, we mainly have to copy the work we've done already. We need a second control sprite and a second kart sprite. We already added a Mario sprite to the game, so let's bring in his brother Luigi as the second player:

  1. Right-click on the control sprite named player1 and choose duplicate.
  2. The copy will be called player2 automatically. If that's not the case, change the name manually.
  3. Go to the player2 Scripts tab.
  4. Change the key pressed controls from a and d to left arrow and right arrow respectively.

The other script works exactly the same as for the player1 sprite.

Copying the sprite can cause some trouble with the variables. So let's make a new speed variable to be used for the second player sprite. We will name this speed2.

We also need another kart costume set. Instead of a guy in red, let's make the second player a guy in green:

Engage thrusters

Find a Luigi sprite sheet. Most of these sheets come in sets for a single game. So if you found a Mario sprite sheet, the Luigi version shouldn't be far off. You can also use the sprite sheet that comes with this project.

  1. First, make a copy of the kart1 sprite to easily get a second kart sprite with all of the scripts attached.
  2. Instead of following the player1 sprite, this sprite has to follow player2 (using go to <player2>).
  3. We also need to change the key pressed controls from a/d to left/right.
  4. The hard work comes with changing the costumes. We need to follow the same procedure we used earlier to create a series of Luigi sprites. Replace all of the Mario costumes with the equivalent Luigi costumes.
  5. Once we're done with the images, we can check the animation with the small test script that loops through all of the costumes.
  6. Make the necessary changes, if needed.

Objective complete – mini debriefing

We now have two player characters that are controlled with different keys. You should grab a friend now and test the game together. Just adding another player to play the game with adds a lot of fun to the activity, even though we haven't scripted a way to clearly win the game yet.

Classified intel

We could leave it at that; but, after driving a few rounds together, you'll notice that both the racers are able to pass through each other. This looks somewhat unrealistic; however, we can fix it easily. We are going to add a collision check. When a player hits his/her competitor, he/she will bounce back, just like when hitting a wall.

  1. Check the script of control sprite player1. Take note of the part where we check for collisions with walls.
  2. Pull the touching color <green>? block out of the slot and set it aside.
  3. Instead, place an or operator block in the slot of the if () then condition.
  4. Replace the touching color <green>? block to the left of the or block.
  5. Place a touching <kart2> block in the slot right of the or operator. Note how we check for the kart and not the control sprite. This is because the kart sprites are a lot bigger than the control sprites. So, they are easier to hit.
    Classified intel
  6. Player one will now not only bounce back when hitting a wall, but also when hitting his/her opponent. Of course, we need to do the same for player two, or the game would be a bit unfair.
  7. Make sure to have control sprite player2 respond in the same way to hitting kart1.

Turning players into obstacles that slow each other down also adds a tactical element to the game. Players can now actively try to run each other off the road.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.35.194