Blitz2D Newbies: - How to write a shoot 'em up -
by Phoenix

Hello! As you probably already understood, this tutorial will teach you how to make a simple shoot 'em up. There are some requirements though.

  • You have to know how to use variables, IF...THEN statements, types and functions
  • In other words, basic knowledge of blitz
  • You need any version of Blitz

Now are you ready to begin? Ok lets start! I will show the source and then tell you how it works.


This first part, we set the title of the window to "Shoot 'em up Tutorial" and if we are in windowed mode, a "Are you sure?" dialog will pop up if you click X. Then we make all random numbers random with SeedRnd MilliSecs(). After that we go into the resolution 640,480 and sets the current buffer to the backbuffer to avoid flickering.


In this part, we create types for the enemies and the bullets.
  • X = The current x position of the enemy/bullet
  • Y = The current y position of the enemy/bullet
  • shoot = This determines if the enmy is going to shoot
  • dir = The direction of the enemy(0 = left,1 = right)
  • team = The owner of the bullet(0 = player,1 = computer)
We also create constants to hold the scancodes for our keys(Up,Down,Left,Right,Space).


Just as the comment says, here is the main variables.
  • X = Player's x position
  • Y = Player's y position
  • Stary = The background stars are scrolling, this is the y position of the stars.
  • NumStars = How many stars is it?
  • Speed# = How fast is the player?
  • NumEnemies = How many enemies are alive?
After that, we create image handles for our images, the stars, the player and the enemies. But hey! What about CreateImages()? Here it comes!


Alot of source there, but there is not much to explain here, it sets the buffer to the imagebuffer so that we can draw our images and then they are being drawn. Then resets the buffer to the backbuffer again.


The main game loop! That was a short one! But it has large functions to do the job. Before the loop, ive put a delay command, because when you go into graphics mode, the screen gets black for about two seconds, so we wait until its done! Look below to see the functions, because this loop(Without the functions) only updates the screen.


Now you will probably say something like this: - Please help me! Relax, I will explain it. First of all, we check to see if any of the arrow keys are being held down, if so, then move the player to the corresponding direction(Using our speed variable that we declared in the beginning). We prevent the player to go offscreen after that. And now for the shooting part yay! If the player presses space we create a new bullet type, and make it start at the player's position. We also tell the program that the owner of the bullet is the player with "b\team = 0". We go into a For...Each loop to update all enemies. First of all we draw them, then we set shoot to a random number between 0 and 3000 and immedeately checks to see if it became 0, if so we fire an enemy bullet! In the next lines, we move the enemies to the left if dir is 1 else we move them to the right. Now we start another For...Each loop to update the bullets! We check who is the owner of the bullet and set the color to the right color, so that you can see diffrence between your bullets and the computer's bullets. We check the owner again and move the bullet to the right direction. Finally we draw a circle for the bullet, but the loop still continues, because we have to check the collisions too right? We enter a "double-loop" as I call it. One bullet loop and one enemy loop. First of all we check the team(We don't want to shoot ourselves!), if the bullet collides with the enemy we delete the enemy and the bullet, else, we print "GAME OVER!!!" and ends the program. "StarY = StarY + 1" Scroll the stars, are there anything more to say? This is a game that lasts for ever, so if there are no enemies left create some new ones using CreateEnemies().


Create 48 enemies and place them on the screen. We go through a nested for...next loop as it is called, place the enemies on the screen as I said and randomizes which direction it should go, left or right. For each enemy that is createted, we update the NumEnemies(Number of Eenemies alive). Now there are only one thing remaining, the RenderGame(), it is the shortest part of the game so there is nothing to be afraid of.


We tile the starfield, and you know that the StarY variable always increases, so the starfield scrolls! We simply draw the player after that. Now i've explained everything and the full sourcecode comes here:


Thats all folks! If you have any questions then mail me at MSvanis@hotmail.com Bye!


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


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