2D - SDG 14 - Plastic Pollution Underwater (Difficulty: 2)
-
Key Topics Covered
- Cloning Sprites
- Set or Change Sprite Size
- Paint Costumes
- Using variables
- Switch Costumes
- Broadcasting messages
- Picking a random number
- Forever loops
- When touching event
- Repeat-until loops
Introduction
The SDGs are 17 goals adopted by the United Nations in 2015. They are a to-do list for everyone to work on together.
This tutorial is about removing plastic pollution in the seawater, which is related to the 14th goal of “Preserving life below water”.
A huge amount of plastic trash enters the sea every year, which threatens the wildlife below water. In this project, you will build a game to catch plastic trash. The game will be over if any plastic trash or the net touches any wildlife underwater.
Step 1 - Remix a Template Project
First, please click this link to open the project template:
https://play.creaticode.com/projects/2346d59cdaba3485820986ec
Click the “remix” button to make a remix project of your own. Note that you need to be signed in to do this.
This project contains 3 sprites and an underwater backdrop.
- The “Wildlife” sprite contains 8 costumes for fishes and other underwater wildlife.
- The “Plastic” sprite contains 4 costumes for plastic trash: plastic bag, cup, bottle and plate.
- The “Net” sprite contains a net that can be used to catch the plastic trash.
Step 2 - Clone the Wildlife Repeatedly
First, we’ll work on the Fish sprite. After the game starts, we want to clone a new wildlife every 3 seconds forever:
When you run the project, you won’t see the new clones because they are overlapping with the original sprite. So you have to drag the original sprite to a new position to see the clones.
Step 3 - Move the Cloned Wildlife to the Left Edge
All the wildlife will be swimming from the left edge to the right, so we will set their starting X position to -220. To make it interesting, we can set a random Y position for them, but we need to make sure they appear in the lower half of the stage. We can use the range between -50 and -170.
Now we see the clones appearing on the left edge:
Step 4 - Switch to a Random Costume
To make each new clone show a different costume, we can use the “switch costume” block. Note that this block not only allows you to choose from a list, but can also take a number as its input. So we can use the “pick random” block to generate a random number between 1 and 8. For example, when the number happens to be 1, then this clone will switch to costume number 1, which is the “gold fish”.
Now we get a random wildlife each time:
Step 5 - Hide the Original Sprite
The original sprite has been shown on the stage. We can simply hide it, so all the game logic is carried out by the clones. Note that when the original sprite is hidden, its clones will be hidden as well, so we need to show them after they are born:
Now we only get the clones, and the original sprite is hidden:
Step 6 - Make the Wildlife Swim
After each clone is born, we will make it swim to the right. We can increase their X position by 1 forever:
Now we get a flow of wildlife at the bottom of the sea:
Step 7 - Delete Clones
When a clone reaches the right edge of the stage, we will need to delete it. An obvious idea is to use the “touching edge” block like this:
However, it turns out to be a bad idea, since the clones will touch the left edge when they are born, so they will be deleted right away. To fix this issue, we can check if the X position is close to the right edge:
Now we see the clones will be deleted when they reach the right edge:
Step 8 - Game Over if Touching the Net
In the game, the player will be controlling the net to catch the plastic trash, and the net should not hurt any wildlife. Therefore, if the net ever touches any wildlife by accident, then the game is over. We can add a new variable named “game over” for this purpose.
- When the game starts, we set “game over” to 0, which means the game is not over
- When any clone of the wildlife touches the “Net” sprite, we set “game over” to 1.
Here is a simple test on the “game over” variable. It starts as 0, then changes to 1 when the fish touches the net.
Step 9 - Repeat Until the Game is Over
When the game is over, we should stop creating new clones, and also freeze the existing clones. We can use the “repeat until” block to replace the “forever” block, so that the repet*tion will stop when “game over” becomes 1.
When we test it, all the clones are frozen when the first fish touches the net:
Step 10 - Copy Blocks to the Plastic Sprite
Now let’s work on the “Plastic” sprite. It turns out we can reuse most of the code from the Wildlife sprite, because the plastic sprite also creates a lot of random clones that move on the stage. The key difference is the moving direction.
Therefore, we can copy the 3 stacks of blocks of the “Wildlife” sprite into the “Plastic” sprite.
Step 11 - Fix the Copied Blocks in the Plastic Sprite
Although it feels really good to copy a lot of blocks, you should also be very cautious, because Most of the bugs in programs come from “copy and paste”.
We need to change the new code carefully, so that the plastic sprite’s clones will move to the bottom randomly. Here are the changes you will need to make:
- Make the “Plastic” sprite generate a new clone every 1 second so we get more of them.
- The clones can only choose from 4 costumes
- The clones should go to the top of the stage with a random X position
- The clones should move down, with Y positions decreased by 2 each step
- If a clone’s Y position is below a number like -150, we delete it.
For a test, now we get both random wildlife and random plastic trash:
Step 12 - When to Delete the Plastic Trash
We also need to change how we delete the clones of the “Plastic” sprite. We should not delete it when it reaches the bottom, since the real plastic trash would stay at the bottom of the sea. Also, when the plastic touches the “Net” sprite, that means the net has collected this trash, so we should delete this clone.
Now the plastic trash will disappear when they touch the “Net” sprite:
Step 13 - Game Over if Trash is Touching the Wildlife
The goal of the player is to prevent the plastic trash from hurting the wildlife, so we should set “game over” to 1 whenever the trash is touching any wildlife:
For a test, the game stops when the wildlife touches the falling trash:
Step 14 - Click to Move the Net
Now let’s change the “Net” sprite a bit. There can be many ways for the player to control the “Net” sprite to move it around. Let’s pick a simple method: whenever the player clicks anywhere on the stage, we move the Net there. We can use the “when mouse button pressed” block.
Note that it requires us to create 2 new variables “x” and “y”, and we need to select them from the dropdown
Now the game can be played by controlling the Net:
Step 15 - Total Scores
Since all games have to have a goal to work towards, let’s award the player a point whenever he/she removes a trash item. We can add a new variable “Score”, set it to 0 when the game starts, and increase it whenever some plastic trash is removed. This can be done in the “Plastic” sprite:
Now the game is completed:
Enhancement
There are many ways you can build on top of this project. Here are some example ideas:
-
Add More Wildlife or Trash Costumes: Find out what other types of underwater wildlife are hurting from this problem, or other types of common plastic trash. Add them to the game, so people become aware of them.
-
Plastic Trash Speed: To make the game more challenging, you can make the trash fall at random speeds, or make them go faster and faster.
-