Aim of the game is to hit the cat with a hammer. Just like the classic Whack-A-Mole game.
The cat will pop up from a random hole in the ground and your job as the game player is to hit the cat when it is fully out of the hole.
We will try to trick the player by having some of our cats only poke their heads up part of the way.
We will be learning how to use the following blocks with this code:
- Random – We will be using random numbers to determine which cat should come out of the hole.
- Broadcast messages – We will continue to use the broadcast messages to show the cat.
The variables we will need to use:
CatToShow is used to store a random number that will determine which cat should be shown.
PretendCatToShow is used to store a random number that will determine which cat will push his head half way to trick the player.
Score will store the number of times the cat has been hit with the hammer.
Step 1: Animate the hammer when the mouse is clicked
We want to move the hammer around where ever the mouse is, to do this we must use always be moving the sprites position to where the mouse cursor is.
When the hammer sprite is clicked on, we want the hammer to rotate in one direction, wait a small amount of time and then rotate back to the starting position. This will look like we have tried to hit something.
Step 2: Setup the background to send messages to the Cats
We should firstly reset our score to zero every time the program has been started.
Setup a broadcast message called ‘Play’. On the same sprite, use the hat block that will respond to the Play message.
The backdrop Play message will pick a random number that will tell us which cat has been chosen to appear, store this in our CatToShow variable. We will use an if block to compare the CatToShow variable with a number that we assign to each cat. We then broadcast a message inside the if block to the cat.
For example, cat 1 will receive the message ‘Cat1’, cat 2 will receive the message ‘Cat 2’.
Step 3: Cat Script
Our cat has 3 costumes:
- A blank hole
- A cat which is half way out of the hole
- A cat that is fully out of the hole
We need to respond to our Cat message and change costumes. The first costume should switch to costume 1 (blank hole), then wait a small amount of time and switch to costume 2, wait a small amount of time again and show costume 3 (the cat is out of the hole). Wait about half a second and show costume 1 again.
Once the cat is back in the hole (we just showed costume 1), then we can broadcast the ‘Play’ message again which will be picked up by the backdrop and the game will restart again.
Our cat also needs to be checking to see if they have been hit by the hammer.
This check should include:
- Check that the costume number is equal to 1
- Make sure that the mouse button is down
- We might need to include a colour on the hammer to be more precise
The completed code:
The code for the cat is:
The code for the hammer is:
Code for the backdrop: