Tutorial
#1.4 - Movement Of The Spacecraft
So, now we've got our spacecraft on screen, we can go on to make
it move. There are several ways in which it can move. They
include joystick, mouse and keyboard. We'll concentrate on the
keyboard for now, but I may include a tutorial soon about
joysticks and the like. Anyway, here's the new listing:
PROGRAM
starwars;
BEGIN
set_mode(640480);
load_fpg("starwars.fpg");
space_ship();
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
As you can see, it hasn't much changed from the last tutorial except for between the LOOP and FRAME; statements in the process space_ship(). This is what will create our movement!
IF
(key(_o))
x=x-6;
END
The first new line contains an IF statement. IF statements will become second nature to you soon, so don't worry if you don't quite understand them now. An IF statement contains commands that will happen IF something defined by the user takes place! In this case the line IF (key(_o)) is saying that if the key o (that's the letter, not a zero!!) is pressed do the following commands. Another IF statement coming up a little later
x=x-6;
OK, this may take a little thought, but it does make sense - honest! If you remember from the last tutorial x is equal to 320 at the moment. Now, when the 'o' key is pressed we are to take that value (x) and subtract 6 from it. This will leave us with 320 - 6, which is equal to 314. DIV then replots you at the new location as the x value has changed. Hard to make sense of it? Yeah, I know how you feel - I was like that too in the hay-day of BASIC - don't worry, it's a long story.
END
If you remember that last tutorial, we had to end the LOOP function by putting in END. The same thing has to happen with the IF command. As you can tell by looking at the code so far, there's going to be a lot of END commands dotted around the place. That's why layout is important. It makes no difference to how the program works - just how easy it is to see what's going on (or in the case of an error, what's not)!
IF
(key(_p))
x=x+6;
END
As you can see the other piece of new code looks
similar, however there are two key differences. Can you spot them
yet?
IF
(key(_p))
The key has changed from 'o' to 'p' - this means that IF the
letter 'p' is pressed then do the following set of commands,
which in this case is another of those movement things.
x=x+6;
This time, we
take the value of x (remember that at the start of the game it's
320) and then add 6 to it IF the letter 'p' is pressed.
Therefore, if we press the key 'p' twice we'll have to add 6 to
the 320 (which gives us 326) and then add another 6 (which leaves
us at the position of 332)!
QUESTION - Why not put in x=x-1 or x=x+1, that
makes better sense doesn't it? True, and it will work! However,
moving the space ship 1 pixel left or right on screen each time
is slow and impractical.
Now, give the program a quick save and run. Try pressing the 'o' and 'p' keys. They should move your little space ship left and right. Why not try experimenting with the x=x+6 and x=x-6 values. Why not change them to x=x+1 and x=x-1 or (for a laugh) x=x+14 and x=x-14. Seeing that the tutorial was so long last time, I'll leave you now. Tutorial 5 will see us playing with some asteroids. Be seeing you...
This tutorial is ©2000 Fore-go gaming.