Tutorial #1.3 - Programming The Game
Rather than going on blind, I decided to program the game to its most basic level and it was rather long. In fact, a little too long for just one tutorial. No, let's be honest here, there is more than enough for 8 tutorials and that excludes sound and special effects (although I do have a nifty hi-score object that can save!)
So, let's just start the game for now and I'll let you download the 'completed so far' game to play with as a kind of 'this is what we're going to achieve over the next few tutorials'. I don't know the size yet, but it shouldn't take too long!
Anyway, here's source number #1:
PROGRAM
starwars;
BEGIN
set_mode(640480);
load_fpg("starwars.fpg");
space_ship();
END
PROCESS space_ship()
BEGIN
graph=3;
x=320;
y=440;
LOOP
FRAME;
END
END
"What the heck???" - don't worry, it'll all become apparent soon...
PROGRAM
starwars;
BEGIN
set_mode(640480);
load_fpg("starwars.fpg");
space_ship();
END
Okay, we know what program is and we can see it's called
starwars. We know that BEGIN starts and END quits the routine. It
is important to remember that END quits the routine and not
the game!!! That command is different and we'll cover it
in a few tutorial's time. The first real problem line is:
set_mode(640480);
What set_mode does is tell DIV what resolution the screen should
be at. In this case I'm making the game a nice and large 640 x
480 (the size of our background remember) so that we can get a
lot on-screen. We could have done higher resolutions, but some
older video cards would be suffering. If your video card is really
old you may have to use 320 x 200 as your screen size (ouch!) but
I doubt this will happen if you're running Windows.
If you want a quick run through of screen sizes then check out page 199 of the DIV manual. Next line goes...
load_fpg("starwars.fpg");
Load litterally loads external things into our game. It can load many things such as palettes and saved games. In this case however, we are loading the fpg file that we made way back in tutorial #2. If you saved the starwars.fpg to a different directory, you'll have to remember to change the path or else DIV with get moody and throw error messages at you during run-time :(
Remember to place the filename between two speechmarks and don't forget to place a ';' after each command. It will still remind you to put one in, but really you should try not to forget...
space_ship();
space_ship() is NOT a command. It acts as a cross-reference for DIV. In other words it's just a little note that says, "go and find whatever and run it". In this case it's space_ship(), however it could just as easily have been rocket(),shuttle() or even apple_and_peach_pie()
This is where the END command kicks in. This tells DIV that once space_ship is finished running, end the routine (and therefore the program)! So is this the end of programing? Not by a long shot! All we've done is tell DIV to put the computer in a resolution of 640x480, load up the graphics pack and look for something in the program called space_ship(). Therefore, if you run the program with just these few commands, you'll end up with an error saying that it can't find space_ship()!
PROCESS
space_ship()
BEGIN
graph=3;
x=320;
y=440;
LOOP
FRAME;
END
END
This is the mystery of the space_ship() request. Let's have a look at the new command process:
PROCESS space_ship()
This is what's called a sub-routine in programing circles. But, rather than call it a sub-routine, DIV decided to call it a PROCESS. All you have to know is that it is a part of the program that can be called upon to be used, but it isn't required all the time for the main program!
Got a headache? Don't worry, a better way of explaining is to say that a PROGRAM is a human and that a PROCESS is something that a human can do. The PROGRAM will contain commands like 'breathe' and 'pump blood around the body', whilst the PROCESSES will contain things like 'walk_a_dog()' and 'cook_a_meal()' - not essential to life, but required for a decent standard of living :)
Anyway, after the BEGIN statement, the next commands that you'll encounter are:
graph=3;
x=320;
y=440;
The graph=3 bit is saying to DIV to use graphic number 3 from the FPG file, which in this case (if you followed tutorial #2 to the letter) will be the space shuttle. If we put the command graph=2 instead, we'd be using the asteroid (which isn't what we want)!
x=320 and y=440 are grid references. "Eek!", we all scream with terror, "MATHEMATICS!". Don't panic! I'm one of the world's worst at mathematics. Just remember that the x-axis runs left and right and that the y-axis runs up and down. These two commands along with the previous graph=3 statement will put our space shuttle just where we want on-screen.
Why 320 and 440? Well they're points on the screen. We start at the top left of the screen with x=0 and y=0. We then go right 330 pixels (out of a possible 640, putting the space shuttle near the middle of the screen) and down 440 pixels (out of a possible 480 pixels, so nearly at the bottom of the screen). That's why x=320 and y=440. After you've run the program you can see what changing these values does :)
LOOP
FRAME;
END
Alright, LOOP and FRAME; are new here and both are required in 99.9% of all games! The LOOP command and the END command go together. The program will keep repeating whatever is in between the LOOP and END statements, until they are told to stop it! In this case the only command between the two is the command FRAME. FRAME litterally means, "Show this on the screen". It tells DIV what should be on screen at any one time. If the loop wasn't there it would only show us a quick flash of the program before quiting. If we didn't have the FRAME command, we couldn't see anything...
END
The final END command. Why two you ask? Well, one of the END commands holds the LOOP movement and the other END command holds the space_ship() request. Therefore it is telling the space_ship() process that it's finished. Then the main program will finish because it's got nothing else to do. However, the program won't end until you quit it (using ALT + X) because we haven't told the LOOP command to stop looping, so it's still putting the space ship on the screen, over and over again...
Getting tired?
Me too! Once you've finished typing in the program, run it. If
all is well then you should see your blue triangle sitting on the
screen. Hooray! Try changing the x=320 and y=440 values to x=100
and y=100 and see what happens. That's it for now, if you want to
download the 'almost finished' version of the game (we'll be at
that stage at around tutorial #6 if not a little bit later) then
click the link and download. Next time we'll add some movement to
this game as well as adding a nice background image. Be seeing
you....
DOWNLOAD 'ALMOST FINISHED' GAME (WINDOWS/DOS)
This tutorial is ©2000 Fore-go gaming.