While I have been on this board, I have noticed that many â€œnoobsâ€? are struggling to come to grips with the powerful language within DarkBasic and so I have written this simple tutorial based on 2D sprites.
Its not for those of us who know the language but it should help some of the newcomers.
Type of game
This tutorial will focus on making a â€œbare bonesâ€? program for a scrolling shoot em up game.
Please note that when you type in this program, you do NOT put the line numbers in. So where you see something like this:
20 goto flibble
You type in:
The line numbers are only here to help me point out which line we are looking at.
The first thing we must do is to set up the system.
This is done by typing in the following lines:
1 sync on
2 sync rate 30
3 hide mouse
Line 1 sets the program up so that you, the programmer, have to tell the system when to refresh (or re-draw) the screen and update everything. Darkbasic does do this automatically by using SYNC OFF but in a program like this, it causes the game to run slowly. So it is always better to control the refresh yourself.
Line 2 sets the speed that the program will work at in Frames Per Second (FPS). If you have an old system that runs slowly you can change this setting to speed up your game. SYNC RATE 0 will set the program to run at the fastest the computer can handle. If you need to change it, try setting it to SYNC RATE 60 before you set it to zero.
Line 3 simply hides the mouse as we donâ€™t want a mouse pointer on the screen when your playing.
The next thing to do is to load in the graphics for the game.
Darkbasic has several ways to do this, and there are also several different ways using programming to load the graphics in, but I have chosen to keep it simple.
Type in the following:
4 load image "yourship.bmp",1
5 load image "alienship.bmp",2
6 load image "orb.bmp",3
7 load image "greenlazer.bmp",4
8 load image "yellowlazer.bmp",5
9 load image "exp1.bmp",6
10 load image "exp2.bmp",7
11 load image "exp3.bmp",8
12 load image "exp4.bmp",9
13 load image "exp5.bmp",10
14 load image "exp6.bmp",11
15 load image "stars.bmp",50
16 load image "title.bmp",9999
What we are doing is thisâ€¦â€¦.
Line 4 loads the image of our ship, called â€œyourship.bmpâ€? in to an empty bank in memory, which is bank 1 in this case.
All the other lines are doing exactly the same thing but with different graphics and different banks.
Ok, so now your program sets itself up and loads in all the graphics. Now you are going to make a star field that moves from right to left to give the impression of movement.
In games such as this there are many ways to achieve the effect of scrolling stars. Some people use sprites for this, while others use animations or pre-calculated data. We are going to use a simple and fast routine that does not take up pages of programming to do, and can be controlled using a single variable.
(In fact, my daughter, Dragon3, used a routine that was similar to this in her Evolution Intro).
You start off by making a 3d object.
17 MAKE OBJECT PLAIN 4,600,56
Then you position it on the screen.
18 POSITION OBJECT 4,0,-4,80
Next you use the graphic held in bank 50 (which is the stars.bmp picture) and â€œtextureâ€? the blank 3d object with it. In other words your Stars.bmp picture is stretched out over the 3D object so that you see the stars.
19 TEXTURE OBJECT 4,50
Finally you lock the object on to the screen so that it is positioned correctly.
(If you want to know what would happen if you did not do this, then REM out Line 19 and 20 and later on when you run the program you will be able to see what happens.)
20 LOCK OBJECT ON 4
Ok, so now just colour the screen in black and you have your stars in space.
21 color backdrop rgb(0,0,0)
If you want to see the program so far, add the following lines:
Okâ€¦. Well done.
However, lets set up some variables now that will help us control the program later.
These variables set the score, lives, level and also if you have fired your weapon (pf=Pressed Fire). They also store the position of your ship (ysx=Your Ships Xâ€¦. ysy=Your Ships Y). This will be used for moving your ship sprite when you press the up or down arrow key.
The next thing we do is to put your ship on the screen at the coordinated specified by the ysx and ysy variables.
24 sprite 1,ysx,ysy,1
We then set up the position of where the lazer beam is going to shoot from. This is done by these two lines:
25 lx=SPRITE X(1)+20
26 ly=SPRITE Y(1)+10
The lines set lx (lx=lazers X: ly=lazers Y) and ly to where the top left point is on your ships sprite and then it moves it 20 pixels forward and 10 pixels downwardâ€¦Which put the firing point roughly at the front middle of your ship. Later on, when you have typed in this listing, try altering the +20 and the +10 to see what it does.
Next we put the title on the screen, which is stored in sprite 9999. I used a high number to make it clear within the listing what it was.
28 sprite 9999,10,10,9999
Rightâ€¦. After line 29 you need to type in this:
30 set cursor 200,400: Print "SCORE:";score; " LEVEL:";level;" LIVES:";lives
This will set up a simple print command to display score, level and lives. We donâ€™t use this within this tutorial, but hopefully some of you new programmers will take this code from the tutorial and will add to it and make it in to a mini game. If you want to use a score, level and lives counter then you would only need to ADD an amount to the â€œscoreâ€? variable every time you shot an alien and then your main loop would just display the updated score via line 30 when it loops. You could of cause remove or replace line 30 should you wish toâ€¦.It is totally up to you.
Next we get the players ship to respond to an input from the player.
To keep it simple, your ship can only move UP and DOWN and FIRE.
The routine to move the ship UP is this:
31 if UPKEY()=1 and ysy>100
32 dec ysy,10
This small routine is basically saying: â€œIf the user presses the UPKEY (arrow key) AND the players ship is LOWER that 100 pixels from the top of the screen, then take away 1 from the Y position of the shipâ€¦But do it 10 times, so your really taking -10 from itâ€?
The routine for moving the ship down is much the same except it checks to make sure that the ship if higher that 350 pixels.
34 if DOWNKEY()=1 and ysy<350
35 inc ysy,10
The fire a lazer routine checks to see if you are pressing the â€œZâ€? key and if you are AND if the variable pf=0 (in other words, you have NOT fired a lazer) then it jumps (gosub) to the shoot a lazer routine and then sets pf to 1 to make sure that the routine cannot be used again until the lazer you have just fired has been removed from the screen.
37 if inkey$()="z" and pf=0 then gosub shoot:pf=1
We then put the ship on the screen again:
38 sprite 1,ysx,ysy,1
And jump to 2 routines that move the stars and also the lazer:
39 gosub moveall
40 gosub movestars
To move the stars is simplicity itself. All you have to do is to make the star pattern scroll across the 3D object. To do this use the following:
42 SCROLL OBJECT TEXTURE 4,0.01,0.0
If you want to speed up or slow down the stars just alter the 0.01 to another amountâ€¦. You will be able to have them any speed at all.
Shooting the lazer is easy too. Just put the lazer (which is sprite 2) on the screen and also play a sound to let everyone know that its been fired. Like this:
45 sprite 2,lx,ly,4
46 PLAY SOUND 1
To move the lazer we first check to make sure that it is on the screen by the command : â€œif SPRITE EXIST(2)=1â€? (Line 49). If the sprite was NOT on screen, then this would return a â€œ0â€?.
Provided that it IS on screen, the next thing to do is to make sure its not gone off the right side of the screen by checking that its X coordinate is LESS than 640 (line 50) (the screen is a 640 by 480 size by default).
If it is less that 640 then we move the lazer to the right by 45 pixels (line 51), which is the same as the length of the lazer.
49 if SPRITE EXIST(2)=1
50 If sprite X(2)<640
52 sprite 2,lx,ly,4
What is the sprite IS GREATER than 640? Well we donâ€™t move it any more and we reset the pf variable to 0 to make sure we can re-fire the lazer.
Here is that routineâ€¦.See if you can figure out yourself how this little part works.
55 if SPRITE EXIST(2)=1
56 If sprite X(2)>640
58 lx=SPRITE X(1)+20
59 ly=SPRITE Y(1)+10
Ok so there you have it. A bare bones for a side shoot em up game. A lot simpler than you thought eh?
Here is the full listing for you to copy and paste in to DarkBasic.
I have included a shoot the alien and blow it up routine too..But remember... This tutorial is NOT a finished program. Its now up to you to mess about with it and to make it in to a mini game.
Learn from it and when you have used it ( and maybe made something from it) post it here so that we all can see it. Even if you donâ€™t use it at all, please post and tell me if this kind of tutorial helped you.
Also, go HERE...
And download the graphics you will need for this tutorial. Just unpack them into the SAME directory you save this program in and everything will work fine.
sync rate 30
color backdrop 0
load image "yourship.bmp",1
load image "alienship.bmp",2
load image "orb.bmp",3
load image "greenlazer.bmp",4
load image "yellowlazer.bmp",5
load image "exp1.bmp",6
load image "exp2.bmp",7
load image "exp3.bmp",8
load image "exp4.bmp",9
load image "exp5.bmp",10
load image "exp6.bmp",11
load image "stars.bmp",50
load image "title.bmp",9999
LOAD SOUND "phaser03.wav",1
LOAD SOUND "boom2.wav",2
MAKE OBJECT PLAIN 4,600,56
ghost object on 4
POSITION OBJECT 4,0,-4,80
TEXTURE OBJECT 4,50
LOCK OBJECT ON 4
color backdrop rgb(0,0,0)
set cursor 200,400: Print "SCORE:";score; " LEVEL:";level;" LIVES:";lives
if UPKEY()=1 and ysy>100
if DOWNKEY()=1 and ysy<350
if inkey$()="z" and pf=0 then gosub shoot:pf=1
if SPRITE EXIST(2)=1
if SPRITE HIT(2,3)=1 then gosub blowitup
SCROLL OBJECT TEXTURE 4,0.01,0.0
PLAY SOUND 1
if SPRITE EXIST(2)=1
If sprite X(2)<640
if SPRITE EXIST(2)=1
If sprite X(2)>640
delete sprite 2
PLAY SOUND 2
for t=6 to 11
delete sprite 3