Mathematics Scratch Game
Hard
60 mins
615 points
What you need:
  • Computer/laptop

Rocket Lander

The Space X Falcon 9 rocket is capable of landing back on earth on top of a moving ship at sea, in this project we try and recreate the landing and make it into a game.

This code along has finished! ⌚

You can watch it back by playing the video!

The code along is open for everyone to join in but if you would like to earn the points and badges please join our club!

1 - The Space X Falcon 9

Unlike most rockets that get launched into space, which can never be used again can never be used, the Falcon 9 is capable of re-entering the atmosphere and landing vertically. This feat was achieved for the first time on flight 20 in December 2015.

As the flight number 20 suggests, there were many tests and attempts before flight number 20 that weren't successful! Don't worry though all these rockets are unmanned and are either piloted by computers and remotely from the mission control center.


2 - Open the starter project

We've created a starter project with the backdrop and a Falcon 9 rocket and ship sprites already added. Open the project by using this link https://scratch.mit.edu/projects/520071024/editor.


3 - Program gravity

When a rocket enters back into Earth's atmosphere, gravity will start to pull it downwards. We can simulate this in code by changing the Y position of the sprite.

Add the following code to the rocket sprite to make it:

  • shrink it's size to 25%.
  • start at the top center.
  • point upwards (be vertical).
  • start falling.

when green flag clicked set size to (25) % go to x (0) y (200) point in direction (0) forever change y by (-5) end


4 - Program booster

Now let's program the up arrow to make the rocket fires it's booster so we can slow it down.

Add the following code to the rocket sprite.

when green flag clicked forever if < key (up arrow v) pressed? > then move (8) steps end

When you try it out you'll see that you can slow down the rocket and even make it go back up again. This is because move 8 steps  is greater than change y by -5  


5 - Paint the rocket thrust

To make it more realistic when we fire the rocket's booster, we will create a new costume for the rocket sprite with fire thrusting out of the bottom.

  • Open the costume editor.
  • Rename the costume to 'normal'.
  • Duplicate the normal costume and rename it to 'thrust'.
  • Use the paint tools to draw fire coming out at the bottom of the rocket (in the 'thrust' costume).

To start off the game the rocket sprite should be showing the 'normal' costume so add switch costume to (normal) blocks underneath the when clicked block and also underneath the change y by -5  block.

when green flag clicked switch costume to (normal v)// add this block set size to (25) % go to x (0) y (200) point in direction (0) forever change y by (-5) switch costume to (normal v)// add this block end



6 - Show the rocket thrust

Now let's show the 'thrust' costume when you press the up arrow and fire the booster.

Add the switch costume to (thrust) block above the move 8 steps block.

when green flag clicked forever if < key (up arrow v) pressed? > then switch costume to (thrust v)//add this block move (8) steps end


7 - Move left

Next we will program the left arrow to rotate the rocket to the left and fire the booster.

Add the following code to the rocket sprite.

when green flag clicked forever if < key (left arrow v) pressed? > then switch costume to (thrust v) turn ccw (3) degrees move (4) steps end


8 - Move right

In the same way we will program the right arrow to rotate the rocket to the right and fire the booster.

Add the following code to the rocket sprite.

when green flag clicked forever if < key (right arrow v) pressed? > then switch costume to (thrust v) turn cw (3) degrees // notice this is different from moving left move (4) steps end


9 - Program the ship sprite

The aim of the game is to land the rocket on the ship that'd slowly moving in the sea.

Add the following code to the ship sprite to place it on top of water and make it slowly move back and forth.

when green flag clicked go to (random position v) set y to (-160) // change this to match where your water is forever move (0.25) steps if on edge, bounce end


10 - Land on the ship

To land it on the ship safely the rocket must be straight so we will need to check its angle as it touches the ship.

Create a variable called 'angle' and then add the following new code to the rocket sprite above the change y by -5  block. This code checks if the rocket is touching the ship and also that the angle is between -5 and 5.

when green flag clicked switch costume to (normal v) set size to (25) % go to x (0) y (200) point in direction (0) forever set [angle v] to (direction) if < touching (ship v) ? > then if < < (angle) > (-5) > and < (angle) < (5) > > then stop [all v] // this stops eveything else end end change y by (-5) switch costume to (normal v) end

