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.
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:
player1
and choose duplicate.player2
automatically. If that's not the case, change the name manually.player2
Scripts tab.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:
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.
kart1
sprite to easily get a second kart sprite with all of the scripts attached.player1
sprite, this sprite has to follow player2
(using go to <player2>).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.
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.
player1
. Take note of the part where we check for collisions with walls.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.
3.145.35.194