
-----------------------------------
shorthair
Mon Feb 16, 2004 10:09 pm

[Tutorial][Blitz] How To Build A Game
-----------------------------------
Blitz Help

The status variable contains the "state" of the game, which is either displaying a title page or allowing the player to steer round the screen. Extra states such as player dies with cosmic explosion and gameover screen would be added to extend the game further.

The main loop, then uses the Flip command to perform the double buffering (allowing us to draw to one screen while the other is displayed) and then either prints a message informing the user to press Enter to start or calls the UpdatePlayer() function.

BackBuffer()

Global status=0,x#=0,y#=0,speed#=1,dir=1
; eat the dots
Graphics 640,480

SetBuffer 
; main loop

While Not KeyHit(1)

; refresh screen

Flip

Cls

Color 255,255,0

Rect 0,0,640,480,0

; select state

Select status

Case 0

Locate 100,100

Print "Press Enter To Start"

If KeyHit(28) InitGame() 

Case 1

UpdatePlayer()

End Select

Wend
 

What UpdatePlayer() function you ask? And if the user presses Enter what's this InitGame() function?

Unlike traditional BASIC languages where we would implement these functions as subroutines and call them with the Gosub command BlitzBasic features user defined functions. 

Add the following two functions at the bottom of the above program to allow the program to run.

The first function initializes the variables we will need inorder to steer the players rectangle around the screen. Note how these variables have been declared at the top of the program as Global which allows us to access them from inside functions such as InitGame().

Function InitGame()
x=320

y=240

speed=1

dir=1

status=1

End Function 
 

This second function changes the players direction depending on the arrow key they are pressing or the direction of the joystick. The code then moves the players position (x,y) depending on the dir variable which corresponds to up, right, down and left respectively.

Function UpdatePlayer()
; steer player 

If KeyDown(200) Or JoyY()0.5 dir=1

If KeyDown(208) Or JoyY()>0.5 dir=2

If KeyDown(203) Or JoyX()