This is a classic arcade game and a good example of a game that uses realistic physics.
The goal of the game is to land a space
craft on a landing pad on the moon. The landing must be accurate and gentle -- the
space craft is destroyed if you hit part of the moon that is not a landing pad, or
if you land with too much downward velocity or when the ship is tipped at too
much of an angle.
Draw the moon surface on the Stage's backdrop using one color for the terrain
(brown in the picture above) and one color for the landing pads (pink in
the picture above). Normally it's best to draw things in vector mode, but
in this particular case it might be easier to stick with bitmap mode.
You can simulate physics by thinking in terms of velocity: each time through the
main "forever" loop you move the ship by its velocity. You can change the velocity
by using the ships thruster, and also gravity is constantly operating, making the
ship's downward velocity more negative.
You can think of the velocity in terms of two variables -- "y velocity" and
"x velocity". "y velocity" tells you how fast the ship is rising (if "y velocity"
is positive) or falling (if "y velocity" is negative). "x velocity" tells you
how fast the ship is moving to the right (if "x velocity" is positive) or to the
left (if "x velocity" is negative). Gravity is constantly affect "y velocity".
So in your main "forever" loop, you'll have the three lines shown on the left
side of the picture below. The
first two lines move the ship at it's current x and y velocities, and the
third line applies gravity to the y velocity. (You can play with other values
that -0.04 to affect how strongly gravity pulls the ship downward.)
You'll want to use a some "if key ___ pressed" blocks to check if the player is
holding down the left arrow, the right arrow, or the space bar. If the left or
right arrow is pressed, the ship rotates. If the space bar is pressed, the ship
thrusts. Remember that the thrusting does not "move" the ship directly, but
rather affects the ship's x velocity and y velocity.
The two lines on the
right side of the picture above show how you can change the two velocity values
based on the angle the ship is facing. For instance, if the ship is facing
straight upright, when you thrust all the "thrust power" increases the ship's
y velocity upward, but does not affect the ship's side-to-side x velocity.
However, when the ship is turned at a 45 degree angle to the right, half of
the "thrust
power" pushes the ship upward (increases the y velocity) and half of the
"thrust power" pushes the ship to the right (increases the x velocity). At
other angles (for instance, when the ship is turns just slightly to the right),
different distributions of the "thrust power" between the y velocity and
x velocity are needed. The way to figure out this distribution is to use
the sin() and cos() functions as shown in the code above. Normally you don't
learn about sine and cosine until you study trigonometry in about 10th grade.
But you can look up sine and cosine on Wikipedia and/or talk to a mentor
to understand how these two lines of code work.
In the "rules of thumb" above, it says to try not to use "if touching color".
However, in this particular game, it is especially convenient to use
"if touching color" to see if the ship is touching a landing pad (perhaps
pink, as in the picture above), or the moon surface (brown in the picture
above). In general, it is best to check coordinates instead of using
"if touching" or "if touching color", but sometimes, as in this game, it
just makes so much sense to use them that it's smart to break this "rule".
The picture above shows that when the ship is thrusting (when the player
is holding down the space bar), some fire is seen coming out of the
bottom of the ship. One problem with using "if touching color" is that
if the fire is poking out of the bottom of the ship as it's landing,
and if the fire is part of the ship's costume, the fire might be
touching brown, and you'll think the ship as crashed, but really it should
only crash if the main part of the ship is touching brown. One idea
is to use "if color touching color", but unfortunately we have found that
"if color touching color" doesn't always work because of a bug in
Scratch. A way around this is to make the fire it's own sprite and to have
the fire "follow" the ship and show itself when the player is thrusting.
If the ship touches the moon surface or doesn't land gently, make it crash.
Remember that landing gently means not falling too fast or being at too
much of an angle when you hit the landing pad.
Here are some additional challenges for this game:
- Start the ship with a certain amount of fuel. As the ship thrusts it uses up fuel.
If you are out of fuel you can't thrust anymore.
- Make a "fuel refilling station" somewhere. If the ship touches the fuel
refilling station it disappears and sets the ship's feul back up to the
starting value.
- Make it so that you get more points for landing on a landing pad that is more
difficult to reach. To do this, you'll have to do more than just check "is touching
color" pink. You'll need to use coordinate comparisons to figure out which landing
pad the player landed on.
- Make an alien ship fly around so that in addition to having to land you also
have to avoid crashing into the alien.
- Make multiple levels. When you complete a level, go do the next level by
changing the Stage's backdrop.