Vous êtes sur la page 1sur 16

Page 1 of 16

05 Clean the Reef with Tidy Turtle Try harder

Story Our Friend Tidy Turtle is on a mission. He is tired of seeing a lot of


garbage in his underwater sea home. Between oil cans, glass bottles, plastic
bottles and paper, he has had enough! He doesnt understand why humans
cant use garbage cans to keep their waste organized? Why do they have to
dump it in his home the SEA? He has set out to clean the ocean but so has
his predator the Shark. Sharkie is cleaning the sea and looking for
something tasty to eat. Turtle is determined, but a little slow by nature. We
are making him slow by putting him through a maze where he can move Left,
right, up or down.

Get Free Coding lessons, videos & games NOW! Join us @ www.littlecodeninja.com

Page 2 of 16
In this game, Sharkie too has to go through Maze! Tidy turtle has 3 lives
against Sharkie and they are both on a contest to see how many trash items
they can clean up in 2 minutes!
Our job is to speed up Tidy turtle to clean the sea and also protect him from
Sharkie. Are you ready to code this super exciting adventure?
Learning Goals - Math, Language, Science, Programming, Logic Artificial
Intelligence (AI)
Scratch Functions used here Motions, Looks, Data, Events, Controls, More
Blocks, Sensing, Operators and Sound.

Key game components mastered


Animation of characters
Sharkie and Tidy Turtle make a lot of movements as they move around
cleaning the sea.
Random life like movements of Sprites/Characters.
Artificial Intelligence
Sharkie chases the fish without the help of player using some
advanced logic
Generate less code more function
Reuse Code multiple times using More Blocks
Play with nested loops and complex conditions
Use data variables to Store Score, Lives, Time information in the
games memory
All images are available at www.openclipart.org for free download.

Get Free Coding lessons, videos & games NOW! Join us @ www.littlecodeninja.com

Page 3 of 16

Code for the Shark

Creating random movements for Shark to move in the Maze:


Create Variables to store values for the X & Y axis Shark, Shark+, SharkThese are internal variables for the program to move the shark without the
players help. So we hide these variables. The player doesnt need to see it on

Get Free Coding lessons, videos & games NOW! Join us @ www.littlecodeninja.com

Page 4 of 16
the screen when the game is on. We set the variables Shark =0, Shark+=2,
Shark-=-2.
We set the variable Shark to be a random value between -2(Shark-) &
2(Shark+).
We use the Shark variables to first set the X coordinates. If the system
picks -1 or 0, we set the Shark value to be -2. if it is 1 we set Shark to 2.
Till the shark touches the wall, we change the X value by Shark value (-2 or
2). Shark will move all the way from -1 X coordinate to -240 X value unless it
hits another black wall going from -2,-4,-6..-240. Or if it starts from 1, it
will move 2,4,6..240 till it hits a black wall. Its doing SKIP COUNTING like
we learned in class! It it reaches an edge, it will bounce. Switch its costume,
wait a sec and move again.

Get Free Coding lessons, videos & games NOW! Join us @ www.littlecodeninja.com

Page 5 of 16
If it hits the wall of the maze, then it will have to change its direction to
the opposite side. To do this we will have to use our MULTIPLICATION with

Signs.
- times - = +

Get Free Coding lessons, videos & games NOW! Join us @ www.littlecodeninja.com

Page 6 of 16

+ times + = +
- times + = + times - = To change its direction and keep it following the same SKIP COUNTING, we
multiply the X value by (-1). If x was -2 and going right to left, X times (-1)
will now become +2! So it will move from left to right! How cool is that?!?
The opposite will also work just fine. If x was 2 moving from left to right, X
times (-1) will make it -2! So it will move from right to left on the X axis. You
can move the mouse on the scratch screen and check the values for X & Y on
the bottom right hand corner of the screen. This is a fun exercise to
understand your axis and how to move the characters!
Now we have created some really fun code to make the Shark move on its
own in the maze!

Get Free Coding lessons, videos & games NOW! Join us @ www.littlecodeninja.com

Page 7 of 16
We repeat the same code for the Y axis! Plain and easy!
This time the movement will be up and down. +ve 2,4, 6 through 180 goes up.
-2,-4,-6 through -180 goes down.

Get Free Coding lessons, videos & games NOW! Join us @ www.littlecodeninja.com

Page 8 of 16
We create 4 more variables 2 for keeping score Score Turtle & Score
Shark. 1 to track time in the game called Timer2 and another to give Tidy
turtle 3 lives called Life.
Now comes the fun stuff to customize the game to include all kinds of
tracking!
Turtles lives: If the shark touches the turtle, we change the Life variable
by -1. We use plain old subtraction to reduce the life of the turtle which was
set to 3 when we started the game by 1 every time they meet. We make the
shark change his costume to open his mouth to make it look like he is eating
the turtle!
Timer in the Game: We set each game to last 2 minutes (120 seconds). We
want to see who cleans the most trash in 2 minutes. We use the Less than &
greater than, equal to functions to show the correct message once the game
time is over. If the turtles score is less than Sharks score, we say you won
turtle!. If Sharks score is greater than Turtles score, we say You lost
turtle!. If both their scores are equal we say You both tied!. After each
message we display the scores for which we created a block from More
blocks called Show Scores. Show scores shows the timer, the scores of
Shark & turtle and stops the game!
Life of Turtle: If the life of the turtle comes down from 3, to 2, to 1 to 0,
we show the scores, but dont display any message. Because here the turtle
is not alive anymore to win or lose!

