Simple Game Loops

Simple Game Loops

In a live action game, much is always going on - on the screen. A clock or timer is running, objects are moving across the screen, your player is moving, collisions are happening and sometimes even the landscape is moving. All of this could be happening without you doing anything.

The computer is running what is called a "Game Loop", constantly moving, checking and updating things on the screen and behind the scenes.  This code is always running, in the background. In that game loop, we add our code so that it gets checked and run too.

In the last lesson, We made a message handler ("moveObj" or "moveEnemy") that sent itself messages over and over. Now we will create a Game Loop to handle moving the backgrounds and everything else going on in the game.

Creating a Game Loop in your Program

Here is what you have to do to put a Game Loop in your program:

1. Create a button to move - name the button  "box"
     This is just the name that I used in the card script to move it. You can change it to whatever name that you wish.

2. Open the Card Inspector - name the card "Level1"
     This is just the name that I used in the card script to move the button "box". You can change it to whatever name that you wish.

3. Create a Start Button, add this to its script (the object script)

    on mouseUp
        global gameRunning
        put true into gameRunning
        gameLoop
    end mouseUp


4. Create a Stop button, add this to its script (the object script
)

    on mouseUp
        global gameRunning
       put false into gameRunning
    end mouseUp


5. Add this script to your card script (after the on arrow keys message handler)
The game loop (in the card code) is run every 50 milliseconds when the screen is updated:

    on gameLoop  
        global gameRunning
       if gameRunning then      
          moveBox
          --      moveTerrain
          --      moveEnemy
          --      detectCollisions
          --      updateScore  
         send gameLoop to me in 2 ticks    
       end if   
    end gameLoop


That's it. It should work after you customize the code for your game

Notes:
(The dashes "--" in the lines above comment out the lines so they do not run. Each line runs a different handler. Un-comment them one at a time when you add the handler and its code. 

for instance:
if you have enemies that move across the screen, un-comment out that line - take out the dashes in front of it: 
      
      moveEnemy

then add code below:

    on moveEnemy
       ..... add your code to move the enemy
       set the left of the button "enemy1" to the left of the button "enemy1" - 4
       if the right of the button "enemy1" < 0 then
            set the left of the button "enemy1" to the right of the card "level1"
       end if
    end moveEnemy
 

Un-comment the other ones (moveTerrain, detectCollisions and updateScore) as you add their handlers and their code

on updateScreen  
       if sGameRunning is true then      
          moveBox
          --      moveTerrain
          --      moveEnemy
          --      detectCollisions
          --      updateScore      
       end if   
    end updateScreen
Comments