- How to use communication between objects (sprites) in Scratch
- How to use logic in Scratch
- I will be able to make a game in Scratch using a series of instructions
In this lesson we are going to build upon what we did in lesson 4 and add in a shark. This will make the game more of a challenge. If you have finished lesson 4, you can open that to give you a head start. You can find lesson 4 here.
Finished product
You can see a preview of the finished game below:
You can download a near-complete version of Lesson 4 here.
Setup the shark and stage
- Add a shark sprite
- Set the backdrop to an underwater backdrop
- In the backdrop code, create a variable called lives
Resize your sprites
Resize all sprites to about 50% to make sure that it's not too big and not too small.
Diver: setup
Once again, the diver is going to follow the mouse. Make sure the following blocks are added to the diver make the diver follow the mouse:
Shark: setup
The shark's purpose is to catch the diver. In your shark sprite, add the following blocks:
Star: floating up code
Much like in lesson 4, we are going to add stars. The code blocks for our starts will be the same as in that lesson (shown on the left) but with an extra bit added that makes the blocks go up to the top:
Duplicate the stars
Duplicate your stars so you have multiple stars in the game, say 5 of them.
Diver: Lives
The diver will need lives and when they reach 0 the player loses.
To do this, add the following blocks to your diver, remembering to use the variable called lives:
The go to x: 138 y: 104 block is very important since it moves the diver away from the shark when they are touching it and prevents the shark removing all lives in one go.
The broadcast bite and wait part is also very important to ensure that the diver also tells the shark to move away when he is bitten. This broadcast feature is a really useful part of Scratch that allows sprites (also known as objects in computer programming) to communicate.
Shark: Bite code
On the shark there needs to be some code that lets the player know they have been bitten by the shark. Add the code blocks on the right to the shark to make a bite sound when the shark touches the diver and to move the shark when he bites the diver:
Diver: Win condition
Now we need to allow our diver to win by getting 10 points. To do this, add this sequence of blocks to your diver sprite:
Diver: Loss condition
Now we need to make it possible for our player to lose. To do this, add the following sequence to your diver:
-
1
Upload your screenshot below:
-
2
What does including the not block do in the code shown below?
-
3
How well do think you completed this task?
-
4
What parts of this task, if any, did you struggle with?
- Add in a rare item that the player can collect that would give them a live back
- Add in an item that if collected will take away lives.
- Advanced: Create an item that would allow the player to move faster (you will need a variable that stores the number of steps the player is to move)