Angles

ScratchHard

In this fun project you create a game where you shoot a canon at an egg, but the twist is that you must bounce your shoot a certain number of times. So you need to get your angles right!

1

Remix our starter Scratch project

We've created a starter project that has the sprites already added. Open this starter project by clicking on the following link and then click 'Remix' to create a copy of this project in your Scratch account (you must be signed in).

https://scratch.mit.edu/projects/305972087/editor

2

Aim the canon

Click on the canon sprite so that we can add some code to it. We're going to program the canon to point wherever our mouse is. Add the following code to the canon:

when green flag clicked go to x (0) y (0) // place it in the center forever point towards (mouse-pointer v) // rotate to follow your mouse end


3

Program the egg

Click on the egg sprite to give it some code. We're going to create two new messages called 'new position' and 'hit'. We'll then give the egg instructions on what to do when it receives either of these messages.

Add the following code to the egg:

when I receive [new position v] // create this new message called 'new position' switch costume to (egg-a v) // normal costume go to (random position v) when I receive [hit v] // create this new message called 'hit' switch costume to (egg-b v) // cracked costume


4

Program the Hit sprite

The Hit sprite is some text that reads "Hit!!!", we're going to program this to display when you hit the egg and otherwise hide.

Add the following code to the Hit sprite:

when green flag clicked hide // hide it at the start when I receive [setup v] // create this new message called 'setup' hide when I receive [hit v] start sound (Tada v) // add this sound from the sound library show wait (1) seconds // only show for a second broadcast (setup v) broadcast (new position v)


5

Program the Fail sprite

The Fail sprite is some text that reads "Fail!!!", we're going to program this in a similar way to the Hit sprite.

Add the following code to the Fail sprite:

when green flag clicked hide // hide it at the start when I receive [setup v] hide when I receive [fail v] start sound (Oops v) // add this sound from the sound library show wait (1) seconds // only show for a second broadcast (setup v)


6

Fire on click!

Next we need to add some code to the stage, click on the Stage in the Backdrops box on the right hand side (to the right of the sprite list).

Add the following code to the stage, this will detect when you click and will send the 'fire' message.

when stage clicked broadcast (fire v)// create this new message called 'fire'



7

Create the variables

Part of the game will be showing the player how many bounces he/she must do before hitting the egg. Create these two variables:

bounces - this variable will tell the player how many bounces (between 1 and 3) they need to do before hitting the egg.

mybounces - this variable will record how many bounces the player actually takes with their shot.

8

Set the amount bounces needed

Now we will set the amount of bounces you need to take before hitting the egg. Add the following code to the Ball sprite:

when I receive [new position v] set [bounces v] to (pick random (1) to (3) broadcast (setup v)

9

Setup the ball sprite

Add the following code to the ball sprite to set it up before you take a shot.

when green flag clicked broadcast (new position v) // move to a new position at the start of the game when I receive [setup v] erase all // clear the trace from the last shot set pen color to (#ffhhff) // choose what colour the trace of the shot will be set [my bounces v] to (0) // reset this to 0 hide // hide the ball

10

Fire the ball

Now we are going to program the ball to fire when it receives the 'fire' message. We are going to draw a line tracing the ball's path so we need to add the 'Pen' extension first before adding the code blocks.

Once you've added the Pen extension, add the following code to the Ball sprite to fire a shot.

when I receive [fire v] pen up go to (canon v) // start the ball at the canon point towards (mouse-pointer v) // point the ball the same as the canon show repeat until < (mybounces) > (bounces) > move (10) steps // keep moving until you've taken too many bounces pen down // trace a line of the path of the ball end broadcast (fail v) // in a later step we'll check if you hit the egg and broadcast 'hit'


11

Make the ball bounce

If you test your code and fire the canon you'll notice that it doesn't bounce off the edge of the stage area. Let's add some code to make it bounce.

Add the following code to the Ball:

when I receive [fire v] pen up go to (canon v) point towards (mouse-pointer v) show repeat until < (mybounces) > (bounces) > move (10) steps pen down if < touching (edge v) ? > then // add this new code start sound (Boing v) //Add this from the sound library if on edge, bounce // make it bounce change [mybounces v] by (1) //count the bounce end end broadcast (fail v)


12

Detect a hit

Add the following code inside the 'repeat until' block that you already added to the ball sprite. This code will detect if you hit the Egg and if you did it in the right amount of bounces.

when I receive [fire v] pen up go to (canon v) point towards (mouse-pointer v) show repeat until < (mybounces) > (bounces) > move (10) steps pen down if < touching (edge v) ? > then start sound (Boing v) if on edge, bounce change [mybounces v] by (1) end if < < (mybounces) = (bounces) > and < touching (Egg v) > > then // add this new code broadcast (hit v) stop [this script v] // this stops the ball from continuing end end broadcast (fail v)


13

Play the game!

Click the green flag and play the game! If any part of the game isn't working correctly try and identify the area of code that could be the problem and read back through the steps if necessary to fix it.

Have you any ideas on how to improve the game?

End of lesson
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