This week we decided to create a Lunar Lander game that demonstrated the effects of Gravity.
As always we started with a plan:
- A Spaceship Sprite with 4 Costumes
- No power
- Up Rocket firing
- Left Rocket firing
- Right Rocket firing
- Buttons to control the Space Ship
- Stage which should be a picture of the moon.
- A landing pad to land on – just draw on the Stage
- Code to control the Space Ship
- 3 Variables, to store Gravity, Power and LeftRight information.
Space Ship Sprite
This what mine looked like:
The Left and Right costumes can be confusing, to move left, the flame needs to come out of the right, and to move right the flame needs to come out of the left hand side.
My Stage looked like this:
One thing to remember is to have the landing pad in a different colour.
Buttons and Code
I’ll put these 2 parts of the plan together as it makes more sense.
We decided to use 3 different buttons to control the Space Ship
- Up Arrow – This would fire the rocket at the bottom of the Space Ship
- Left Arrow – This would fire the rocket on the right side of the Space Ship
- Right Arrow – This would fire the rocket on the left side of the Space Ship
And now the code.
We needed code for each of the buttons, code to move the Space Ship and code to know when we have landed. This meant we had lots of small pieces of code all running together in Green Flags with forever loops.
We also decided that we would need 3 variables to store information so we could work out how far to move the Space Ship.
- Gravity – This would change as the Space Ship got higher, just as real gravity does.
- Power – This would change when the Up arrow was pressed and also when nothing was happening
- LeftRight – This would change when the left or right arrows were pressed
And don’t forget if we have variables, we should set them to starting values when the Green Flag is clicked.
This also has the code that puts the Space Ship in to a starting position. Note the Y value as this is used later in the code to see if we are on the ground or not.
For example, when the one of the left or right arrow keys was pressed we would change the Costume and change the LeftRight variable.
We did the same for the left arrow. Note that we just add (or subtract for the left arrow) a small amount from the LeftRight variable.
The delay is to make sure that the variable is not updated too fast if the arrow button is held down.
For the Up arrow we have to change a different variable
Note the wait is much shorter, this is to match the wait in the forever loop that moves the Sprite.
We also change the Power variable by a large amount as this helps with the calculations.
Once we had the buttons working we could move on to the Code that moved the Sprite.
The Left or Right movements were easy, that would just be the amount in the LeftRight variable.
The Up or Down movements were a little trickier as we had to account for Gravity as well. So we just used the Y position of the Sprite and added 180 (to make it a positive number). This meant that Gravity changed as the Sprite moved up or down, which is exactly how Gravity works in real life.
This is how the code ended up
We move first and then check how high we are.
If we are above the ground, we know this by checking that we are above the starting position of the Sprite, then we adjust Gravity and adjust the Power down.
Note the wait is the same as the Up Arrow code.
Also, if we are on the ground, we set Gravity and Power to 0.
There was one final piece of code that we didn’t do but I have added here, this was to check if we had landed and if we had, display a message and end.
It was just another Green Flag with a Forever loop and we kept checking to see if we were touching the colour of the landing pad.
Not much here, but might be better if there was a wait as well.
Finally, if you want to see all the code in one go, here it is
The project that we built is available on the Scratch Web Site:
User Name : cdadvancers1819
Password : advancers
Project : Class-Gravity
Remember, you can put your own projects up there as well if you want.