Computer Science Canada
[Tutorial][Blitz] How To Build A Game
|Author:||shorthair [ Mon Feb 16, 2004 10:09 pm ]|
|Post subject:||[Tutorial][Blitz] How To Build A Game|
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.
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().
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.
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.
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.
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.
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.
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.
|Author:||Cervantes [ Tue Feb 17, 2004 5:59 pm ]|
Can't get it to work.. It's saying not enough parameters...
can you post the full complete code?
(yes, I put everything in the right order )
|Author:||shorthair [ Tue Feb 17, 2004 6:28 pm ]|
The cursor should go to wherethe error is , im gonna quickly run through it ,