make a game in scratch

Create a fun game in Scratch featuring a hungry cat and a tasty muffin!

Introduction 

This tutorial shows you how to build a fun game in Scratch which you can then make more complicated by adding extra features.

In the game, you play the part of a tasty muffin being chased by a hungry cat. You move the muffin using your mouse and the game is over when the cat catches the muffin (and eats it)

what you will need

What will you learn about?

  • Directions in Scratch
  • Broadcasting and receiving messages
  • Sensing and acting on events
  • Costumes

extra information for educators

This activity follows on from the Introduction to Scratch activity, so some basic knowledge of Scratch is assumed

making the basic game

This game involves two characters, or sprites in Scratch terms, the cat and the muffin. 

We already have the Cat sprite loaded so we need to add the muffin.

To do this, click on the blue button in the Sprite part of the Scratch screen - this is circled on the screenshot below:

This game involves two characters, or sprites in Scratch terms, the cat and the muffin.

We already have the Cat sprite loaded so we need to add the muffin.

To do this, click on the blue button in the Sprite part of the Scratch screen - this is circled on the screenshot below:

This will open the screen shown below. Click on the “food” button and select the muffin sprite

The sprite section of your Scratch screen should look like this:

If you look at the stage section of Scratch you will see the muffin and the cat are both showing. Make sure the muffin is not placed on top of the cat (or the game will end as soon as it has begun!) by clicking on the muffin and dragging it to the bottom left corner. Your stage should now look similar to the one below.

 

coding the muffin sprite

Now we need to program the muffin so that it follows the mouse pointer, since that is how you control where the muffin moves.

Make sure you select the muffin sprite on the sprite section. You can check this by looking at the sprite section. It should look like the image below with the muffin sprite highlighted and the ‘Muffin’ written in the box labelled Sprite.

Now you need to drag and drop the blocks into the programming part of the screen. 

The first block you need is the “when space key pressed” block from the “event” blocks section. This block makes sure the program starts running when you press the space bar on your keyboard. Drag and drop the “when space key pressed” block into the programming part of the screen. It should look like this:

The second block you need is the “repeat until” block from the “control” blocks section. This block makes sure the muffin keeps following your mouse until the game has ended. Drag this block to the programming section and place it below the “when space key pressed” block as shown below.

Now you need to make sure the muffin follows your mouse as you move it round the stage. You do this by adding a “go to” block from the “motion” blocks. Place this block inside the “repeat until” loop to make sure the muffin always follows the mouse. Click on the darker blue button on the “go to” block and select “mouse-pointer” from the drop-down menu.

 

 

 

 

 

 

 

 

 

Finally you need to tell the program to run until the muffin touches the cat. You do this by adding a “touching” block from the “sensing” blocks section. Click on the darker blue button on the “touching” block to show the list of options. The cat sprite will be called “Sprite1” by default, unless you have given it a different name. Select “Sprite1” as shown below.

The finished code for the muffin Sprite should look like the one below:

 

Coding the cat sprite

Now we need to program the hungry cat to chase the muffin!

First make sure you are coding the Cat Sprite and not the muffin sprite. The program part of the screen should be empty and the Cat sprite (called Sprite1) should be selected, as shown below:

Like the muffin sprite, we need a “when space key pressed” block as the first block. Drag and drop that into the programming section. This makes sure both the sprites start the game at the same time.

Now, when we start the game, we want the cat to always be in the same place so we don’t accidentally move the muffin on top of it before the game starts! To make sure the cat is always in the top, right corner use a “go to x: y:” block from the “motion” blocks section and set x to 160 and y to 100. The program should now look like the picture below:

We want the game to run until the Cat catches the muffin, so we need a “repeat until” block like previously, except this time the “touching” block should have “muffin” selected. Add these blocks below the program we have so far. After doing this, the program should look like the one below:

The next part of the program controls the movements of the cat, making it chase the muffin! These blocks go inside the “repeat until” block, since we want the cat to keep chasing the muffin until it catches it!

Firstly, we want the cat to move in the direction of the muffin. To do this we use a “point towards” block from the “motion” blocks, and select “Muffin” from the drop down menu. This means the cat is facing in the direction of the muffin. Then, we want the cat to move forward, towards the muffin. To do this we add a “move 10 steps”  block from the “motion” blocks section. The code should now look like this:

Finally, for the finishing touch we are going to make the cat look like it is running! To do this we need to change it’s ‘costume’. This makes it’s legs move as though it is running forward. Add the “next costume” block from the “looks” section  below the “move 10 steps” block. Your final code should look like the picture below:

Playing the Game!

You are now ready to play the game! Move the muffin sprite as far away from the Cat sprite as possible to begin… you’ll have to be quick though - this cat is seriously hungry!

some fun extras to add!

Wouldn’t it be cool if the cat took a bite out of the muffin when it caught it?

To make this happen, add a “next costume” block to the end of the program for the muffin sprite. The muffin sprite program should now look like the image below (remember to make sure you have selected the muffin sprite in the Scratch window!)

How about adding some sound effects? Let’s make the cat meow with delight when it catches the muffin, and say “yummy!”. To do this use the “start sound Meow” block from the “sound” section and add it to the end of the program. To make the cat say “yummy!” add the “say … for … seconds” block from the “looks” section, and type “yummy!” in the space where “Hello” is written.

The program should now look like the image below:

now let's mix things up a bit!

