Tutorial #1.5 - Asteroid Attacking...
All is not yet right for our Star Warrior - he's got nothing to do. Let's see if we cannot change that in some way :) Here's todays listing complete with commands for some asteroids.
PROGRAM
starwars;
BEGIN
set_mode(640480);
load_fpg("starwars.fpg");
space_ship();
LOOP
asteroid(rand(0,640));
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
y=y+10;
FRAME;
UNTIL (y>460);
END
Let's start by looking at the modified main program and in particular why we've got a new command thrown in between the LOOP and END.
PROGRAM
starwars;
BEGIN
set_mode(640480);
load_fpg("starwars.fpg");
space_ship();
LOOP
asteroid(rand(0,640));
FRAME;
END
END
Well, everything is looking OK here except for that new
command. For a start, asteroid isn't a command. It's one of those
call markers again, you know, like space_ship() was! The only
thing different is the actual command in the line. That
is the rand part!
rand(0,640)
is the actual command, it's just been sandwiched between the () part of asteroid() - do you see? I'll explain why it's in there when we get to the PROCESS command, but first I'll explain rand in more detail.
rand(lowest value, highest value)
rand chooses a random number. We could use the command on it's own and it'd churn out random values until we were giddy, but it's a waste! It'd create random numbers that would be unimportant to us. We are looking for a random screen point in this case, that means that any value below 0 (-1, -2, -3, ...I could go on forever) or above 640 (641, 642, 643, ...you get the message) would be unseen - D'oh! So, rand lets us put in a range of values that it can work with. In this case between 0 and 640.
This says to the main game to generate a single spot between 0 and 640 from which to throw us an asteroid. We get more than one asteroid on screen because this is done in a loop, so it keeps on choosing a random number on the x-axis from where to throw an asteroid and throwing them at us.
PROCESS
asteroid(x)
BEGIN
graph=2;
y=-40;
REPEAT
y=y+10;
FRAME;
UNTIL (y>460);
END
This is the asteroid process. Most of it looks similar, so we'll skip a lot of it. The important commands (the new commands) we'll go into some depth for, starting with the answer to the whole asteroid(rand(0,640)) thing...
PROCESS asteroid(x)
The asteroid process will run with the value of x each time. The x-value is given by the statement rand(0,640). It's kind of hard to explain. It could be written differently, but this is a new and interesting way, plus it will make the program run smoother later on.
We could have just used asteroid() all the way through and then change the last process to:
PROCESS
asteroid()
BEGIN
x=rand(0,640);
graph=2;
y=-40;
REPEAT
y=y+10;
FRAME;
UNTIL (y>460);
END
If you like you can give it a try and it'll work the same, but make sure that you change it back afterwards, or the next few tutorials won't work properly! Anyway, back to the program. We know what graph=2 means so the next line to look at is:
y=-40
This is telling the program that the asteroids are to be thrown at us from off the visible screen.
REPEAT
y=y+10;
FRAME;
UNTIL (y>460);
The REPEAT...UNTIL statement does just that - repeats
something until an event occurs. It could be anything from a
mouse movement to a keypress. In this case the command that is
repeated is y=y+10 (or move the asteroids down the y-axis 10
pixels at a time) and you are to do that until y>460 (or until
the asteroids cannot be seen).
QUESTION
Why do we use REPEAT...UNTIL instead of a LOOP...END
command?
The answer is to do with processing power. If we did a normal
loop here we'd end up with thousands of asteroids that just
continue to move down the y-axis. If left for long enough, the
computer would freeze up with all the asteroids that have been
thrown at us. This would leave us (and anyone else playing the
game) with a hung computer - nasty! Instead, we just show the
asteroids until they leave the visible area of the screen - much
easier on the processor :)
END
As usual, we have to END any process that we start or we'll get errors thrown at us. Run the listing and see what you get. If all is well then you should get loads of asteroids flying towards your ship. OK, so nothing happens if they smash into your craft, but that's the next tutorial... Be seeing you...
This tutorial is ©2000 Fore-go gaming.