Get Free Coding lessons, videos & games NOW! Join us @ www.littlecodeninja.com

Page 9 of 16

Get Free Coding lessons, videos & games NOW! Join us @ www.littlecodeninja.com

Page 10 of 16

Code for the Turtle

The code to move Tidy Turtle was adapted from the book Scratch Programming in Easy Steps by Sean McManus

Get Free Coding lessons, videos & games NOW! Join us @ www.littlecodeninja.com

Page 11 of 16
Animating Tidy Turtle: Tidy Turtle is animated by switching 4 costumes,
one every second. Since we have a maze in the game, we move all sprites to
the front of the maze so the lines of the maze are behind the character and
not on top of it.
We give Tidy turtle a starting position of X -6 & Y 141. This is the fixed
position of Tidy turtle every time the game starts.
Tracking turtle position to navigate in the Maze: We create 2 variables
called Current Position of X & Current Position of Y. We use these variables
to store the current X & Y position of turtle as he moves around the maze.
We initialize the Timer2 variable to start here at 1 second and Turtles life
variable to start with 3 full lives!
Creating time to track the game - Timer2: Instead of initializing (setting
to a start value) and counting the variable timer when Flag clicked, we
initialize it on flag clicked, broadcast a message to start the timer. When
the timer receives the broadcast message, it will start storing the time in
seconds in the timer2 variable. We repeat tracking time till the variable
stores 120 seconds. At which time we execute the code in the Shark
variable to stop the game and display the scores.
Moving the turtle in the maze: To move him up, we use the up arrow
pressed key. We move 2 steps. You cane experiment with more / less steps
to move him faster or slower. Move him faster than the Sharkie, so the
game lasts longer.
To move his down the maze, we use the when key down arrow pressed.
Left and right movement is controlled by their respective left and right
arrow key pressed blocks from sensing. The whole sequence follows a simple

Get Free Coding lessons, videos & games NOW! Join us @ www.littlecodeninja.com

Page 12 of 16
if then else block. You can duplicate the blocks and change the keys instead
of writing them from scratch. Saves time!
Prevent the turtle to walk over the maze lines: The main function here is
to stop turtle from walking over the lines. We do this by checking to see if
he is touching the color black which is the color of the lines in the maze. If
Tidy is touching black, we keep him in his current position instead of moving
him in any direction. This is where the variables come to use. Since the X
position & Y position are changing all the time, the variables Current Position
of X & Current Position of Y help to store the exact position of the X & Y
axis. Using these variables,

helps us

make Tidy change

direction

(Up/down/left/right) to avoid crossing the line with the help of the player.

CODE FOR TRASH

Get Free Coding lessons, videos & games NOW! Join us @ www.littlecodeninja.com

Page 13 of 16

Get Free Coding lessons, videos & games NOW! Join us @ www.littlecodeninja.com

Page 14 of 16

The code for Trash has been adapted from our previous tutorials where we
have made the fish and worms move.
We create a block from More Blocks called Move Trash. This code helps us
release the trash from random places on the screen.
We create a block because we are using the code to move the trash 3 times
in the program. This will reduce the lines of Code that we write.
Trash Enters Screen: The move trash block works to move the trash in
random directions. It picks 3 random numbers = 0,1,2. If it is 1, it is on the
right side of the X axis. It sets the x position to be left corner (-240) and
moves the trash from left to right (90 deg). If it is 0 or 2, it sets the X

Get Free Coding lessons, videos & games NOW! Join us @ www.littlecodeninja.com

Page 15 of 16
position to the right corner of the screen (240) and points the trash from
right to left. The Y axis is set to pick a random value from the bottom of
the screen (-180) to the top of the screen (180). The reason is to create a
random inflow of trash on the screen where the shark or turtle cannot know
where the trash will come from.
Once the trashs entry point is determined by the move trash block, we
execute a forever loop.
Trash touches turtle: If the trash is touching the turtle, we increase
the score of the turtle, hide the trash to show that it is removed. We play a
sound to let the player know it was the turtle who scored. Then we wait
random seconds from 1-5 and then move the trash again as a new item onto
the screen.
Trash touches Shark: If the trash is touching the shark, we increase
the score for the shark, hide the trash to show it is removed. We play a
sound that is different than the one used when the turtle gets the trash, to
let the player know that the shark scored this time! After making the trash
disappear (hide) and waiting random seconds we re-introduce it back into the
screen. The trash will keep re-appearing on the screen till the game ends or
the stop button is pressed.
Turning the Trash: When the trash cans are on the screen, we create
a random turning range for them between -10 to 10 degrees. We have the
trash move 3 steps. Now it appears as though the trash cans are floating in
the water. If the trash cans reach the edge of the screen, we have it
bounce back into the screen.

Get Free Coding lessons, videos & games NOW! Join us @ www.littlecodeninja.com

Page 16 of 16
Copy the same code on every trash item that is created. Make sure that
each trash item has the relevant sounds selected in the sound tab to play
the music for the shark and the turtle.
In the true spirit of writing the code, all the code has been recycled and
modified from the Scratch community.

www.openclipart.org is used for

images not available in the Scratch library!

Get Free Coding lessons, videos & games NOW! Join us @ www.littlecodeninja.com

Vous aimerez peut-être aussi