So far we’ve controlled the muffin sprite and tried to help it escape from the hungry cat… but now let’s control the cat sprite using the keyboard rather than the mouse. In this game the muffin will appear at a random position and you must use the arrow keys to move the cat to the position the muffin is at. Once you reach the muffin (and take a bite out of it!) a new muffin will appear at a new location. Eat as many muffins as you like to satisfy the cat!

We'll start this game from scratch, so load another Scratch window on your computer.

coding the cat: we need some direction!

We’ll control the Cat using the arrow keys on the keyboard. To make the controller we will need 4 blocks of code - one for each of the keys.

Before we do this though, we need to understand how directions work in Scratch. The numbers used for the four different directions (Up, Down, Left, Right) are shown in the image below:

These values might seem a bit random, but they are based on the number of degrees the cat turns through. If you ever forget the values for each direction, you can click on the “direction” value box in the sprite section of the screen and a helpful compass will pop up as shown below:

programming the controller

Ok, so now we understand how directions work, let’s program the controller!

We will have 4 blocks of code, one for each of the arrow keys. Let’s start with the arrow pointing right.

Firstly, make sure you are programming the cat sprite. Now from the “events” block section, select the “when space key pressed” block and from the drop down menu select the “right arrow” key as shown.

Now we need to tell the cat which direction to move in and how far to move. The direction to move the cat to the right is 90, so select the “Point in direction…” block from the motion blocks section and enter “90” into the box. Then, also from the motion blocks section, select the “move … steps” block and add it to your code. We find 10 steps in a good value to get the cat to move, but you can experiment if you like! 

Finally we want the cat to look like it is running. Do you remember how to do this? We need to add a “next costume” block from the “looks” section of blocks.

You should now have the following block of code on your screen:

Now we need to program the other three arrow keys, so that the cat moves in the correct direction when each of them is pressed. Have a go at doing this yourself, then check if your code matches the code below. Be sure to check the values in the “point in direction” block carefully!

 

 

starting and ending the game

We want the game to start when we start moving the cat, and we want it to end when the cat catches the muffin.

Still programming the cat sprite, we need to add a new event block. Since we don’t know which direction we will want the cat to move in first, we’ll start the game when any key is pressed using the code shown below:

Now, we want the program to constantly be checking if the cat has caught the muffin, so we will use a “if … then …” loop from the “control” section of blocks.

Now make sure you have added the muffin sprite to your program, like we did previously. Your sprite section of the screen should contain both the muffin and cat sprite as shown:

Once you have done that, select the “touching” block from the “sensing” blocks and from the drop down menu select “muffin”. If you don’t see the option of “muffin” in the drop down menu, it means you have not yet added a muffin sprite to your program. Your code block should look like the one shown below:

 

 

message received!

Now, when we catch the muffin, we want both the cat and muffin sprite to stop moving at the same time and for the game to end. To make this happen we need to broadcast a message from the cat sprite to the muffin sprite when the cat catches the muffin. When the muffin sprite receives this message, it will know it is game over and it should stop moving.

To do this, select a “broadcast …” block from the “control” set of blocks, and place it inside the “if … then” loop (since we want the program to be constantly checking whether the game has ended). You can leave the message being sent as “message1” or you can change it to a relevant name like “Game Over”. Since there is only one message needed in this game it doesn’t matter too much, but when you are coding more complex programs it is always a good idea to give your variables relevant names! To change the name of the message, select the “New Message” option from the drop down menu, and type in the name of your message, as shown below:

Your code should look like this:

Finally, why celebrate catching the muffin by saying meow and “yummy”? To do this, add a “play sound meow” block from the sound blocks, and a “say … for … seconds” block from the looks blocks. Your code block should now look like this:

And the code for the cat sprite should look like this:

 

coding the muffin

That’s us all done with the cat sprite! Now we need to program the muffin sprite so that the game ends when the cat touches it.

Like for the cat sprite, we want the game to start when we press any key, so that is the first block to add to the muffin sprites program. We also want the game to end when the cat (or sprite1) touches the muffin, so we will use a “wait until touching sprite1” block from the control blocks to do this. Finally, wouldn’t it be cool if the cat took a bite out of the muffin after it caught it? After all, what’s the point of catching a muffin if you can’t eat it!? To do this, use the “change costume to muffin-b” block from the “looks” section. The code should now look like that shown below:

Now we need a second block of code to tell the muffin when the game ends.

Remember how we got the cat to broadcast the message when it touched the muffin? Well, now we need to make the muffin listen for the message so that it knows the game is over when it receives it. To do this we use a “when I receive …” block from the “control” block section. Select the name of the message you sent from the drop down menu in the “when I receive…” block. We called the message “Game Over” but the default message is “message1”. The block should look like the one below:

Now, when the game is over, we need to set things up for the next round, with a new muffin at a new location!

Let’s wait a second for the cat to catch it’s breath by adding a “wait .. seconds” block. Now we need to switch to a new muffin without a bite taken out of it, and move it to a random location. We can do this using the “switch costume to muffin-a” block from the looks section and a “go to random position” block from the “motion” section.

The block of code should look like this:

And the entire code for the muffin sprite should look like the image below:

 

 

playing the game

You are now ready to play the game! Simply move the cat sprite towards the muffin using the arrow keys. When you catch the muffin another new one will appear at a random location ready for the cat to track down. How many muffins does your cat need to eat to fill it up!?