Scratch Game
Normal
60 mins
405 points
What you need:
  • Computer/laptop

Penalty Shootout

Create a penalty shootout game, can you score on the keeper?

This message will be visible to everyone. Send

Coders

- (- logged in)
Current Step
-
Previous Step
-
coders -

    This code along has finished! ⌚

    You can watch it back by playing the video!

    The code along is open for everyone to join in but if you would like to earn the points and badges please join our club!

    1 - Create a new Scratch project

    Go to the Scratch website and create a new Scratch project. You can delete the cat sprite.

    2 - Download and add the goal backdrop

    Included with this step is a picture of a soccer goal, download this picture onto your computer and then upload it as a new backdrop in your project.


    If for some reason you can't save the background image to your computer and upload it, you can just use this starter project which already has the backdrop added to it. https://scratch.mit.edu/projects/573459765/editor/

    3 - Add the Soccer Ball sprite

    Add the Soccer Ball sprite from the sprite library and give it the following code to set it up for the start of the game.

    when green flag clicked set size to (100) % go to x (0) y (-130) // the bottom middle of the stage area



    4 - Choose your shot

    To take a penalty shot you will click with your mouse the spot in the goal where you want to ball to go. This means that you will be clicking the backdrop so add the following code to the backdrop to broadcast a message.

    when stage clicked broadcast [shoot v]


    5 - Move the ball

    Now let's make the ball move to the spot that you clicked.

    When the ball receives the 'shoot' message. we will make the ball move towards where your mouse pointer is and also shrink the ball so that it looks like it's moving away from you.

    Add the following code to the Soccer Ball sprite.

    when I receive [shoot v] point towards [mouse-pointer v] repeat (30) change size by (-2) move (8) steps end


    6 - Add the Casey sprite

    Now let's add a goalkeeper! Add the Casey sprite from the sprite library and give it the following code to set it up for the start of the game.

    when green flag clicked set size to (70) % switch costume to [casey-a v] go to x (0) y (30) // in the middle of the goal point in direction (90) set rotation style [left-right v] // this keeps the sprite the right way up



    7 - Make the goalkeeper dive

    When the choose a spot and send the 'shoot' message we will make the goalkeeper dive to a random spot and try and save your shot.

    Add the following code to the Casey sprite.

    when I receive [shoot v] switch costume to [casey-d v] point in direction (pick random (-180) to (180)) move (100) steps


    8 - Detect if the keeper makes a save

    At the moment the keeper doesn't stop the ball when he makes a save so let's add the code to detect that.

    Add the following code to the Soccer Ball sprite, underneath the move 8 steps block.

    when I receive [shoot v] point towards [mouse-pointer v] repeat (30) change size by (-2) move (8) steps // add new code under here if < touching [Casey v] ? > then say [It's saved!!!] for (2) seconds stop [this script v] // this stops the ball from moving end end



    9 - Detect if it hits the post

    Now let's doing something similar for detecting if the ball hits the post.

    Add the following code to the Soccer Ball sprite, underneath the if then block.

    when I receive [shoot v] point towards [mouse-pointer v] repeat (30) change size by (-2) move (8) steps if < touching [Casey v] ? > then say [It's saved!!!] for (2) seconds stop [this script v] end if < touching color [#ffffff] ? > then // get the colour of the post say [It hits the post!!!] for (2) seconds stop [this script v] // this stops the ball from moving end end




    10 - Score a goal

    Finally we just need to add the code for scoring a goal. We do this by adding a say Hello! for 2 secs block after the repeat block, because the code will reach here if the keeper didn't save it and it didn't hit the post so therefore it must be a goal!

    Add the following new code to the Soccer Ball sprite, underneath the repeat block.

    when I receive [shoot v] point towards [mouse-pointer v] repeat (30) change size by (-2) move (8) steps if < touching [Casey v] ? > then say [It's saved!!!] for (2) seconds stop [this script v] end if < touching color [#ffffff] ? > then say [It hits the post!!!] for (2) seconds stop [this script v] end end say [Goooaaallll !!!] for (2) seconds // add this block


    Join our club 😃

    Create your profile, earn points & badges and learn how to code!

    Copyright Notice
    This lesson is copyright of Coding Ireland. Unauthorised use, copying or distribution is not allowed.

    Scratch is developed by the Lifelong Kindergarten Group at the MIT Media Lab. It is available for free at https://scratch.mit.edu
    🍪 Our website uses cookies to make your browsing experience better. By using our website you agree to our use of cookies. Learn more