05 clean the reef hard
TRANSCRIPT
-
7/26/2019 05 Clean the Reef Hard
1/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 organied "hy do they ha#e to
dump it in his home the $%& He has set out to clean the ocean but so has
his predator the $hark. $harkie is cleaning the sea and looking forsomething tasty to eat. Turtle is determined, but a little slow by nature. "e
are making him slow by putting him through a mae where he can mo#e 'eft,
right, up or down.
(et Free )oding lessons, #ideos * games +O"! oin us - www.littlecodenina.com
http://www.littlecodeninja.com/http://www.littlecodeninja.com/ -
7/26/2019 05 Clean the Reef Hard
2/16
Page 2 of 16
/n this game, $harkie too has to go through 0ae! Tidy turtle has 1 li#es
against $harkie and they are both on a contest to see how many trash items
they can clean up in 2 minutes!
Our ob is to speed up Tidy turtle to clean the sea and also protect him from
$harkie. &re you ready to code this super e3citing ad#enture
Learning Goals4 0ath, 'anguage, $cience, 5rogramming, 'ogic 6 &rtificial
/ntelligence 7&/8
Scratch Functions used here6 0otions, 'ooks, 9ata, %#ents, )ontrols, 0ore
Blocks, $ensing, Operators and $ound.
Key game comonents mastered
&nimation of characters
$harkie and Tidy Turtle make a lot of mo#ements as they mo#e around
cleaning the sea.
:andom life like mo#ements of $prites;)haracters.
&rtificial /ntelligence
$harkie chases the fish without the help of player using some
ad#anced logic
(enerate less code more function
:euse )ode multiple times using se data #ariables to $tore
-
7/26/2019 05 Clean the Reef Hard
3/16
Page 3 of 16
Code for the Shar!
Creating random mo"ements for Shar! to mo"e in the #a$e%
)reate ?ariables to store #alues for the @ * A a3is 6 $hark, $hark, $hark4
These are internal #ariables for the program to mo#e the shark without the
players help. $o we hide these #ariables. The player doesnt need to see it on
(et Free )oding lessons, #ideos * games +O"! oin us - www.littlecodenina.com
http://www.littlecodeninja.com/http://www.littlecodeninja.com/ -
7/26/2019 05 Clean the Reef Hard
4/16
Page 4 of 16
the screen when the game is on. "e set the #ariables $hark CD, $harkC2,
$hark4C42.
"e set the #ariable &Shar!' to be a random #alue between 427$hark48 *
27$hark8.
"e use the $hark #ariables to first set the @ coordinates. /f the system
picks 4E or D, we set the $hark #alue to be 42. if it is E we set $hark to 2.
Till the shark touches the wall, we change the @ #alue by $hark #alue 742 or
28. $hark will mo#e all the way from 4E @ coordinate to 42D @ #alue unless it
hits another black wall going from 42,4,4G..42D. Or if it starts from E, it
will mo#e 2,,G..2D till it hits a black wall. /ts doing $/5 )O>+T/+( like
we learned in class! /t it reaches an edge, it will bounce. $witch its costume,
wait I a sec and mo#e again.
(et Free )oding lessons, #ideos * games +O"! oin us - www.littlecodenina.com
http://www.littlecodeninja.com/http://www.littlecodeninja.com/ -
7/26/2019 05 Clean the Reef Hard
5/16
Page 5 of 16
/f it hits the wall of the mae, then it will ha#e to change its direction to
the opposite side. To do this we will ha#e to use our 0>'T/5'/)&T/O+ with
$igns.
4 times 4 C
(et Free )oding lessons, #ideos * games +O"! oin us - www.littlecodenina.com
http://www.littlecodeninja.com/http://www.littlecodeninja.com/ -
7/26/2019 05 Clean the Reef Hard
6/16
Page 6 of 16
times C
4 times C 4
times 4 C 4
To change its direction and keep it following the same $/5 )O>+T/+(, we
multiply the @ #alue by 74E8. /f 3 was 42 and going right to left, @ times 74E8
will now become 2! $o it will mo#e from left to right! How cool is that!
The opposite will also work ust fine. /f 3 was 2 mo#ing from left to right, @
times 74E8 will make it 42! $o it will mo#e from right to left on the @ a3is. Aou
can mo#e the mouse on the scratch screen and check the #alues for @ * A on
the bottom right hand corner of the screen. This is a fun e3ercise to
understand your a3is and how to mo#e the characters!
+ow we ha#e created some really fun code to make the $hark mo#e on its
own in the mae!
(et Free )oding lessons, #ideos * games +O"! oin us - www.littlecodenina.com
http://www.littlecodeninja.com/http://www.littlecodeninja.com/ -
7/26/2019 05 Clean the Reef Hard
7/16
Page 7 of 16
"e repeat the same code for the A a3is! 5lain and easy!
This time the mo#ement will be up and down. #e 2,, G through EJD goes up.
42,4,4G through 4EJD goes down.
(et Free )oding lessons, #ideos * games +O"! oin us - www.littlecodenina.com
http://www.littlecodeninja.com/http://www.littlecodeninja.com/ -
7/26/2019 05 Clean the Reef Hard
8/16
Page 8 of 16
"e create more #ariables 6 2 for keeping score 6 $core Turtle * $core
$hark. E to track time in the game called Timer2 and another to gi#e Tidy
turtle 1 li#es called 'ife.
+ow comes the fun stuff to customie the game to include all kinds of
tracking!
Turtle(s li"es%/f the shark touches the turtle, we change the 'ife #ariable
by 4E. "e use plain old subtraction to reduce the life of the turtle which was
set to 1 when we started the game by E e#ery time they meet. "e make the
shark change his costume to open his mouth to make it look like he is eating
the turtle!
Timer in the Game%"e set each game to last 2 minutes 7E2D seconds8. "e
want to see who cleans the most trash in 2 minutes. "e use the 'ess than *
greater than, eKual to functions to show the correct message once the game
time is o#er. /f the turtles score is less than $harks score, we say
-
7/26/2019 05 Clean the Reef Hard
9/16
Page 9 of 16
(et Free )oding lessons, #ideos * games +O"! oin us - www.littlecodenina.com
http://www.littlecodeninja.com/http://www.littlecodeninja.com/ -
7/26/2019 05 Clean the Reef Hard
10/16
Page 10 of 16
Code for the Turtle
The code to move Tidy Turtle was adated from the !oo" # $cratch Programmi%g i% &asy $tes !y $ea% 'c'a%us
(et Free )oding lessons, #ideos * games +O"! oin us - www.littlecodenina.com
http://www.littlecodeninja.com/http://www.littlecodeninja.com/ -
7/26/2019 05 Clean the Reef Hard
11/16
Page 11 of 16
)nimating Tidy Turtle%Tidy Turtle is animated by switching costumes,
one e#ery second. $ince we ha#e a mae in the game, we mo#e all sprites to
the front of the mae so the lines of the mae are behind the character and
not on top of it.
"e gi#e Tidy turtle a starting position of @ 4G * A EE. This is the fi3ed
position of Tidy turtle e#ery time the game starts.
Trac!ing turtle osition to na"igate in the #a$e% "e create 2 #ariables
called )urrent 5osition of @ * )urrent 5osition of A. "e use these #ariables
to store the current @ * A position of turtle as he mo#es around the mae.
"e initialie the Timer2 #ariable to start here at E second and Turtles life
#ariable to start with 1 full li#es!
Creating time to trac! the game * Timer+% /nstead of initialiing 7setting
to a start #alue8 and counting the #ariable timer when Flag clicked, we
initialie it on flag clicked, broadcast a message to start the timer. "hen
the timer recei#es the broadcast message, it will start storing the time in
seconds in the timer2 #ariable. "e repeat tracking time till the #ariable
stores E2D seconds. &t which time we e3ecute the code in the $hark
#ariable to stop the game and display the scores.
#o"ing the turtle in the ma$eL To mo#e him up, we use the
-
7/26/2019 05 Clean the Reef Hard
12/16
Page 12 of 16
if then else block. Aou can duplicate the blocks and change the keys instead
of writing them from scratch. $a#es time!
,re"ent the turtle to wal! o"er the ma$e lines%The main function here is
to stop turtle from walking o#er the lines. "e do this by checking to see if
he is touching the color black which is the color of the lines in the mae. /f
Tidy is touching black, we keep him in his current position instead of mo#ing
him in any direction. This is where the #ariables come to use. $ince the @
position * A position are changing all the time, the #ariables )urrent 5osition
of @ * )urrent 5osition of A help to store the e3act position of the @ * A
a3is. >sing these #ariables, helps us make Tidy change direction
7>p;down;left;right8 to a#oid crossing the line with the help of the player.
C-./ F-R TR)S
(et Free )oding lessons, #ideos * games +O"! oin us - www.littlecodenina.com
http://www.littlecodeninja.com/http://www.littlecodeninja.com/ -
7/26/2019 05 Clean the Reef Hard
13/16
Page 13 of 16
(et Free )oding lessons, #ideos * games +O"! oin us - www.littlecodenina.com
http://www.littlecodeninja.com/http://www.littlecodeninja.com/ -
7/26/2019 05 Clean the Reef Hard
14/16
Page 14 of 16
The code for Trash has been adapted from our pre#ious tutorials where we
ha#e made the fish and worms mo#e.
"e create a block from 0ore Blocks called 0o#e Trash. This code helps us
release the trash from random places on the screen.
"e create a block because we are using the code to mo#e the trash 1 times
in the program. This will reduce the lines of )ode that we write.
Trash /nters Screen% The mo#e trash block works to mo#e the trash in
random directions. /t picks 1 random numbers C D,E,2. /f it is E, it is on the
right side of the @ a3is. /t sets the 3 position to be left corner 742D8 and
mo#es the trash from left to right 7MD deg8. /f it is D or 2, it sets the @
(et Free )oding lessons, #ideos * games +O"! oin us - www.littlecodenina.com
http://www.littlecodeninja.com/http://www.littlecodeninja.com/ -
7/26/2019 05 Clean the Reef Hard
15/16
Page 15 of 16
position to the right corner of the screen 72D8 and points the trash from
right to left. The A a3is is set to pick a random #alue from the bottom of
the screen 74EJD8 to the top of the screen 7EJD8. 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 mo#e trash block, we
e3ecute a fore#er loop.
Trash touches turtle%/f the trash is touching the turtle, we increase
the score of the turtle, hide the trash to show that it is remo#ed. "e play a
sound to let the player know it was the turtle who scored. Then we wait
random seconds from E4N and then mo#e the trash again as a new item onto
the screen.
Trash touches Shar!%/f the trash is touching the shark, we increase
the score for the shark, hide the trash to show it is remo#ed. "e 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! &fter making the trash
disappear 7hide8 and waiting random seconds we re4introduce it back into the
screen. The trash will keep re4appearing on the screen till the game ends or
the stop button is pressed.
Turning the Trash%"hen the trash cans are on the screen, we create
a random turning range for them between 4ED to ED degrees. "e ha#e the
trash mo#e 1 steps. +ow it appears as though the trash cans are floating in
the water. /f the trash cans reach the edge of the screen, we ha#e it
bounce back into the screen.
(et Free )oding lessons, #ideos * games +O"! oin us - www.littlecodenina.com
http://www.littlecodeninja.com/http://www.littlecodeninja.com/ -
7/26/2019 05 Clean the Reef Hard
16/16
Page 16 of 16
)opy the same code on e#ery trash item that is created. 0ake sure that
each trash item has the rele#ant sounds selected in the sound tab to play
the music for the shark and the turtle.
/n the true spirit of writing the code, all the code has been recycled and
modified from the $cratch community. www.openclipart.org is used for
images not a#ailable in the $cratch library!
(et Free )oding lessons, #ideos * games +O"! oin us - www.littlecodenina.com
http://www.openclipart.org/http://www.littlecodeninja.com/http://www.openclipart.org/http://www.littlecodeninja.com/