| ' Tutorial 3: The Game Loop ' -----------------SETUP GAME CONDITIONS-------------------- Graphics 640,480,0 Local URL:String="http::www.2dgamecreators.com/tutorials/gameprogramming/basic/" Local Player:TSpaceShip = TSpaceShip.Create(URL+"/blobship_1-1.png",320,420) ' ---------------------MAIN LOOP------------------------- Repeat Cls Player.UpdateState() Player.DrawSelf() Flip Until KeyDown(KEY_ESCAPE) Or AppTerminate() End ' ---------------------TYPES, ATTRIBUTES AND BEHAVIOURS------------------------- Type TSpaceShip Field X:Int = 320 Field Y:Int = 420 Field Speed:Int=3 Field Image:TImage Function Create:TSpaceShip(File:String,xstart:Int,ystart:Int) Local Ship:TSpaceShip=New TSpaceShip Ship.X=xstart Ship.Y=ystart Ship.Image=LoadImage(LoadBank(File)) If Ship.Image=Null Print "Not able to load image file. Program aborting" End EndIf Return Ship End Function Method UpdateState() If KeyDown(KEY_LEFT) X :- Speed EndIf If KeyDown(KEY_RIGHT) X :+ Speed EndIf If X<0 Then X=0 If X>620 Then X=620 End Method Method DrawSelf() DrawImage Image, X,Y End Method End Type |
| Local URL:String="http::www.2dgamecreators.com/tutorials/gameprogramming/basic/" Local Player:TSpaceShip = TSpaceShip.Create(URL+"/blobship_1-1.png",320,420) |
| Type TSpaceShip Field X:Int = 320 Field Y:Int = 420 Field Speed:Int=3 Field Image:TImage Function Create:TSpaceShip(File:String,xstart:Int,ystart:Int) Local Ship:TSpaceShip=New TSpaceShip Ship.X=xstart Ship.Y=ystart Ship.Image=LoadImage(LoadBank(File)) If Ship.Image=Null Print "Not able to load image file. Program aborting" End EndIf Return Ship End Function |
| Repeat Cls Player.UpdateState() Player.DrawSelf() Flip Until KeyDown(KEY_ESCAPE) Or AppTerminate() |
The Game Loop will continue to repeat until the ESCAPE key is pressed or Window Close button is clicked.
The KeyDown(KEY_ESCAPE) function will be true when then the ESCAPE key is pressed. Similarly the AppTerminate() function will be true when the Window Close Button is clicked.
So the loop will exit when either the ESCAPE key is pressed OR the Close button is clicked.
Notice below how we now have numbers after the Field
variable declaration. BlitzMax allows us to assign default numbers to
variables. In this instance we want our spaceship to be located at
coordinates 320,420 at the beginning of the game. If you have not noticed this
before, try re-running the above program again. The speed (which in
this case is the number of pixels moving per frame
is set to 3).
|
Type TSpaceShip Field X:Int = 320 Field Y:Int = 420 Field Speed:Int=3 |
The UpdateState Method now checks for keypresses, if the LEFT cursor key was pressed, then the location variable X is reduced by the amount stored in variable Speed, in this case 3 pixels. Similarly for when the RIGHT cursor key is pressed the X position increases by 3 pixels.
| Method UpdateState() If KeyDown(KEY_LEFT) X :- Speed EndIf If KeyDown(KEY_RIGHT) X :+ Speed EndIf If X<0 Then X=0 If X>620 Then X=620 End Method |
The IF statements are then used to make sure the location variable X does not go outside the screen..
| Method UpdateState() If KeyDown(KEY_LEFT) X :- Speed EndIf If KeyDown(KEY_RIGHT) X :+ Speed EndIf If X<0 Then X=0 If X>620 Then X=620 End Method |
| Method DrawSelf() DrawImage Image, X,Y End Method |
| Function AppTerminate() | ||
| True if user has requested to terminate application. |
| Function KeyDown( key ) | ||
| Check for key state, returns True if key is currently down.. |
| Keyword Function | ||
| Begin a Function declaration. |
| Keyword Return | ||
| Return from a Function. |
| Keyword EndFunction | ||
| End a Function declaration. |