How to Create a Chase (Shark) Game on Scratch

Ryan Barone
January 24, 2024

We love Scratch as one of the best resources for kids who want an easy and fun way to be introduced to coding. 

In this lesson, we will show how to create a "Chase Game" where the player controls a sprite with their arrow keys, chasing other sprites around the map to score points. 

(If your young coder has yet to work with Sensing and Motion blocks, you might want to check out our previous tutorials on how to create a game with Scratch.)

how to create chase game scratch.png

Setup

Set up your game by choosing one backdrop, one player sprite, and three chase sprites. This example uses the "Shark" and "Fish" sprites.

Click on Stage > Sounds > Choose a Sound and search for a song to play on loop for background music.

In the Stage code area, add  when ???? clicked  and a  forever ↺  block with play sound until done.


Create a Score Variable

1. Select the player sprite (e.g. "Shark") and create a score variable. 

2. Add when flag clicked with set score to 0 and go to x, y

3. Choose an x, y location near the bottom left of the game screen.


Code the Player's Movement

In this game, the player will be able to move with arrow keys.

1. Add four when key pressed blocks to the code area.

2. Change each block to up arrow, down arrow, right arrow, and left arrow.

3. On each block, attach a repeat 10 block.

4. For up/down arrow: Add the  change y block inside, with 2 for up and -2 for down.

5. For right/left arrow: Add the change x block inside, with 2 for right and -2 for left.

Learn more about how to make a snake game in Scratch


 

Pause for a couple of Scratch coding tips:

Using repeat blocks helps make the x and y movements feel smooth, like how a real shark would move.

Also, you can use the switch costume block to change the direction your sprite faces. This will help show your player which directional key they are pressing.

To do this, you just need to Duplicate the Shark's costume. Name the original costume shark-right. On your duplicate, press the Flip Horizontal button to change its direction and then name it shark-left.

Moving back into the tutorial, in this particular game, there will be three fish sprites being chased around the game screen. 


Code the Fish

1. Start by coding one sprite first. You'll copy the code later.

2. Add when ???? clicked to the Fish's code and attach the go to random position block.

3. Add a forever ↺  block with move 3 steps, turn ↻ 1 degree, another move 3 steps, and if on edge, bounce inside of it.


Sense the Shark

1. Add another when ???? clicked to the Fish's code area.

Attach a forever ↺ block with an if <> then block inside of it.

Drag <touching Shark?> into the if statement bracket.

Inside the if statement, add  start sound, change score by 1 , turn ↻  180 degrees, and wait 0.5 seconds.

Choose any sound you want for your sprite. This example uses "Coin".


Add Brightness

You can use the set brightness effect block to turn your chase sprites white, making it easier to tell when they've touched the Shark.

  1. Replace if <> then with the if <> then, else block.
  2. Attach set brightness effect to 100 below the start sound block.
  3. Attach set brightness effect to 0 in the else statement.

And just like that, your chase game is complete! But why stop there?

Right now, the fish don't really run away from the shark that's chasing them. If you'd like to make this game a little more challenging for the player, increase the difficulty:

1. Add another when flag clicked to the Fish's code area.
2. Attach a forever ↺ block with an if <> then block inside of it.
3. From Operators, drag a <  block into your code area.
4. Drag (distance to mouse-pointer) into the first bubble, and type 60 into the second bubble. Change the sensor to "Shark".

Thanks to the go to random position block, each fish starts at a different location every time. (If you don't see your score variable, make sure the checkbox is turned on in your Variables list.)

With this Chase Game, we uncovered how to make sprites detect other sprites and run away from them! To take things to the next level, customize the game with extra code or custom art and sounds. Or, if you need help, check out our Scratch coding summer camps and Scratch tutoring lessons

Meet iD Tech!

Sign up for our emails to learn more about why iD Tech is #1 in STEM education! Be the first to hear about new courses, locations, programs, and partnerships–plus receive exclusive promotions! Online camps, Roblox coding classes, coding summer courses, and more. 

By signing up you agree to our Privacy Policy
Subscribe & Save!

Meet iD Tech!

Sign up for our emails to learn more about why iD Tech is #1 in STEM education! Be the first to hear about new courses, locations, programs, and partnerships–plus receive exclusive promotions! Online camps, Roblox coding classes, coding summer courses, and more. 

By signing up you agree to our Privacy Policy

Meet iD Tech!

Sign up for our emails to learn more about why iD Tech is #1 in STEM education! Be the first to hear about new courses, locations, programs, and partnerships–plus receive exclusive promotions! Online camps, Roblox coding classes, coding summer courses, and more. 

By signing up you agree to our Privacy Policy
Subscribe & Save!

Meet iD Tech!

Sign up for our emails to learn more about why iD Tech is #1 in STEM education! Be the first to hear about new courses, locations, programs, and partnerships–plus receive exclusive promotions! Online camps, Roblox coding classes, coding summer courses, and more. 

By signing up you agree to our Privacy Policy