Blitz2D Newbies: Writing A Simple Game In Blitz Basic
by Alex Jones

Pong Source Code By Mattthew "Cloudnine" Whitworth

Writing a simple game in Blitz Basic:

Making your first game is a massive hurdle to jump in Blitz Basic. Although Blitz is really powerful it is important to start with a simple game. And there is hardly any game that is simpler that Pong.

In this tutorial I'll show you how to make a simple game of Pong. Before starting remember to add comments to your work often by pressing ; and typing in your comment after.

Also you'll need some graphics:

Make in MS Paint

White (blank picture) size 16,16 pixels (press Ctrl+E make sure the pixels button is on and type 16 and 16 in the boxes) saved as ball.bmp

A white (blank picture) size 16,64 pixels (press Ctrl+E make sure the pixels button is on and type 16 in the width box and 64 in the height box) saved as bat.bmp

Or you can download them (with the source from this tutorial) here:

http://mysite.freeserve.com/blitzbasement/pongtutorial.zip

Okay, lets go!

First we need to say how big the screen size will be. The Command we need is "Graphics" followed by pixels across then down. The best ones are 640,480 and 800,600. We'll use 640 by 480 pixels so:


We'll also need to let Windows know the name of our program using the "Apptitle" command.


The Pong bit can be replaced with "Billy's First Game","Testing" or anything,

We'll need to draw our stuff in the background to avoid the screen flickering about 50 times a second using the SetBuffer and Backbuffer commands. To swap the buffers we'll need to use flip a bit later on.


Next we need to load in some stuff from outside Blitz like pictures. Each picture should be given it's own name to be identified later on in the prog.


The Ball,Player and Opp bits are the names,the "Loadimage" is the command and the ("ball.bmp") and ("bat.bmp") are the pictures

We need some settings for the start of the game like position, speed etc. These settings need to have the same names as the corresponding picture. Some new settings have been created as well like speed and score.


There needs to be a way of ending the game and the Esc. key is probably the best. It's code is 1 (easy to remember) so it's code will be


Meaning if the Esc. isn't pressed do the following. At the end we'll need to add a Wend to close this command.

We next need to tell what we want to happen when different keys are pressed. Key 8 on the number pad (Up arrow) is 72 and Key 2 (Down) is 80. The commands we set up earlier will be used as a base ready to add or subtract from.


Also we need a way to keep it from going off screen (i.e. past 0) so if the Player_y is less than zero change it back to zero:


And finally we need to close the If command


We now need to do something similar to move down.


We need to tell it if the images hit each other to bounce back the ball.


The "SpeedX=SpeedX-(2*SpeedX)" means use the new speed is the old speed takeaway (to make it bounce off) two times the old one.

The same now needs to be done with the opponents:


If the ball goes behind the opponent the player gets 10 points so using the same method of new=old+(whatever):


If the ball goes past the other side of the screen then we'll need to give the opponent 10 points.


To clear the screen after every move (to make sure the old graphics are removed) Simply type


As well as stopping the ball going off the screen we'll need to stop the bat going off screen

Using the if (name) is more than (number) then (name) is (number) commands


To make sure the computer doesn't end up hitting every ball and to make it a bit more human like using the "Random" command we'll make a 1 in 3 chance of hitting the ball


If the ball is lower than the bat


Is the random number one? If so miss the ball


Otherwise move the bat towards the ball


And close the if command


Do the same when the ball is higher than the bat


Now we can actually tell Blitz to draw the image where the above code tells it to


And should the ball go past either bat then reset it to the starting position and speed


To make the ball move we need to tell it to move at the Speed


And then draw the players and opponents bats


Finally print the scores at 300 and 340 pixels across and 0 pixels down.


Now to show what's been happening in the background type


We'll need to end the while command we did earlier for the Esc. key and end the code


Then by pressing F5 to run the program you should have a working pong game. When you are familiar with these commands try and modify/customize it. You could add a background,music,sound effects,title screen,competition mode (this version runs for ever until Esc. is pressed) 2 player battle, winner screens, menus, better graphics... the list goes on and on.

So when you've finished this tutorial think about what you could do to improve it, and experiment with it.

Good Luck!

-Alex Jones

This Tutorial was originally from BlitzBasement.


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


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