Tutorial #1.7 - From an okay game to a good game

Hooray! We've got our basic game running - this calls for a celebration! So, it looks a little lame at the moment, but by the end of this tutorial, It'll be looking fantastic (and a lot like the download at the end of tutorial #3!). The listing follows, but before that may I point out the following:

WARNING!!! READ THIS FIRST!!! - Glad I got your attention. Before running this listing, you need to create an empty text file and rename it to starwars.dat - If you don't do this then the game will throw error messages at you from here to next week!

PROGRAM starwars;

GLOBAL
score=0;
lives=3;
hiscore=0;

BEGIN
load("starwars.dat",&hiscore);
write_int(0,200,470,0,&score);
write_int(0,250,470,0,&lives);
write_int(0,300,470,0,&hiscore);
write(0,50,470,0,"Star Wars");
write(0,400,470,0,"Keys - Left = O Right = P");

set_mode(640480);
load_fpg("starwars.fpg");
put_screen(0,1);
space_ship();

LOOP
IF
(rand(0,100)<25)
asteroid(rand(0,640));
score=score+5;
END
IF (hiscore<score);
hiscore=score;
save("starwars.dat",&hiscore,1);
END
FRAME;
END

END

PROCESS space_ship()
BEGIN
graph=3;
x=320;
y=440;
LOOP
IF (key(_o))
x=x-6;
END

IF (key(_p))
x=x+6;
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

That's as long as this listing is ever going to get!!! If you've read the warning and followed out the instructions, then compiling this will leave you with a project similar to the one you ran in tutorial #3. OK, let's have a look at the new stuff:

GLOBAL
score=0;
lives=3;
hiscore=0;

As you can see, we've got two more global variables to speak of. They hold the score and the hi-score. Both are set to zero at this point as you've scored nothing and a hi-score hasn't been achieved

BEGIN
load("starwars.dat",&hiscore);
write_int(0,200,470,0,&score);
write_int(0,250,470,0,&lives);
write_int(0,300,470,0,&hiscore);
write(0,50,470,0,"Star Wars");
write(0,400,470,0,"Keys - Left = O Right = P");


The write and write_int commands are easy! Let's start by looking at that new command in the top line:

load("starwars.dat",&hiscore);

Ahh! A command that looks familiar :) The load command loads things. Here is the syntax used:

load( name of save game file, variable to load);

Hmm...Interesting. You can now see why I asked you to make that new file now - that file is going to hold all our hi-score data.
In this case we want the variable for the hi-score counter to be loaded into memory, that is what the second half of the command is doing - loading the hi-score data into memory! This isn't going to do anything at the moment, as we have no hi-score in the file, however, soon enough you will!

write_int(0,200,470,0,&score);
write_int(0,250,470,0,&lives);
write_int(0,300,470,0,&hiscore);
write(0,50,470,0,"Star Wars");
write(0,400,470,0,"Keys - Left = O Right = P");

The two commands here are write and write_int! They are both the same except that write deals with letters and write_int deals with integers (or whole numbers as they're known to stupid people like me)!

write( font used, x-location, y-location, text-position, text)

You can find out more about write and write_int in your DIV manual. The font used here is 0. Remember that 0 is a way of telling DIV that we don't want to use this part of the statement. This is because we haven't made a font file. Therefore, we are telling DIV to use the system default font.
The x and y locations are easy enough, just remember that x is left and right and y is up and down!
The text-position is a way of saying how you would like the text displayed. You can have it up, centred or down. In this case we don't want to bother with trivial things like that so we'll just use 0 - the default location.
The text is always placed between speech marks, otherwise DIV gets all bitter and twisted about it. If you are using write_int you cannot use words. You can only use numbers or variables where the numbers are stored. Don't forget to put the '&' before each variable!

LOOP
IF
(rand(0,100)<25)
asteroid(rand(0,640));
score=score+5;
END
IF
(hiscore<score);
hiscore=score;
save("starwars.dat",&hiscore,1);
END
FRAME;
END
END

Let's have a quick look at the new changes and the added IF statement:

IF (rand(0,100)<25)
asteroid(rand(0,640));
score=score+5;
END

As you can see, all I've added here is a score increaser. I've told DIV to add 5 points to my score every time that a new asteroid is thrown down towards me! This is how the scoring works.

 

IF (hiscore<score);
hiscore=score;
save("starwars.dat",&hiscore,1);
END

This part of the listing is saying "IF the hiscore is less than the present score" or if you like, a new hiscore has been achieved [
IF (hiscore<score);] then make the hi-score equal to the score [hiscore=score] and save it to the file starwars.dat [save("starwars.dat",&hiscore,1);]

That seems pretty simple, all except maybe the line save("starwars.dat",&hiscore,1);

save( name of the file to save to , variable to save , save to location);

The first two parts should be easy to understand having gone through the load command earlier. It's the same except this time it is saved rather than loaded. The 'save to location' bit tells DIV where in the file we are saving to. To do this, we must create some kind of table, however in this example game it'd be a little extreme. For console junkies, It's kind of like telling the game to save into an empty save slot on your memory card.

Running the file now will leave you with a simple 'dodge the asteroid' game. Not bad for a few days work (or about 2 hours without writing these tutorials)! Next tutorial I'll be showing you a 'souped up' version of the game and giving you ideas that you can use in your games, till then: be seeing you ...

This tutorial is ©2000 Fore-go gaming.