Test your code and see if it lands when the angle is right and if it doesn't land when the angle is outside of -5 and 5.




11 - Explode

If the rocket doesn't land correctly on the ship or if it touches the water we will make it explode.

To make it explode we will create 3 (or more) new costumes for the rocket showing it explode more and more, and then we will create a custom block (function) to animate through the new exploding costumes.

First create some new costumes by duplicating the 'normal' costume and then drawing fire coming out of the rocket. You can create as many as you want but 3 should be enough. Rename the new costumes to be 'explode-1', 'explode-2' and so on.

Then create a new custom block called 'explode'. Add the following code to the define explode and add a explode block underneath the 'else'.

define explode switch costume to (explode-1 v) wait (0.25) seconds switch costume to (explode-2 v) wait (0.25) seconds switch costume to (explode-2 v) stop [all v] when green flag clicked switch costume to (normal v) set size to (25) % go to x (0) y (200) point in direction (0) forever set [angle v] to (direction) if < touching (ship v) ? > then if < < (angle) > (-5) > and < (angle) < (5) > > then stop [all v] else explode // add this in here end end change y by (-5) switch costume to (normal v) end

Test your code and see if it explodes when you land it sideways.



12 - Crash into the sea

If you miss the ship and land in the sea then the rocket will also explode.

Add the following new code to the rocket sprite above the change y by -5  block. Use the colour of the sea in the touching color   ? block.

define explode switch costume to (explode-1 v) wait (0.25) seconds switch costume to (explode-2 v) wait (0.25) seconds switch costume to (explode-2 v) stop [all v] when green flag clicked switch costume to (normal v) set size to (25) % go to x (0) y (200) point in direction (0) forever set [angle v] to (direction) if < touching (ship v) ? > then if < < (angle) > (-5) > and < (angle) < (5) > > then stop [all v] else explode end end if < touching color [#356c34] > then // add these in here explode end change y by (-5) switch costume to (normal v) end

Test your code and see if it explodes when it touches the water.



13 - Fuel

The Falcon rockets only have a certain amount of fuel on board that they can use so we're going to:

  1. add a fuel variable.
  2. set it to a certain amount of fuel, say 60.
  3. use the fuel when you use the booster.
  4. only allow the booster to be used if you have some fuel left.

So if you use all of your fuel before landing your rocket will just freefall!

Add a variable called 'fuel' and then change each of the code blocks for up arrow, left arrow and right arrow as follows.

when green flag clicked set [fuel v] to (60)//add this block forever if < < key (up arrow v) pressed? > and < (fuel) > (0) > > then // add in the fuel condition switch costume to (thrust v) change [fuel] by (-1) //add this block move (8) steps end when green flag clicked forever if < < key (left arrow v) pressed? > and < (fuel) > (0) > > then // add in the fuel condition switch costume to (thrust v) change [fuel] by (-1) //add this block turn ccw (3) degrees move (4) steps end when green flag clicked forever if < < key (right arrow v) pressed? > and < (fuel) > (0) > > then // add in the fuel condition switch costume to (thrust v) change [fuel] by (-1) //add this block turn cw (3) degrees move (4) steps end



14 - SpaceX flights explained

Watch this short 2 minute video that explains the different stages of a SpaceX Falcon 9 flight.

Credit Supercluster Science YouTube Channel.

15 - A Falcon 9 landing

Watch these short videos and see actual SpaceX Falcon 9 rockets landing on land and on a ship in the sea.

Landing on land

Credit SciNews YouTube Channel.

Landing at sea

Credit Bloomberg Quicktake YouTube Channel.

Join our club 😃

Create your profile, earn points & badges and learn how to code!

Copyright Notice
This lesson is copyright of Coding Ireland. Unauthorised use, copying or distribution is not allowed.

Scratch is developed by the Lifelong Kindergarten Group at the MIT Media Lab. It is available for free at https://scratch.mit.edu
Our website uses cookies to make your browsing experience better. By using our website you agree to our use of cookies. Learn more