For the last two weeks we have been learning about Gravity and building a lunar module project to simulate how gravity works in the real world.
What is Gravity?
Gravity is what holds us down on Earth, if we jump up in the air, gravity will pull us back down.
If you want to break free of the Earths gravity you have to travel very fast and reach “escape velocity” this is about 25,000 mph.
The space station, even though it is travelling at 17,150 mph (that’s about 5 miles per second!) has not escaped the Earths gravity, while it is trying to travel away from the Earth at 17.150 mph gravity is pulling it back down, this makes it travel around the Earth in an Orbit.
So we set out to build a Gravity simulation game.
In order to make it a little easier we decided to use the Moon as our base as the gravity is less and there is no atmosphere to slow us down when we are moving.
Some of the rules that we had to code for were:
- Our spaceship should not move if it is on the Ground.
- The higher it gets the faster it should move – Less Gravity.
- If it starts moving left or right, it should stay moving until it is stopped – No Atmosphere.
The Sprite needed 4 Costumes:
- Landed – This is the default costume where nothing is happening.
- Up – This costume has a flame coming out the bottom.
- Left – This costume has a flame coming out the right hand side.
- Right – This costume has a flame coming out the left hand side.
Once we had the sprite built we started on the code. We started with the left and right code first, basically when you clicked on either the left or the right button, we would change the costume and also increase or decrease a variable to indicate how far we should move along the X axis (this is the one that goes from left to right).
We used a variable for the LeftRight movement to make it simpler. This ended with some code that looked like this:
We put a small delay in as well, this gave the effect of the thrusting going on and off which looked more realistic.
The left right movement was taken care of by a separate piece of code that just keep moving the Sprite.
This also had a little bit of code to stop everything if we touched a particular colour, this was the colour of my landing pad.
For the Up Code we did something similar, but had to add in the movement in as well.
To calculate the Up/Down movement was a little more complicated as we needed to adjust how much we moved up depending on how high we were, basically the Y value. Scratch doesn’t help here as the Y value goes from -180 to 0 and then on to +180, we needed it to always be positive, so we had to use a variable and add 180 to it.
The other thing we had to do was find a number that when we divided by the Y value it gave us a reasonable value to move the Sprite by, through trial and error this came out at 200.
Which ended up with some code like this:
This code also has all of the initialisation code, where we set all the variables we are using to their initial values. Notice that the UpDown default is 1, this is to ensure that the first time that you press the Up key that the spacecraft actually moves.
And if you want all of the code, here it is
And finally this is what my Project looked like in the end