You are on page 1of 21

Projects

Rocket launch
Make an animation of a rocket propelling a satellite into
orbit

Step 1 You will make

Make an animation to propel a satellite into orbit — by hitching a ride on a rocket! Your animation will create cool
graphic effects and simulate the best amount of fuel to give the rocket.

Computer simulations are used to calculate what will happen when we send a rocket into space. Running
simulations and making plans on a computer reduces the risk of expensive or dangerous problems in space.

You will:
Use images in your animations
Use for loops to repeat actions
Join conditions with and
Dorothy Vaughan made important contributions to early spaceflight. Seeing that the future would use electronic
computers, she taught herself, and her team, how to code. The film Hidden Figures tells the story of how Dorothy
and other Black women played a key role in the success of the US space program.
Step 2 Set the scene

The animation needs a space backdrop with a planet to launch


the rocket from.

Open the project template (https://trinket.io/python/f2199f5a8c).


If you have a Trinket account, you can click on the Remix button to save a copy to your My Trinkets
library.

You will use a screen_size variable to set the size of the screen and in calculations. Variables defined outside
functions are global so you can use them anywhere in your program.

Find the comment Setup global variables and add a line of code to create your screen_size
variable:
main.py
7 #Setup global variables
8 screen_size = 400

Use the screen_size variable to create a square 400 by 400 pixels:


main.py — setup()
18 def setup():
19 #Setup your animation here
20 size(screen_size, screen_size)

The starter project has three different planet images and the moon provided for you. You can view these in
the Trinket image library by selecting the View and Add Images button.

Choose: Decide which image you want to use and make a note of the filename. For example,
orange_planet.png.
It’s a good idea to load images in setup() so that they are ready when you need to use them and your animation will
run quickly.

The image_mode(CENTER) line says that you will be positioning images by giving the coordinates of the
centre of the image (instead of the top left corner).
Also add code to the setup() function to load your chosen image into a planet global variable. The
variable needs to be global so you can use it later when you draw the planet to the screen.
main.py
18 def setup():
19 #Setup your animation here
20 size(screen_size, screen_size)
21 image_mode(CENTER)
22 global planet
23 planet = load_image('planet.png') #Your chosen planet

Define a draw_background() function, to draw the background, below the comment that tells you where
it should go.
Use background(0) to set the background colour to black and add an image() function to draw the
planet. The image() function is laid out:

image(image filename, x-coordinate, y-coordinate, image_width, image_height)

The p5 library sets global width and height variables based on the size of the screen. Use these in your
code to position the planet with its centre half-way across (width/2) and at the bottom (height) of the
screen.
main.py — draw_background()
14 #The draw_background function goes here
15 def draw_background():
16 background(0) #Short for background(0, 0, 0) — black
17 image(planet, width/2, height, 300, 300) #Draw the image

Putting all the code for drawing the background into one function makes your code easier to understand.

To make the background appear, call draw_background() in draw(). This will cause the background to
be re-drawn every time draw() is called, covering over any older drawing:

main.py — draw()
28 def draw():
29 #Things to do in every frame
30 draw_background()
Test: Run your code and check that it draws a black background with half a planet at the bottom.

Save your project


Step 3 Liftoff!

Each time a new frame is drawn, the rocket needs to move up


the screen to create an animation effect.

The starter project has a rocket image provided for you.

Add code to the setup() function to load the rocket image into a rocket global variable.

main.py
20 def setup():
21 #Setup your animation here
22 size(screen_size, screen_size)
23 image_mode(CENTER)
24 global planet, rocket
25 planet = load_image('planet.png')
26 rocket = load_image('rocket.png')

The y position of the rocket will start at 400 (the screen height) and then decrease by 1 each time a new frame is
drawn.

Add a rocket_y global variable to keep track of the rocket’s y position.


main.py
7 #Setup global variables
8 screen_size = 400
9 rocket_y = screen_size #Start at the bottom
Define a draw_rocket() function to change the rocket’s y position and redraw it.
rocket_y -= 1 is a shorter way of saying rocket_y = rocket_y - 1.

main.py
11 #The draw_rocket function goes here
12 def draw_rocket():
13
14 global rocket_y #Use the global rocket_y variable
15 rocket_y -= 1 #Move the rocket
16 image(rocket, width/2, rocket_y, 64, 64)

Call your new draw_rocket() in the draw() function so that the rocket gets redrawn every frame.
main.py
34 def draw():
35 #Things to do in every frame
36 draw_background()
37 draw_rocket()

Test: Run your code to check that the rocket starts at the bottom of the screen and moves up each frame.

Save your project


Step 4 Exhaust effects

The rocket will look more realistic with some special effects to
simulate the exhaust trail.
You can create cool effects by using a for loop to draw lots of
shapes in each frame.

Coding is used to make graphic effects for movies and games. It’s much quicker to write code than to draw each
frame of an animation individually.

Drawing lots of yellow ellipses at different y positions creates an exhaust trail with a round bottom.

A for loop repeats a piece of code once for every item it is given. To run the code in a for loop a certain
number of times, you can use the range() function. For example, range(5) creates a sequence of five
numbers starting from 0, so [0, 1, 2, 3, 4].
Each time the for loop repeats, it sets a variable to the current item so that you can use it in the loop.
Update your draw_rocket() function to include a for loop that repeats the drawing of 25 exhaust
ellipses. The loop variable i gets added to rocket_y to draw each ellipse further below the rocket.
main.py - draw_rocket()
12 def draw_rocket():
13
14 global rocket_y
15 rocket_y -= 1
16
17 no_stroke() #Turn off the stroke
18
19 for i in range(25): #Draw 25 burning exhaust ellipses
20 fill(255, 255, 0) #Yellow
21 ellipse(width/2, rocket_y + i, 8, 3) #i increases each time the loop repeats
22
23 image(rocket, width/2, rocket_y, 64, 64)
Test: Run your code to check the rocket has a new exhaust trail.

The i variable can also be used to create a colour gradient with less green in each ellipse that gets drawn.

Change the call to fill() to set the amount of green to 255 - i*10 so that the first ellipse has equal
amounts of red and green and the last ellipse has very little green.
main.py - draw_rocket()
19 for i in range(25):
20 fill(255, 255 - i * 10, 0) #Reduce the amount of green
21 ellipse(width/2, rocket_y + i, 8, 3)

Test: Check that you get a trail of ellipses gradually changing from yellow to red.

The smoke exhaust trail is created by drawing lots of slightly transparent grey ellipses at different positions in each
frame.
This time the fill() is outside the loop as the colour is the same for each smoke ellipse. The fourth input to
fill() is the opacity, a low opacity value makes the colour more transparent so you can see the shapes
underneath.

In each frame of the animation, 20 ellipses of random sizes will be drawn at random positions.
main.py - draw_rocket()
19 for i in range(25):
20 fill(255, 255 - i * 10, 0)
21 ellipse(width/2, rocket_y + i, 8, 3)
22
23 fill(200, 200, 200, 100) #Transparent grey
24 for i in range(20): #Draw 20 random smoke ellipses
25 ellipse(width/2 + randint(-5, 5), rocket_y + randint(20, 50), randint(5, 10), randint(5, 10))
26
27 image(rocket, width/2, rocket_y, 64, 64)

Test: Run your program and check the exhaust fumes are visible.

Save your project


Step 5 Burn fuel

One of the most important things to decide when launching a


rocket is how much fuel to load into it.
To do this, you need to simulate how much fuel will be burned
on the journey.

Add a variable to keep track of how much fuel your rocket burns (in frames).
main.py
7 #Setup global variables
8 screen_size = 400
9 rocket_y = screen_size
10 burn = 100 #How much fuel is burned in each frame

At the bottom of your program, add code to ask the user how much fuel to add to the rocket and store their
answer in a fuel global variable.
main.py
52 fuel = int(input('How many kilograms of fuel do you want to use?'))
53 run()

The rocket should only move if it hasn’t burned all of its fuel.

Add code to the draw_rocket() function to reduce the remaining fuel by the burn of each frame. Use
print() to show how much fuel is left in each frame.

You need to say that you want to use the global fuel and burn variables.
main.py — draw_rocket()
15 global rocket_y, fuel, burn
16 rocket_y -= 1
17 fuel -= burn #Burn fuel
18 print('Fuel left: ', fuel)
Test: Run your program to check that the animation doesn’t start until How many kilograms of fuel
do you want to use? has been answered. Try entering 30000 as the amount of fuel.

The rocket will keep going even if it has no fuel left.

The rocket should only move if it has enough fuel left. Add an if statement to check that fuel >= burn.

You will need to indent all of the lines of code before the image() function call. To do this, highlight all of
the lines with the mouse and then tap the Tab on the keyboard to indent all the lines at once.
The image() line doesn’t need to be indented because you always want to draw the rocket.

main.py — draw_rocket()
15 global rocket_y, fuel, burn
16
17 if fuel >= burn: #Still got fuel
18 rocket_y -= 1
19 fuel -= burn
20 print('Fuel left: ', fuel)
21
22 no_stroke() #Turn off the stroke
23
24 for i in range(25):
25 fill(255, 255 - i*10, 0)
26 ellipse(width/2, rocket_y + i, 8, 3)
27
28 fill(200, 200, 200, 100)
29 for i in range(20):
30 ellipse(width/2 + randint(-5, 5), rocket_y + randint(20, 50), randint(5, 10), randint(5, 10))
31
32 image(rocket, width/2, rocket_y, 64, 64)
Test: Run your program to check that the rocket stops when there is no fuel left.

This computer simulation isn’t very accurate, but it’s good enough for our animation.

Save your project


Step 6 Reaching orbit

The point of launching the rocket is to propel a


satellite into orbit.
An orbit is a curved path that one object takes
around another due to gravity.
The rocket can change colour to show how
successful the launch was.

Create two new global variables to set the radius of the orbit circle and the y coordinate of the orbit to the
point the rocket centre needs to reach to launch the satellite.

main.py
7 #Setup global variables
8 screen_size = 400
9 rocket_y = screen_size
10 burn = 100
11 orbit_radius = 250
12 orbit_y = screen_size - orbit_radius

Update the draw_background() function to draw an ellipse to represent the satellite orbit that the rocket
needs to reach.

main.py - draw_background()
37 def draw_background():
38 background(0) #Short for background(0, 0, 0) — black
39 image(planet, width/2, height, 300, 300)
40
41 no_fill() #Turn off any fill
42 stroke(255) #Set a white stroke
43 stroke_weight(2)
44 ellipse(width/2, height, orbit_radius * 2, orbit_radius * 2)
Test: Run your program and check that a white orbit line is drawn.

The rocket should stop when it reaches the satellite orbit — the end of the mission.

Update your if fuel >= burn code to also check that the rocket hasn’t reached the orbit.

You can use an and in if statements to check if two, or more, conditions are true.
main.py - draw_rocket()
14 #The draw_rocket function goes here
15 def draw_rocket():
16
17 global rocket_y, fuel, burn
18
19 if fuel >= burn and rocket_y > orbit_y: #Still flying

Test: Run your project and enter 50000 as the amount of fuel. This should be plenty of fuel to reach orbit.
The rocket should stop moving when it reaches orbit.

The rocket should be coloured red if it runs out of fuel before getting high enough to launch the satellite.

main.py — draw_rocket()
30 fill(200, 200, 200, 100)
31 for i in range(20):
32 ellipse(width/2 + randint(-5, 5), rocket_y + randint(20, 50), randint(5, 10), randint(5, 10))
33
34 if fuel < burn and rocket_y > orbit_y: #No more fuel and not in orbit
35 tint(255, 0, 0) #Failure
Test: Run your code and enter 20000 as the amount of fuel. Check that the rocket turns red when it stops
below the orbit.

Oh no, the planet has turned red!

The tint() function sets the tint colour for all images that are drawn until you change the tint or use
no_tint() to turn it off.

Choose: Add a call to no_tint() after drawing the image so that the planet isn’t tinted red in the next
frame — or leave it if you like the planet turning red!
main.py - draw_rocket()
34 if fuel < burn and rocket_y > orbit_y:
35 tint(255, 0, 0) #Failure
36
37 image(rocket, width/2, rocket_y, 64, 64)
38 no_tint() #So the planet isn't tinted red in the next frame!

Use the tint() function again, this time to colour the rocket green if the rocket has enough fuel to reach
the satellite orbit:

main.py - draw_rocket()
34 if fuel < burn and rocket_y > orbit_y:
35 tint(255, 0, 0) #Failure
36 elif rocket_y <= orbit_y:
37 tint(0, 255, 0) #Success
38
39 image(rocket, width/2, rocket_y, 64, 64)
40 no_tint()
Test: Run your project and enter 50000 as the amount of fuel. Check that your rocket turns green when it
reaches the satellite orbit.

You now have a simulation that can be used to show how much fuel is needed as a minimum to reach the satellite
orbit. That’s great; however, you could take a huge amount of fuel and still be successful, but this is costly and
wasteful!

Amend the conditions in your success code so that the rocket only turns green if it reaches the orbit and
has less than 1,000kg of fuel left.
Add code to colour the rocket yellow if the rocket has more than 1,000kg of fuel left when it reaches orbit.

main.py
34 if fuel < burn and rocket_y > orbit_y:
35 tint(255, 0, 0) #Failure
36 elif fuel < 1000 and rocket_y <= orbit_y:
37 tint(0, 255, 0) #Success
38 elif fuel >= 1000 and rocket_y <= orbit_y:
39 tint(255, 200, 0) #Too much fuel
40
41 image(rocket, width/2, rocket_y, 64, 64)
42 no_tint() #So the planet isn't tinted in the next frame!
Test: Run your program several times with different numbers; for example, 25,000kg of fuel should be the
amount needed to turn the rocket green, but also check that the yellow tint works too by using a bigger
number.

Save your project


Upgrade your project
Your project is complete, but you can still make it better if you want. Here are a few upgrades to consider:
Let the user set the rate at which the rocket burns fuel or how far the rocket flies in each frame.
Add a second, higher, orbit for the rocket to reach and drop off another satellite. Have it change colour again when
it gets there.
Add you own rocket or planet image to use in your project.

Adding your own image to Trinket


If you want to use your own picture for the planet — or the rocket — then you can do that by choosing the View and
Add Images button.

Then choose Image Library and follow the on-screen instructions to upload an image to your Trinket image library.

Note the filename of the image you’ve uploaded, and use it in the code in place of planet.png.
Here’s a version of the project with all those upgrades, so you can see how they could work:

Completed project
You can view the completed project here (https://trinket.io/python/622b4dd113).

Save your project


What next?
If you are following the Introduction to Python (https://projects.raspberrypi.org/en/raspberrypi/python-intro)
pathway, you can move on to the Make a face (https://projects.raspberrypi.org/en/projects/make-a-face)
project. In this project, you will draw a face or mask using geometric shapes.

If you want to have more fun exploring Python, then you could try out any of these projects (https://projects.raspbe
rrypi.org/en/projects?software%5B%5D=python).

Published by Raspberry Pi Foundation (https://www.raspberrypi.org) under a Creative Commons license (htt


ps://creativecommons.org/licenses/by-sa/4.0/).
View project & license on GitHub (https://github.com/RaspberryPiLearning/rocket-launch)

You might also like