Simple Game Loops - an introduction Many games are constantly in motion. There is always something happening. Enemies are moving, the background is moving, a timer is running and often, you can lose the game by just doing nothing. These games have what is called a "game loop" which keeps looping over and over, driving all the moving objects on the screen. A Simple Game Loop - 2 examples Lets create a few simple loops to show you how it works. 1. Example #1 - "BURP ME" Message Game We are going to make a simple but annoying game. It will be called "The Baby Game". It will ask you to burp it every second. To do that, we are going to create a message handler called "burpMe". It will pop-up a dialog box that says "BURP ME" and you are going to have to answer it. It looks like this: on "burpMe" answer "Please BURP ME" with "Okay" end burpMe Put it on the Card Script Just doing it once will not bother you. But if we do it every second, that will keep you busy while you are playing your game. We do that by changing the code to this: on "burpMe" send "burpMe" to me in 2 secs answer "Please BURP ME" with "Okay" end burpMe Now it will ask you every second to burpMe. This is what a game loop is - a loop in the game. We have 2 problems. 1 - How do we start it up? and 2 - How do we stop it? #1 is simple - We just add a start button with the following code: Start Button: on mouseUp burpMe end mouseUp note: If you feel adventurous or curious, Save it now, then run it. You will find that you can not stop it. You have to quit out of the program #2 is harder - We need to know when to stop it and how to stop it. We need a variable (also called a 'flag') to tell us when to stop sending the messages. We need to change the Start button to set the flag to send the messages We need a Stop button to set the flag to not send the messages anymore We need code to stop sending the messages when we click the Stop button MAKE THE FOLLOWING ADDITIONS AND CHANGES: Start Button: global sendIt on mouseUp put true into sendIt burpMe end mouseUp Stop Button: global sendIt on mouseUp put false into sendIt end mouseUp Card Script: global sendIt on "burpMe" if sendIT is true then answer "Please BURP ME" with "Okay" send "burpMe" to me in 2 secs end if end burpMe ![]() Do you see how it now works? We use the variable "sendIt" as a 'flag' that is true or false. We check it in "burpMe" before sending the message. So we can turn it on or off with the 2 buttons 2 - Example #2 - Avoiding Falling Objects Lets create some falling objects that we have to avoid. We will have apples falling from the sky. We have to move past them without being hit. We add a button, call it "apple" and 'skin' it with the image of an apple. To make the apple drop from the sky we need code like this: set the top of the button "apple" to the top of the button "apple" + 10 When the apple hits the ground, we want it to start again from the top. We add code to start it from the top again: set the top of the button "apple" to the top of the button "apple" + 10 if the top of the button "apple" > the bottom of the card "level1" then set the bottom of the button "apple" to the top of the card "level1" end if We will put this code in it's own message handler and call the message "dropApple". PUT IT ON THE CARD SCRIPT Card Script: command dropApple set the top of the button "apple" to the top of the button "apple" + 10 if the top of the button "apple" > the bottom of the card "level1" then set the bottom of the button "apple" to the top of the card "level1" end if end dropApple We now need a way to run this code again, over and over, to keep the apples moving. To do that, we make it a message of it's own and we keep sending messages to ourself every sec to keep it going command dropApple set the top of the button "apple" to the top of the button "apple" + 10 if the top of the button "apple" > the bottom of the card "level1" then set the bottom of the button "apple" to the top of the card "level1" end if send "dropApple" to me in 1 secs end dropApple Do you see how this works? Now to Start it To start it, either add a button which sends it the first message “moveObj” or add the message when you open the card on openCard dropApple end openCard Stopping it !! (Very Important) You want it to stop after the game is over or whenever you go to another level (card). If you don't stop it, it will run on forever, or worse - cause your game to stop with an error. ( When you go to another card, it may not have a "moveObj" message handler. So the message "moveObj" will be sent to the new card in 2 seconds and it will not know what to do.) So we need to tell it when to stop sending itself the message. We use a variable for this and set it to either true or false. Add on the card script at the top: global stillGoing Add on the card script in the openCard handler: on openCard put true into stillGoingdropApple end openCard Add on the card script in your moveObj handler: command dropApple if stillGoing then set the top of the button "apple" to the top of the button "apple" + 10 if the top of the button "apple" > the bottom of the card "level1" then set the bottom of the button "apple" to the top of the card "level1" end if end if command dropApple Add on the card script whenever you stop the game or go to another card: put false into stillGoing |
Game Programming >




