Welcome to our exciting coding adventure! Today, we are going to create a fun and interactive game called the 'Diver Game'. In this game, you will control a diver who is swimming in the ocean. Your mission is to collect as many starfish as you can while avoiding a shark that's trying to catch you! Sounds thrilling, right? Let's dive in!
In this game, we will have 3 characters, also known as sprites:
Are you ready to start coding your very own Diver Game? Let's go!
Create a new Scratch project and delete the cat sprite.
Go to the Scratch website using the link below and click on the 'Create' link in the blue bar at the top.
By default, each new project starts with the cat sprite already added. To delete the cat click on the x in the blue circle beside the cat in the sprite list.
Add the Underwater 1 backdrop to the project.
To add a backdrop from the backdrop library follow these steps:
You can use search box or the filter links (Fantasy, Music, Sports etc) to locate your backdrop.
Add the Diver1 sprite from the sprite library. We are going to position the diver at the top middle of the stage area and shrink him to half his size.
Add the following code to the Diver1 sprite.
when green flag clicked
go to x (0) y (150)
set size to (50) %
To add a sprite from the sprite library follow these steps:
You can use search box or the filter links (Animals, People, Fantasy etc) to locate your sprite.
Next we are going to program the diver to always point towards your mouse pointer and move towards it. This is how you will control where the diver swims to.
Add the following new code to the Diver1 sprite.
when green flag clicked
go to x (0) y (150)
set size to (50) % // add new code under here
forever
point towards (mouse-pointer v)
move (4) steps
endNow add the Starfish sprite to your project.
To add a sprite from the sprite library follow these steps:
You can use search box or the filter links (Animals, People, Fantasy etc) to locate your sprite.
At the start of the game the starfish will appear in a random position and each time you catch a starfish a new one will appear somewhere else.
Add the following code to the Starfish sprite to change it's size, hide it and create a copy of it.
when green flag clicked
set size to (30) %
hide
create clone of (myself v)
Now let's program what the starfish should do when a copy (or a clone) of it is created.
When the copy is created we will program it to:
Add the following code to the Starfish sprite.
when I start as a clone
go to (random position v)
show
forever
if < touching [Diver1 v] ? > then
create clone of (myself v)// create a new copy
delete this clone // delete this one
end
end
Time to add some thrill to our game! Let's introduce a shark. First, add the Shark 2 sprite from the library. Next, we will program the shark to appear at a random position and then start chasing the diver.
Add the following code to the Shark 2 sprite.
when green flag clicked
set size to (50) %
go to (random position v)
forever
point towards (Diver1 v)
move (2) steps // slower than the diver
endTo add a sprite from the sprite library follow these steps:
You can use search box or the filter links (Animals, People, Fantasy etc) to locate your sprite.
Now, let's add some code to stop the game if the shark catches the diver. We will use the block touching [Diver1] ? to sense if the shark sprite is touching the diver sprite. If it is, we will stop all the code so that the game is over.
Add the following new code to the Shark 2 sprite underneath the move 2 steps block.
when green flag clicked
set size to (50) %
go to (random position v)
forever
point towards (Diver1 v)
move (2) steps
if < touching [Diver1 v] ? > then
stop [all v]
end
end
Congratulations! You have successfully created your Diver Game. You've learned how to control a sprite with your mouse, make a sprite appear at random positions, and create a game where a sprite follows another. You've also learned how to use conditionals to trigger game events. Keep practicing these skills and see what other games you can create. Well done!
Every lesson in the course is ready to teach like this one. See what else comes with it, and how to order it.