Hi everyone, my name is Gabriel and I'm a Senior Instructor at Juni Learning!
In this project, we'll be building a model of the solar system by using Python Turtle.
Before we start, first consider:
- What models of the solar system have you seen before?
- What might you imagine about the solar system?
- How could we model it?
Planets are basically circles — and using knowledge we've gained from previous tutorials, we can draw circles to represent planets, with their appropriate coloring.
Who is this project for?
- Juni Level: Python Level 1
- Coding Language: Python
- Coding experience: Familiarity with basic Python commands and intermediate experience in Python with Turtle.
- Difficulty level: Medium
- Approx. lines of code: 100~200, and then ~200 for bonus features
- Est. Time to Complete: ~1.5hrs, but every student is different!
Core concepts practiced:
- Math related to circles
- Organizing code
Prerequisite concepts to know/review:
- Math with ratios and radii
Click run to see the project yourself below!
You can also view my project solution code if you get stuck.
Specific features to look out for:
We'll be utilizing the radius of the sun as a constant variable to proportion the rest of our planets. This helps keep our universe in scale. However, the proportions of the sun in comparison with the rest of the solar system is massive.
Look at this image of the literal scale of the solar system — zoom in, it’s tiny! As a result, we can downscale what we consider to sun’s radius to be. We can do this by adding a multiplier to the sun’s drawing, while keeping everything else in proportion.
We have a couple of implementation options. We could create a different turtle for every single planet, and put them into a list to iterate through them later on.
Since we are using the sun’s radius to keep all of the planets in the system proportional, we'll have a lot of duplicated code that draws a planet based on the radius of the sun.
General order of steps to implement:
- Make lists of the different distances, radii, and colors of the planets you require.
- Draw the Sun in the center of the screen and color the background black.
- Draw all of the other planets circling the Sun using the distances and radii, making the distances a ratio to the Sun’s radius.
- Draw the orbits of each planet using the same distance ratio.
How do we do each of these steps?
Step 1. Make lists of the different distances, radii, and colors of the planets you require.
We'll need to fill our shape with their correct colors. We can get closer to the specific shades of the different planets by using their rgb values. I've listed the rgb values of each planet below:
- Sun: (255, 204, 51)
- Mercury: (151, 151, 159)
- Venus: (211, 156, 126)
- Earth: (140, 177, 222)
- Mars: (198, 123, 92)
- Jupiter: (211, 156, 126)
- Saturn: (197, 171, 110)
- Uranus: (213, 251, 252)
- Neptune: (62, 84, 232)
If we want every planet to be drawn on a line, we need to have something that takes care of drawing them across the screen. We should, therefore, determine what their distance from the Sun is.
Reference the distances listed out below!
- Sun: 0 mi
- Mercury: 35.98 million mi
- Venus: 67.24 million mi
- Earth: 92.96 million mi
- Mars: 141.6 million mi
- Jupiter: 483.8 million mi
- Saturn: 890.9 million mi
- Uranus: 1.784 billion mi
- Neptune: 2.793 billion mi
Next, we need to find what each radius is:
- Sun: 432690 mi
- Mercury: 1516 mi
- Venus: 3760.4 mi
- Earth: 3958.8 mi
- Mars: 2106.1 mi
- Jupiter: 43441 mi
- Saturn: 36184 mi
- Uranus: 15759 mi
- Neptune: 15299 mi
Step 2. Draw the Sun in the center of the screen and color the background black.
Let’s now start by drawing the sun in the center of our screen.
How do we calculate how far to go?
We can define how much the turtle itself needs to draw as its circumference–these will ultimately be the same thing. Since we have the sun’s radius, we can therefore use the circumference formula 2πr to calculate the sun’s circumference.
Then, we can draw a circle by repeatedly going forward and turning right multiple times in small increments.
For example, since there are 360º, we can do this is in 36 increments — 1/36th of the circumference, turning right 10º a total of 36 times.
Let’s also color the sun.
- We can use RGB values that we provided above, passing them into
- We can also use
begin_fill()before we start drawing the sun in order to make the shape full of the color we want. When we are done drawing our lines, we can use
end_fill()in order to fully color the shape.
Let’s make the background black.
- We can do this via a special turtle
- We should generate a new turtle to handle the background, then set
bgcolor(“black”)on that new turtle.
Step 3. Draw all of the other planets circling the Sun using the distances and radii, making the distances a ratio to the Sun’s radius.
We should create a function to draw a planet given a list of [r,g,b] values, their distance from the sun, and their radius.
- We can then repeat the steps from drawing the sun as a planet, while also calculating their distance from the sun by adding their radius, their distance from the sun, and their radius.
- Now downscale this by the same variable we downscaled the sun by.
Then, we can start drawing out pathways in the solar system corresponding to planets’ distances from the sun.
- Our furthest out planet is Neptune at 2.793 billion miles, it’s really hard to see!
- Think about your car. It might go 60 miles per hour. That's a mile per minute. Now imagine that you could move a mile a second – that's like moving across NYC in just 13 seconds. Now, think of 1 million miles away. That would take you 12 days to get there.
- Then, 1 billion miles away would take you 30 years moving a mile a second. That's the scale of our solar system–that's not even accounting for stars even farther away!
- The USA is 2680 miles long – if you could travel a mile a second, you'd get across the US in 45 minutes. Even if you could travel at the speed of sound — 767 miles per hour — it would take you 13 years to get to the sun.
Just like with the sun, we can calculate the circumference of each planet and draw a circle according to the necessary length of the circumference that we need to traverse.
Oh no! The sun is so massive and other planets are so far away that we can’t even see them on the screen. How can we get around this issue?
Let’s try scaling everything down.
- Let’s make everything based on the radius of the Sun to keep everything proportional, and then scale everything else down.
- We should start by scaling them down using the scale parameter we used for the sun earlier.
- We should downscale how far away these planets are to make them show up – e.g. divide them by 50.
Step 4. Draw the orbits of each planet using the same distance ratio.
We can also draw the orbits of a planet by adding a new orbit turtle that will draw a circle all along the planet’s distance from the sun.
We run into the issue of the circle drawing forward rather than around the sun – we can get around this by using
setheading(270) to make the turtle point down so it draws the circle as we desire.
We then also need to reset the heading to 0 again before drawing to ensure that it draws from the same place every time.
Nice! Want more of a challenge? Try adding these bonus features!
- Randomizing Planet Locations: We can implement random locations for the planets to go to by using
random.randint(0,360)to generate a random value between 0 and 360. We can then
setheading()before having the planet move forward, making it move forward in a random direction.
- Sense of Scale: Look at how the Earth is compared to the other planets. Try scaling up the sun so that everything is properly proportional. Play around with our distance scaling, and examine just how massive the solar system is in comparison with the Earth!
- Add randomizing drawing where the planets go.
- Add rings to planets like Saturn!
- Make the planets spin by looping the planets through their path, and using
screen.update()! (only if you're familiar with event listeners)
- Add stars! (you can draw small circles all around the black screen)
Great job — now check out more tutorials!
Thanks for watching and hope you had fun making this Python project with me!
Built the project above? We'd love to see it! If you're interested in sharing your coding project or experiences with diversity in STEM, please reach out to us at firstname.lastname@example.org.
Every week, we’ll be posting project tutorials like this one, for different coding languages and experience levels, as well as math tutorials.
Visit our coding projects blog page to find our other tutorials in more coding languages! You can also subscribe to our newsletter using the form to the right or top of this page for more updates on our programs and newest coding tutorials.
Need more help, or want to keep learning?
Looking up your coding questions is one of the best ways to learn!
Another great way to learn is from an experienced coder or instructor. Juni Learning Computer Science Instructors like Gabriel work closely with students ages 8-18, and are specially trained to adapt to each child's unique learning style, pace, and interests.