Posted on

Scratch Coding – Animal Noise Reaction Timer

We are going to build a game that will test our reaction time, the player will hear an animal noise and will be given 2 seconds to click on the correct sprite to score a point. Each time the round resets the animals will move to a random location.

Sprites

We start by selected three animal sprites, these will already have the correct sounds attached to them.

We can resize these sprites to fit into the screen nicely.

Starting our Game:

We are going to use the Scratch the Cat sprite to tell the player how to play the game. We are going to setup our game a little bit different to normal games as we want to be able to keep repeating the game until the player fails.

We are going to first create a ‘score’ variable to keep track of the player score.

We then broadcast a message called ‘Start’.

We will have scratch the cat tell the player they have two seconds to click on the animal. Our ‘Receive message’ hat block is now working just like the green flag hat block.

We need to keep track of the animal that is going to be making the sound. To do that we will create another variable called ‘RandomAnimal’. We will set this variable to a random number between 1 and 3 (since we have three animals).

We will then broadcast the random number that got selected.

Animal Sprites:

Our animals will listen for their number to be broadcast. When it comes through, we will play the animal sound.

I’ve made my dog number 1:

The cat number 2:

The horse number 3:

If we test our game now, we will see that a random animal will start making noises.

Clicking on our sprites

We want our player to click on the sprite when the noises are made, on each of the animal sprites we are going to use the ‘when clicked on’ hat block. We need to check to see if the ‘RandomAnimal’ variable contains the same number as our clicked on animal.

We also need to check to see that the player clicked on the animal within two seconds.

My Dog sprite (number 1) looks like:

My Cat sprite (number 2) has:

And my Horse (number 3) has:

Notice how we also broadcast the ‘start’ message after the player has correctly guessed the animal? This will restart the game again.

Make the animals move to a random position.

Our animals can move to a random position when the start message is broadcast:

Put this on each of the animal sprites.

Posted on

Scratch Coding – Angry Birds

Today we are going to build a simple Angry Birds game where we shoot a bird up to hit a nasty pig.

The first thing we need to do is create a new sprite for our nice red bird.

Normally we use the ‘Choose a sprite from the library’ button, but the library doesn’t contain the nice bird picture that we want to use. Instead we are going to use the ‘upload sprite from file’ button.

Once we click on this, we need to find our file. Do the same thing for the green pig. We should end up with two nice sprites:

Now that we have our sprites setup, we can start thinking about how the game should work.

Some ideas of the game might include:

  • Make sure the Angry bird starts in the correct spot
  • Make sure the Green Pig starts in a different spot each time
  • Ask the player the angle that the angry bird should be fire
  • Fire the angry bird off at the angle the player gave
  • Keep score of how many times the bird hit the green pig

The range of angles that we will use, looks like this:

Use this image to help play the game by placing it behind your Angry Bird sprite.

To start our game, we need to make sure that both the bird and the pig start in the correct spots.

On the Angry Bird:

Let’s add the ‘Green Flag Hat Block’ so that we can move the bird when the game starts:

Pick a good starting spot for your bird, I’ve chosen -175, -95.

Ask the player for an angle

Next we want to ask the player which angle we should use to shoot our Angry Bird:

We can then use the answer to turn the bird to the number given by the player.

Making the bird move

We would like our bird to move until it either hits the edge of the screen or if it hits the Pig. To do this we can use the ‘repeat until block’.

The code that we add into this block will run all the time while the bird doesn’t touch either the edge of the screen or the pig. We do however want our bird to move, so let’s add some steps to this code.

Test it out. We should see that the bird will stop either at the pig or on the side of the screen.

On the Pig:

Make the pig move to a random spot

We want to make the angle that the bird needs to fly different each time the game is played. We don’t really want to move the pig closer to the bird, we just want it to be higher or lower on the screen. This is the Y Axis, so when we pick a random spot, we will not change the X axis (across the screen).

Test out the game now. Does it work OK?

Adding a score:

Now that we have our game almost working, we would like to keep score. To add scoring to our game, we need to create a variable that the computer can use to store the score.

Once the variable has been created, we should have some new blocks:

We can now change the code on our Angry Bird sprite to check to see if the bird has touched the pig.

Notice that I’ve also added the block to point in the direction (0) (Up).

Test your game out. How many pigs can your angry bird hit?