You are on page 1of 11

RACE GAME PROGRESS -------11%

The game loop


Fast games need clever code. This game uses
.----
EXPERT TIPS
Ci amelo o p s
something called a "game loop" to keep ali the
action happening just when it should. lt's as if Using one main loop to keep
everything in sync is common in
the game loop bangs a drum, and with each
computer games. The loop keeps
beat ali the other sprites move one step. Start ali the sprites in step and makes
by creating a blank sprite to hold the game the code tidy and short. lt also
loop's script. helps the game run quickly-in
Glacier Race, the game loop runs
as fast as 30 times per second.
1
Start a new project and delete the cat sprite. Use the paintbrush In Scratch, a program with lots of
symbol / to create a blank sprite and rename it "Game Loop". sprites each with their own loops
Then make a variable for all sprites called "Countdown"for the can become slow as the computer
game timer and show it on the stage. Build the following script
has to constantly jump between
to make the game loop. You'II need to create the messages
them. Using a single game loop
"Setup'; "Calculate'; "Move'; and "Game Over''.
fixes this problem, but be careful
not to use loops elsewhere in the
(reate the variable game because they will slow it down.
"Countdown" in the
when ,,. dlckad
ata section.
_)
D
broadcast ;;;
ISetup ,.. 1!...:..
and ::..::._____
walt
_ ;_:;::; ;;;
L
1/ < [TI
_ _ 2
(reate two new variables for all sprites: "RoadY " (to
repeat until Countdown store the y coordinate used to position our moving
scenery) and "CarSpeed" (to set how quickly the cars
when I realve ! Setup,.. 1
broadcast I C.lculata ,..1 and -lt can move around the stage). Uncheck the boxes in
the Data section so they aren'! displayed on the
set I RoadY,.. 1 to @]

broadcast I Mon ,..1 and walt stage. Add the script on the right to set the values
set I CarSpeed ... to [!]
of the variables at the start of the game.

set I Countdown ... to

broadcast ! Game Over ,..


1 Name the This block sets the time limit
for the game in seconds.
message here.
.........._
New Message
broadcast ! mauape1 ,.. ! Use the "broadcast" \ The y coordinate of the

3
Add another variable for all
____-
block to create the road decreases from 360
messagel
,________
messages for your
script.
Message name: [S_etu
_ p sprites called "RoadSpeed"
to store the speed of the
when I realve I C.lculate ,..1 to -360 beforejumping
back to 360 as the road

J [ set I RoadSpeed -.. to l=!l


-----
moving scenery. Uncheck
new message... \ OK Cancel the box. Then create
a script to calculate the change I RoadY,.. 1 by RoadSpeed
6 How does it work?
position of the road each
When the project runs, the script sends out a "Setup" message time the game loop runs.
if RoadY 1-3601 then

....,
that tells all the sprites to get ready for the game. lt waits for You'II see how this works <
them to finish, and then the main loop begins. The loop sends out once you've made the
I RoadY,.. 1
B11
messages telling every sprite in the game when to run each part road sprites. change by @
of their code. The loop ends only when the countdown reaches
zero, at which point the "Game Over!" message is sent so all sprites
can perform any final actions and the winner is announced.

You might also like