Creating A Game Loop
Over and Over Again
A lot of games have something called a game loop. This is just a way to run some code over and over to check to see whatâs happening or to update stuff constantly.
Letâs go program our other sprite for a moment.
when green flag clicked
forever
next costume
wait (1) seconds
end
Now, youâll see that our monkey is cycling through itâs costumes once a second. Itâs not much, but it helps show what weâre talking about. This code will run until the game stops, which right now will only happen when we hit the red stop sign.
Sensing and Saying
We can use this loop to check for different conditions. In this case, weâre constantly checking to see if Arnold is touching the other sprite. If so, then weâll run a little block of code. If not, weâll just keep checking!
when green flag clicked
go to x: [-172] y: [-117]
forever
if <touching (Monkey v)?> then
say [Hello] for [2] seconds
end
end
We could add some additional functionality that puts Arnold back where he is supposed to start as well.
when green flag clicked
point in direction [90]
go to x: [-172] y: [-117]
forever
if <touching (Monkey v)?> then
say [Hello] for [2] seconds
glide [1] secs to x: [-172] y: [-117]
end
end
You could also choose to break apart reseting the game and the game loop. It doesnât really matter, but part of coding is keeping things organized so that itâs easy to find the piece that you need to change later.
when green flag clicked
point in direction [90]
go to x: [-172] y: [-117]
when green flag clicked
forever
if <touching (Monkey v)?> then
say [Hello] for [2] seconds
glide [1] secs to x: [-172] y: [-117]
end
end
You can see the completed version of what we have so far here.
But, with that, I think we should probably go make a game, right? Letâs make a super simple game called Taco Chase. For our game, weâre going to need to create a sprite.