Scratch Game
Beginner
60 mins
410 points
What you need:

Dino food game

In this project you'll create a game in Scratch. You'll add sprites, program them to move, add sounds and keep score!

In the game a dinosaur will roam around and catch beetles for food.

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

Open the Scratch website in a new tab and create a new project. Delete the cat sprite from the project.

2 - Add a character

In the game we are going to make a dinosaur move around by using the arrow keys on our keyboard.

But first let's add the Dinosaur4 sprite from the sprite library.


3 - Shrink the dinosaur

When the dinosaur sprite gets added to our project, it's a bit too big for our game so let's add some code to shrink it and make it smaller.

Add the following code to the dinosaur sprite:

when green flag clicked set size to (50) %

Click on the green flag and the dinosaur sprite will shrink to half it's normal size.


4 - Make the dinosaur move up and down

Next we will program the dinosaur sprite to move up and down when we press the up and down arrow keys on our keyboard.

Each time we press an arrow key, we will point the dinosaur in that direction and then make them move 10 steps.

Add the following code to the dinosaur sprite:

when [up arrow v] key pressed point in direction (0) move (10) steps when [down arrow v] key pressed point in direction (180) move (10) steps

Once you've added the code test that it works by pressing the up and down arrow keys and checking that the dinosaur moves in the correct direction. If the dinosaur goes in the wrong direction, check that you have the right numbers in the code blocks.


5 - Make the dinosaur move left and right

Now we will program the dinosaur sprite to move left and right when we press the left and right arrow keys on our keyboard.

Each time we press an arrow key, we will point the dinosaur in that direction and then make them move 10 steps.

Add the following code to the dinosaur sprite:

when [left arrow v] key pressed point in direction (-90)// notice that this is MINUS 90 (-90) move (10) steps when [right arrow v] key pressed point in direction (90) move (10) steps

Once you've added the code test that it works by pressing the left and right arrow keys and checking that the dinosaur moves in the correct direction. If the dinosaur goes in the wrong direction, check that you have the right numbers in the code blocks.

You will notice that the dinosaur goes upside down when you make it move left. We can fix this by using the "set rotation style [left-right]" code block. Add it underneath the "set size to (50) %" block:

when green flag clicked set size to (50) % // add it under here set rotation style [left-right v]



6 - Add the Beetle sprite

The dinosaur is going to chase and eat beetles so next we're going to add the Beetle sprite from the sprite library.

Again we want to shrink this sprite and also we want to make it appear in a random position in the stage area.

Add the following code to the beetle sprite:

when green flag clicked set size to (30) % go to (random position v)

Click the green flag a few times. You'll notice that the beetle sprite appears in a different place each time.



7 - Make the beetle move on it's own

In the game, the beetle sprite will move by itself left and right, bouncing off the edges of the stage area.

We can program it to do this by using the "forever" block, which allows us to repeat some instructions.

Add the following code to the beetle sprite, underneath the "go to (random position)" block:

when green flag clicked set size to (30) % go to (random position v) // add the new code under here forever move (2) steps if on edge, bounce end

Notice that the shape of the "forever" block is different and you are able to put blocks inside it. Whatever blocks you put inside it will be repeated forever.

Click on the green flag and check that your beetle sprite appears, moves and bounces off the edges.


8 - Catch and eat the beetle

In the game when the dinosaur catches the beetle, it will be eaten. To program this we need to detect that the dinosaur sprite is touching the beetle sprite, and if it is then we will make the beetle go to a new random position (so it looks like the dinosaur has eaten the beetle and a new beetle has appeared).

Add the following code to the beetle sprite:

when green flag clicked forever if < touching (Dinosaur4 v) ? > then go to (random position v) end



9 - Add sound effects

What's a great way to improve your game? Add some sound effects! Let's play a bite sound every time the dinosaur eats the beetle.

Each sprite comes with some default sounds but we can add other sounds from the sound library.

First let's add the Bite sound from the sound library. You can see how by opening the green tip box below.

Once you've added the Bite sound, add in the "Play sound (pop) until done" block like below and change the sound from 'pop' to 'Bite'.

when green flag clicked forever if < touching (Dinosaur4 v) ? > then go to (random position v) // add it in under here play sound (Bite v) until done end

Now click on the green flag again and play the game, each time you catch the beetle you should hear a bite sound!

10 - Keep track of your score

What game would be complete without being able to score points?! Here's how to keep track of how many beetles your dinosaur has eaten.

First we will need to create a variable called "score". A variable is something that remembers a value, in this case the number of beetles we've eaten. You can see how by opening the green tip box below.

Now add the following code blocks to the beetle sprite:

Let's set your score to 0 at the start of the game.

when green flag clicked set (score v) to (0) // add this block in here forever if < touching (Dinosaur4 v) ? > then go to (random position v) play sound (Bite v) until done end

And then give yourself a point each time you eat a beetle. Look at the blocks in the Variables toolbox, can you guess which one you should use and where to put it?

see how to do it

when green flag clicked set (score v) to (0) forever if < touching (Dinosaur4 v) ? > then change (score v) by (1)// add this block in here go to (random position v) play sound (Bite v) until done end

12 - Improve the game?

That's it you've created the game! Now it's time to play it and see how many beetles you can catch.

Can you think of any ideas on how you could improve the game? Have a go at trying to add something new into your project, think about what code you will need to change or to add to.

11 - Add a backdrop

Finally let's add a backdrop to make our game look a bit better.

Add the "Jurassic" backdrop to your project from the backdrop library.


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