Introduction

You are going to learn how to create your own sprint game, in which you have to use the left and right arrow keys to get to the finish line as quickly as you can.

Step 1: On your marks…

Let’s start by creating a race countdown.

Activity Checklist

  • Open the ‘Sprint’ Scratch project. Your club leader will give you a copy of this project, or you can open it online at jumpto.cc/sprint-resources.

  • To start with, let’s put the finish line on the horizon:

    screenshot
    screenshot
  • If you click the flag to test your code, you’ll see your finish line in the distance.

    screenshot
    screenshot
  • Next, use say blocks to create a countdown, and then broadcast a start message.

    screenshot
    screenshot
  • You can also add sounds to your countdown.

    screenshot
    screenshot

Save your project

Step 2: Going the distance

Let’s move the finish line when the arrow keys are pressed.

Activity Checklist

  • You want to allow the player to press the arrow keys until they have run 100 meters. To do this, create a new variable called distance.

    screenshot
    screenshot
  • You should see your new variable on the stage. Right-click the variable and select ‘large readout’.

    screenshot
    screenshot
  • Drag your variable to the bottom of the stage.

    screenshot
    screenshot
  • Set the distance to 0 when the flag is clicked.

    screenshot
    screenshot
  • Once your race starts, your player should sprint until they have run 100 meters.

    screenshot
    screenshot
  • Add code so that your finish line gets a little bigger after the player presses the left arrow key. The distance should also increase.

    screenshot
    screenshot
  • Click the green flag to test your project. You should see that the finish line gets bigger when the left arrow is pressed, but doesn’t move along the track.

    screenshot
    screenshot
  • To fix this, you can add code to move the finish line down slightly each time a key is pressed.

    screenshot
    screenshot
  • Test your project again and you shoud see the finish line move down the stage towards you.

    screenshot
    screenshot
  • You should then do the same for the right arrow key.

    screenshot
    screenshot
  • If you click to see the finish line’s costumes, you should see that there are 2.

    screenshot
    screenshot
  • You can switch to the ‘broken’ costume (and end the game) at the end of the race. Remember to switch to the ‘normal’ costume at the start of the race!

    screenshot
    screenshot
  • If you want to play a sound at the end, you’ll have change your stop all block to stop other scripts in sprite.

    screenshot
    screenshot

    This means that the timer you’ll create will stop counting, but the sound will still play.

    screenshot
    screenshot
  • Have you noticed that you can cheat your game by just holding down the left and right arrow keys?

    To fix this, you need to make sure that each key is pressed and then released before moving the finish line.

    Here’s the code you’ll need to add:

    screenshot
    screenshot

    You’ll need to do the same for the right arrow key.

Save your project

Step 3: Who is the fastest?

Let’s add a timer to your game, to see who can sprint the fastest.

Activity Checklist

  • Create a new time variable, and move it to the center of your stage.

    screenshot
    screenshot
  • Set the time to 0 at the start of your game.

    screenshot
    screenshot
  • Add this code to make your timer count up when the game starts.

    screenshot
    screenshot
  • Test your project by clicking the green flag. You should see your timer counts up until you’ve sprinted 100 meters.

    screenshot
    screenshot

Save your project

Step 4: Adding scenery

Let’s code a tree to move as the player sprints.

Activity Checklist

  • First, position the tree and make it small when the flag is clicked.

    screenshot
    screenshot
  • Once the race starts, the tree should move until the player has sprinted 100 meters.

    screenshot
    screenshot
  • Once the left key has been pressed (and released), the tree should get bigger and move - just like the finish line.

    screenshot
    screenshot
  • If you test your tree, you’ll see that it moves downwards, onto the track.

    screenshot
    screenshot
  • To fix this, add code to make your tree move away from the track slightly.

    screenshot
    screenshot
  • You should also do the same for the right arrow key. Here’s how your Tree’s code should look:

    screenshot
    screenshot

Save your project

Challenge: Add a spectator

Can you add a spectator to your race?

screenshot
screenshot

Remember that the code you’ll need is very similar to the code you’ve already added to your finish line and your tree.

Here are some useful code blocks to help you:

screenshot
screenshot

If you prefer, you can add another tree instead, or anything else you like!

Save your project