Introduction

In this project you’ll learn how to create a football game in which you have to score as many goals as you can in 30 seconds.

Step 1: Moving the football

Let’s code the ball to move across the bottom of the stage.

Activity Checklist

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

  • Click on your football sprite. Add this code, so that the football moves along the bottom of the screen until the space bar is pressed.

    screenshot
    screenshot
  • Click the green flag to test your project. Your football should bounce along the bottom of the screen until the space bar is pressed.

    screenshot
    screenshot
  • Add this code to your football sprite, so that the football moves towards the goal after the space bar has been pressed.

    screenshot
    screenshot
  • Click the green flag to test your code. This time, press the space bar and your football should move towards the goal.

    screenshot
    screenshot
  • Click the green flag to test your code. What happens if you click the flag a second time? Can you use this block to fix the problem?

        go to x:(-180) y:(-140)

Save your project

Step 2: Was it a goal?

Once the ball has reached the goal, there’s a decision to make. If the ball is touching the goalie then it has been saved, else it’s a goal.

Activity Checklist

  • Add this code to the end of your football sprite code, so that you can check whether the ball is touching the goalie.

    screenshot
    screenshot
  • Play the ‘rattle’ sound if the goalie has saved the ball.

    screenshot
    screenshot
  • You can also broadcast a message to the goalie, so that they can tell you that the ball has been saved.

    Broadcast a ‘save’ message when the ball has been saved.

    screenshot
    screenshot
  • You can now code your goalie to say ‘Save!’ when they receive the message.

    screenshot
    screenshot
  • Test your code by trying to score a goal. If your goalie saves the goal they should say ‘Save!’.

    screenshot
    screenshot

Save your project

Challenge: Goal!

Can you play a sound and code your goalie to say ‘Goal!’ when a goal has been scored?

Remember that a goal has been scored if the ball is not touching the goalie.

screenshot
screenshot

Here are some code blocks you’ll need:

screenshot
screenshot

Save your project

Step 3: Adding a timer

Let’s add a timer, so that the player has to score as many goals as they can in 30 seconds.

Activity Checklist

  • First, you’ll need to click Data, then click ‘Make a Variable’ and create a new variable called timer.

    screenshot
    screenshot
  • You should now see your new variable on the stage. You should also see some new variable blocks that you’ll use to make your timer.

    screenshot
    screenshot
  • Click on your stage, and add this code to set the timer to 30 at the start of the game.

    screenshot
    screenshot
  • Next, you’ll need to add a repeat until block, so that the timer can run until it gets to 0.

    screenshot
    screenshot
  • Reduce your timer by 1 every second until it reaches 0.

    screenshot
    screenshot
  • Once the timer has reached 0, you should play the ‘whistle’ sound and then stop the game.

    screenshot
    screenshot
  • Click the green flag to test your code. Your timer should start at 30, and end at 0.

    screenshot
    screenshot

    You can change your timer to start at 10 if you don’t want to wait for 30 seconds!

  • You only have the chance to score 1 goal! To have more than 1 chance, add a forever block around your football sprite. You can also add a wait block between attempts.

    screenshot
    screenshot

Save your project

Challenge: Adding a score

Can you add a score variable to your game?

  • You should set the score to 0 at the start of the game;
  • You should add 1 to your score every time a goal is scored.

Here’s the code you’ll need:

screenshot
screenshot

Save your project

Step 4: Control the goalie

It’s far too easy to score a goal! Let’s allow a second player to try and save goals.

Activity Checklist

  • Click on your Goalie sprite and add this code to change the goalie’s x position when the left arrow is pressed.

    screenshot
    screenshot
  • Press the left arrow to test your new code. Your goalie should move to the left.

    screenshot
    screenshot
  • Have you noticed that your goalie doesn’t move very smoothly? If you want smoother movement, you can use this code instead of the code you just added.

    screenshot
    screenshot
  • Test your code again, this time by clicking the green flag and then holding the left arrow key. Does your goalie move more smoothly?

Save your project

Challenge: More controls

Can you code your goalie to move to the right when the right arrow key is pressed? You can use either of the 2 ways above.

You could even use this code (in a separate script) to make your goalie jump when the up arrow key is pressed:

repeat (10)
    change y by (10)
end
repeat (10)
    change y by (-10)
end

Save your project

Challenge: Manual control

Instead of the ball moving left and right automatically, can you allow your player to control the ball with the a and d keys?

To do this you’ll need to remove the code for moving the ball left and right.

screenshot
screenshot

You can then add code to move the ball when the keys are pressed. Here are some code blocks to help you:

screenshot
screenshot

Save your project