Finishing the game

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.

Engage thrusters

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:

  1. Click on the Paintbrush icon to create a new sprite.
  2. Select the Line tool and draw a horizontal line. Make sure that the line is wide enough to stretch across the road and the sandy areas next to it.
  3. Drag the line to the position of the finish line on the backdrop image.
  4. Name the new sprite finish.
    Engage thrusters
  5. Go to the Scripts tab of the finish sprite.
  6. Add a when <green flag> clicked block.
  7. Attach a set <ghost> effect to () block to make the line invisible; fill in the value 100.
    Engage thrusters
  8. Then, we go to the Scripts tab of player1 to add to the script there.
  9. We create two more variables named lap and touchFinish. Both variables should apply only to this sprite (using only to this sprite).
  10. Click on the check box for the lap variable to make it visible on stage. Drag the display to the top-right corner of the stage.
    Engage thrusters

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. Get an if () then () else () block to form the basic structure of the script. This block will check when a kart is or isn't touching the finish line and set the appropriate variable correspondingly.
  2. Inside the if slot, place an if () then block (without an "else"). With this block, we will count the number of laps completed.
  3. Inside the if () then block, add change <lap> by (); fill in the value 1.
  4. Also add set <touchFinish> to () with the value 1.
  5. Inside the else slot, place a set <touchFinish> to () block with the value 0.

Let's write the conditions for the if statement next:

  1. The slot in the if () then () else () block should be filled with a touching <finish>? condition check.
  2. In the if () then block, we place a () = () operator.
  3. On the left side of the equation, we place the touchFinish variable.
  4. To the right, we fill in 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.

  1. Place yet another if () then block inside the second if statement.
  2. Put a () = () operator in the condition slot.
  3. Put the lap variable to the left of the equation.
  4. Insert 3 to the right of the equation.
  5. When the player has finished three laps, add say timer to show how long it took the player to finish the race.
  6. We also need this stack of instructions for player2. So, let's first drag it over the player2 sprite to copy the script there.
  7. Then drag the stack to the when <green flag> clicked script. Place it just underneath the other if () then collision checks.
  8. Click on the player2 sprite to view its Scripts tab and place the copied script segment in the same place as we did for player1.
  9. Also make the lap variable for player2 visible by clicking on the check box.
  10. Drag the display to the top-right corner of the stage and place it underneath the player1 lap display.

The script should look like the following screenshot:

Engage thrusters

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".

Engage thrusters

Objective complete – mini debriefing

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!

Classified intel

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.

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

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