Tutorial #1.6 - Smashing Ships and Global Bits

We now have asteroids and a ship so lets up the stakes and start to make some life and death situations. Also, we'll create a lives counter, with a 3 strikes and out policy! The listing is as follows:

PROGRAM starwars;

GLOBAL
lives=3;

BEGIN
set_mode(640480);
load_fpg("starwars.fpg");
space_ship();
LOOP
IF (rand(0,100)<25)
asteroid(rand(0,640));
END
FRAME;
END
END

PROCESS space_ship()
BEGIN
graph=3;
x=320;
y=440;
LOOP

IF
(key(_o))
x=x-14;
END

IF (key(_p))
x=x+14;
END
FRAME;
END
END

PROCESS asteroid(x)
BEGIN
graph=2;
y=-40;
REPEAT

IF (collision(TYPE space_ship));
lives=lives-1;
return();
END

IF (lives<1);
exit("Oh Dear - You Died! See README.TXT for more details!",0);
END

y=y+10;
FRAME;
UNTIL
(y>460);
END

Phew! Getting rather big isn't it! Just another 3 tutorials until this is all over :) Firstly, I've got to introduce you to a new concept - I call it the global variable:

GLOBAL
lives=3;

A variable is a number that can go up, down or stay the same. We need variables to hold information within our games. We've already encountered two variables - x and y - however, a global variable is slightly different. A global variable keeps the variables that we'll need throughout the game.

lives=3;

As you can see the global variable here is lives. We set the counter here to three. This is because I want 3 lives in this game. If you are feeling kind then you could always try lives=6, or maybe you want the user to have 999 lives (which would be 'lives=999')!

BEGIN
set_mode(640480);
load_fpg("starwars.fpg");
space_ship();
LOOP
IF (rand(0,100)<25)
asteroid(rand(0,640));
END
FRAME;
END
END

We've seen most of this passage before. However, I want to focus on the new IF line:

IF (rand(0,100)<25)

This line is a very clever way to trick DIV into limiting the number of asteroids on the screen. It's saying "take a random number between 0 and 100 and as long as it's less than 25 then" do whatever. In this case, it'll produce an asteroid for us, however, it could just as easily have been a bullet, a star or whatever else you'd like.

PROCESS asteroid(x)
BEGIN
graph=2;
y=-40;
REPEAT

IF (collision(TYPE space_ship));
lives=lives-1;
return();
END

IF (lives<1);
exit("Oh Dear - You Died! See README.TXT for more details!",0);
END

y=y+10;
FRAME;
UNTIL
(y>460);
END

The asteroid process has changed quite a bit since we last looked at it. We've got two new IF processes to look at. Let's start with the top one;

IF (collision(TYPE space_ship));
lives=lives-1;
return();
END

We start as normal, but then we encounter a new type of command. The COLLISION command. Let's take a greater look at it shall we:

collision(TYPE space_ship)

The collision command is simple really. Collision says "when a collision happens" do whatever. The TYPE part is telling DIV what TYPE of collision it is (ie - what is it hitting?). In this case it's the space ship.

QUESTION Why did you have a collision between a space ship and an asteroid rather than the other way around?

Well, I could have done that, but it would need to have been put in the space_ship() process rather than the asteroid() process. Also, I'd need to change the collision command from collision(TYPE space_ship) to collision(TYPE asteroid)!

lives=lives-1;
return();

Once a collision has been detected, it can do other actions. In this case it does two things. Firstly, it takes a life off. So if you kept with lives=3 in the GLOBAL section then the lives will now be down to 2.
The return() statement is used to make sure that the lives counter does't just hit zero. If we didn't use return() the collision command would just keep on activating as long as you were touching the asteroid. The return command, in effect, tells DIV to 'destroy the asteroid'.

IF (lives<1);
exit("Oh Dear - You Died! See README.TXT for more details!",0);
END

IF (lives<1); is saying, "If the lives counter reaches 0". We could have used IF (lives == 0); but I just didn't feel like it :)

exit("Oh Dear - You Died! See README.TXT for more details!",0);

This is the command you've been waiting for. The command that quits the game to DOS. The exit command works as follows:

exit( type this message when returned to the DOS prompt , run an external file);

The message at the DOS prompt is easy enough. It means that when the program is exited, display this message. However, you may be having problems with the external files part. It's rather complicated so I may cover this in another tutorial.
In the above listing you'll notice that the number 0 is used instead of a file name. This is to indicate that we don't want to use an external file. The number 0 can be used in most cases where you don't want to use a part of a statement.

The rest of the tutorial is pretty much covered. If you haven't done so already, run the tutorial. The game now works! OK, so there aren't any fancy bits and pieces yet, but that'll come in the next few tutorials. Next tutorial we'll be looking at scores and hi-scores. Be seeing you ...

 

This tutorial is ©2000 Fore-go gaming.