Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 [Tutorial][Blitz] How To Build A Game
Index -> Programming, Visual Basic and Other Basics -> Other Basics
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message

PostPosted: Mon Feb 16, 2004 10:09 pm   Post subject: [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.


Global status=0,x#=0,y#=0,speed#=1,dir=1
; eat the dots
Graphics 640,480

; main loop

While Not KeyHit(1)

; refresh screen



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


End Select


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()





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=0

If KeyDown(205) Or JoyX()>0.5 dir=1

If KeyDown(208) Or JoyY()>0.5 dir=2

If KeyDown(203) Or JoyX()<-0.5 dir=3

; move player

Select dir

Case 0 y=y-speed

Case 1 x=x+speed

Case 2 y=y+speed

Case 3 x=x-speed

End Select

; draw player

Color 255,255,255

Rect x,y,10,10

End Function

After adding the InitGame() and UpdatePlayer() code the game should run.

Next it's time to add some deadly rocks and some yummy food.

In order to do this we create some new Types that will hold all the information we need for each rock and food. To begin with these Types will simply hold the x and y position of each rock and food element we create for our game.

Place the following Type declarations at the top of the program.

Type food
Field x,y

End Type

Type rock

Field x,y

End Type

The following code then needs to be added to the InitGame() function, insert it after the line that reads status=1.

This code creates 20 rocks that will kill the player and 20 food that will speed the player up. The New command creates a new object and also adds it to a list. We set the position of each rock and food by setting the x and y fields of each new object created using the backslash \ character to denote which field.

For i=0 To 20
r.rock=New rock




For i=0 To 20 food




We now need a function that draws all the food and rocks each frame and checks if the player has collided with any.

Note how we can loop through each food and rock element that exist using the For..Each command pair. This is another great feature of BlitzBasic that keeps programs simple and easy to read.

We use the RectsOverlap command to check of the players position (x,y) collides with each food or rock element (f \ x , f \ y) or (r \ x , r \ y). If the player collides with some food we delete that piece of food and increase the player's speed. If the player collides with a rock we end the game by resetting the status variable.

Function UpdateRocksandFood()
; draw food and check if eaten

Color 0,255,0

For food

Rect f\x,f\y,10,10

If RectsOverlap(x,y,10,10,f\x,f\y,10,10)


Delete f



; draw rocks and check for roadkill

Color 255,0,255

For r.rock=Each rock

Rect r\x,r\y,10,10

If RectsOverlap(x,y,10,10,r\x,r\y,10,10)




End Function

Oops, one last thing, don't forget to call the UpdateRocksandFood() function from the main loop, just after the UpdatePlayer() call should do nicely.


OK, after playing the game, a few things should become evident.

First, the player should die if they hit the outer wall. We could do this by checking if their position does not collide with the main screen rectangle (0,0,640,480). Try adding the following code to the UpdatePlayer function.

If Not RectsOverlap(x,y,10,10,0,0,630,470) status=0

Secondly, each time the game starts more rocks and food appear. This is because we never delete the food and rocks remaining from the last game. Insert the following code before the code that creates the new food and rocks in the InitGame() function.

For food Delete f Next
For r.rock=Each rock Delete r Next

PostPosted: Tue Feb 17, 2004 5:59 pm   Post subject: (No subject)

Can't get it to work.. Confused It's saying not enough parameters...

can you post the full complete code?
(yes, I put everything in the right order Razz)

PostPosted: Tue Feb 17, 2004 6:28 pm   Post subject: (No subject)

The cursor should go to wherethe error is , im gonna quickly run through it ,
Display posts from previous:   
   Index -> Programming, Visual Basic and Other Basics -> Other Basics
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 3 Posts ]
Jump to: