Blitz2D Newbies: Creating A Platform Game
by Sam Schoenholz

Creating A Platformer Game




Preface


This tutorial will take you through all of the steps needed to create your own platformer game. This tutorial also assumes a basic knoledge of blitz, to the extent of arrays, a basic knoledge of types and a good grasp of 2D graphics. If you do not know the above you will still be able to follow the tutorial however it may take more time and effort. For those of you reading this who are reletively advanced in blitz i will mark in italics the parts that are for newer users. Overall it took me 1 and a half hours to write the code for the tutorial and another 30 minuits to make the demo level that comes along with it. If their is anything you dont understand either post it in the boards or e-mail me at schoenhs at yahoo dot com. Finaly, this is my first tutorial so if theirs anything i should change or do better for next time please tell me :D.

The Features of the Game


The game that you will make will not be extremely complex but it will be easily expandible and have a couple of cool features, these are:
Loadable Levels
Scrolling(The Level can be bigger than the screen)
Full Collision Detection
One Simple Enemy
Two Layers

Throughout this tutorial i wil go through the code incrementaly showing you how each bit works etc...

The File Format


This section of the tutorial is going to cover the file format that i will be using to make the game, this file format has sort of evolved out of necesitty but it works quite well and can handle anything i would need in this example.
the file format is as follows
the first two variables will contain the size of the level, after this all of the variables are the tiles that the level will use. for example: if the number is a 5 then the program will draw whichever tile corrosponds to a five. there are some exceptions to this rule however, there will be the escape codes -1 -2 -3 -4 -5, -1 indicates the end of a line, -2 indicates the end of a section, -3 indicates the starting point for the player, -4 indicates the ending point for the player and -5 indicates the creation point of an enemy.
here is an example level:



I would seriously suggest making a level editor... it is very difficult to create levels, i may release another tutorial on how to make a level editor for this game but regardless you should.

The Game


The Main Game

Finally... here is the actual code for the game :)
The initialization for the game is relatively simple, it just defines some global variables to use sets up the graphics etc..



The first two lines of code just turn on graphics and turn on backbuffering. the third and fourth line are Escape codes, they are not much use in this game however if you were to expand upon it, it is good programing practice to use them ;)
Next is our defanition of an enemy, Our enemy is very stupid and so all that he can do is move in a direction until he hits something and turns around. therefore in our type the field hEnemy stores the image of our enemy. The fields x and y store the position of our enemy and finaly the variable dir stores the direction he is traveling on the X Axis.
The variable MapGFX is the variable which holds all of our tiles for our map, this is one of the most important variables in the game. The variable PlayerGFX is all of our players animations etc... Most of the other variables are self explanitory however a few need a bit of clarification, the variable OffsetX and NewOffsetX, these variables are used to make the level scroll, the point of having to is so that we can make smooth scrolling between them as i will show you later on. Other variables of interest are PlayerSides, PlayerUp and PlayerDown; these variables are used for collision. The MaskImage command is used to change the mask color on our level tiles from black to pink the command Dim Map(2,0,0) creates enough space for our map, the 2 at the begining is so we have enough space to hold both layers and the other two are zero because we we redimensionalize them once we have the size of our level. Finaly the command LoadLevel("test.lvl") calls the custom function to load our level, il go over this more when we get to the function calls.

The next section of code is the actual main section, the place in which all the main stuff takes place, so here it is then, like before, il go through it with you.



The above code is the code to scroll, early on i had thought that it would be pritty cool if, while the sceen was scrolling everything else was still moving, however in practice it wasnt fun so i decided to make everything freeze during the scroll. OK so the first two lines say that if the players position is more that 610 on the screen or less than 30 on the screen then add or subtract 100 from NewOffsetX. The next section of code is where it actually scrolls it, i am only going to analyse the first while loop because they are both almoast exactly the same just for different directions. So the first while loop is basicly saying that while NewOffsetX is not equel OffsetX then add 2 to OffsetX, then it applies the code Origin -OffsetX,0 to Apply the scrolling, it then draws everything, flips the buffer and then loops. This loop is basicly just the main loop without the user input.
this next section is the rest of the main loop, it is very important and handles everything from movement to user input to collision, so, without further adue here it is,



Basicly this code starts off with if PlayerCollisions() = True the PlayerCollisions is a function i made which checks for all possible collisions with the player, it will, as you will see later on will return true if the Player has reached the End point of the game if he has then it makes the string finish = to "you win". The next couple lines are quite simple, basicly if you press left or right and there are no collisions to either side of your player then it moves the player(right if you pressed the right key and left if you pressed the left key) it then increments the animation and if the animation has gone over the number of frames it loops it. The rest of the code is just jumping code, the first if statement is if the player has collided with the ground and the player was moving down, then it stops the player. The next if statement says that if the upkey has been clicked and the player is on the ground(prevents double jumps) then PlayerYVel = -6, and in concquence the player jumps. The Second from last if statement is basicly if the player is not on the ground or if the player is going up then it adds PlayerYVel to PlayerY(making him jump/fall) and finaly ading PlayerYVel + Gravity applying gravity to PlayerYVel. The final if statement is very simple, it calls a function called UpdateEnemies which returns false if your character hit an enemy otherwize it returns true, if the player has hit an enemy then it sets the variable finish$ to "You lose and exits the loop. The next couple lines just call functions to draw the world, the enemies and the player.

Here is Clean up phase, it is not very long and it is not very complicated so i will go over it quite quicly.



In three lines in this code it deletes the MapGFX, the PlayerGFX and the EndGFX. in the last four lines of code it loops through all of the enemies and deletes the image and then deletes the type itself.


The Functions



Here is the section where i show you how all of the functions work, this section is slightly more complex than the last one and if you did not understand it you should wait before continueing. Anyway, here is the first function and the longest it is the function that actually loads the level.



This code is very well comented but i will go through it aswell. The begining of the code just Loading the file and if it failed return EFAIL, the next two lines read two integers from the file and assign them to MapSizeX and MapSizeY. The next part is very important, it loops until it encounters a -2 (the escape code remember) it then begins another loop looking for eithter -1 or -2, it then, reads from the file and if the returned numbers are within the number of tiles we have then it inserts it into map at the correct place. It then increments the x variable and ends the loop.
The Second Section is exactly the same as the first except it utilizes some of the escape codes, so if r=-3(our starting point escape code) then it sets the player position and then sets r back to 0 so that it does not exit the loop. the escape code -4 does the same thing but with the end position and finaly the -5 one creates a new enemy, loads the enemies image, sets his x and y position but then reads another integer to get his direction. once it is finished parcing the file it closes it and returns.

The next section is the drawing of the Map/Enemies, these are not very complex functions so i wont spend too long on them. Well here ya go.



The DrawLevel function simply loops through from 0 to the map's size drawing each tile with its correct frame(retrieved from Map) and thats about all it does, at the end of the function it draws the End graphic and its proper position. The DrawEnemies() function just loops through all of the enemies and draws them it to is not too complex a function...

The Next Section is more complex although not TOO complex, it deals with collisions between the Player and the World.



At the begining of this function it resets all of the collisions from the previous frame, it then loops through each tile on the map and, if the tile is solid, it checks 4 image collides with it, one with the PlayerX+1 to see if there is anything to the right of it, then PlayerX-1 to see if there is anything on the left of it, then it does the same but up and down, PlayerY+1 for down and PlayerY-1 for up, during this time it sets the variables aproprietly. the end of this function checks the Player and the end position and if they collided it returns true but if they have not colided it returns false.

The final section to this part of the tutorial is are the collisions between the Enemy and the World and the Enemy and the Player.



This section of code opporates in much the same way as the last section, first it loops through all the tiles BUT then it loops through all of the enemies checking each one with the map. At the end of this function it checks each of the enemies against the player, if they have collided it returns false and if they havent it returns true.


Finally before the conclusion here is all of the Code in one place





Conclusion




I hope you have learnt something about how to make a platform game and i hope you have found this usefull in any way at all. All of the graphics for this game were either from blitz demos or from http://home.cogeco.ca/~neilroy/free_gfx_1.html Anyway, if people find this tutorial usefull then i will write a level editor for it and a tutorial for how to write a level editor otherwise i will just release the source of the level editor iv yet to write :P. Well Chao. P.S. if you want the zip file with all the gfx go to http://littleboy550.tripod.com/platfomer_tutorial.zip
Or to discuss this article go here:
http://www.blitzcoder.com/cgi-bin/ubb-cgi/postdisplay.cgi?forum=Forum7&topic=000065
Any Comments Appreciated


For a printable copy of this article, please click HERE.


This site is Copyright© 2000-2004, BlitzCoder. All rights reserved.