Two players can now race each other around the circuit, which is already a lot of fun. But it would be even better if the game has a clear end state and a notification about who won the game.
We will add some more scripts to the control sprites player1
and player2
. We will first build one complete instruction set, which we can then easily copy to the second sprite.
To determine whether a player has completed a round, we need to add a finish line. This can be a simple additional sprite laid across the track:
finish
.finish
sprite.100
.player1
to add to the script there.lap
and touchFinish
. Both variables should apply only to this sprite (using only to this sprite).lap
variable to make it visible on stage. Drag the display to the top-right corner of the stage.We will check for collisions with the finish line and increase the lap variable accordingly to count the number of laps the players have driven:
1
.1
.0
.Let's write the conditions for the if statement next:
touchFinish
variable.0
.Crossing the finish line usually takes the kart sprite longer than one loop through the script. With the previous check, we prevent the lap counter to increase multiple times while the kart is crossing the line.
We also need to do something special when the player has completed three laps to indicate that they finished the race.
lap
variable to the left of the equation.3
to the right of the equation.player2
. So, let's first drag it over the player2
sprite to copy the script there.player2
sprite to view its Scripts tab and place the copied script segment in the same place as we did for player1
.lap
variable for player2
visible by clicking on the check box.player1
lap display.The script should look like the following screenshot:
Lastly, we need to set the lap variables to their starting values when the game starts.
Place a set <lap> to () block at the start of the when <green flag> clicked script; fill in the value -1
. Do this for both control sprites.
We use -1
and not 0
because the karts will drive over the finish line as soon as they start racing, activating the script we just wrote and increasing the lap number. So we can explain -1
as "the race has not started yet" and 0
as "the race has begun, but no lap has been completed yet".
That completes our kart racing game. Two players can now compete against each other to race three laps in record time. Gather some friends and have a racing competition. Playing against the computer is good entertainment, but playing with friends and family is even more enjoyable. You can also show off your hard work!
You may have noticed that we keep switching the value of the touchFinish
variable with an if () else statement. You can compare it with flipping a light switch. This is a special kind of variable called Boolean
. It only has two meaningful numbers. It can be either 0
(off) or 1
(on). Any other numbers will be counted as 1
.
This kind of variable is often used in games to check whether something is active. We use it here to check if we are still touching the finish line after first hitting it. Without this check, the lap variable would be increased with each script loop, which as you may remember, is very fast. We only want to have the collision registered once, after which the kart has to completely pass over the line before a collision may be checked again.
18.220.245.140