Have you ever wanted to create a realistic solar system animation using Python? In this tutorial, we’ll use Python’s Turtle module to simulate planets orbiting the Sun with smooth motion and visually appealing orbits!
This project is perfect for beginners interested in graphical programming, animations, and space simulations. By the end of this tutorial, you’ll have a fully functional solar system model where planets revolve around the Sun dynamically. ππͺ
What You’ll Learn
β How to use Python Turtle for animations π¨
β Drawing and animating planetary orbits dynamically π
β Simulating planetary motion using trigonometry and math π
β Creating visually appealing celestial objects with colors and sizes π
β Understanding how real-world physics applies to coding π
Step 1: Import Required Libraries
We start by importing the required Python modulesβturtle
for graphics, math
for calculations, and time
for smooth animations.
import turtle
import math
import time
Step 2: Set Up the Screen
We configure the background of our screen to black (to resemble space) and turn off automatic updates for smooth animations.
turtle.bgcolor("black")
turtle.tracer(0) # Disable auto updates for smooth animation
Step 3: Draw the Sun
We create the Sun at the center using a yellow circle.
turtle.penup()
turtle.goto(0, -50)
turtle.pendown()
turtle.color("yellow")
turtle.begin_fill()
turtle.circle(50) # Sun size
turtle.end_fill()
Step 4: Define Planetary Details
Now, let’s define a list of planets with their distance from the Sun, size, color, and speed.
planets = [
{"name": "Mercury", "distance": 70, "size": 5, "color": "gray", "speed": 0.2},
{"name": "Venus", "distance": 100, "size": 8, "color": "orange", "speed": 0.15},
{"name": "Earth", "distance": 140, "size": 10, "color": "blue", "speed": 0.1},
{"name": "Mars", "distance": 180, "size": 7, "color": "red", "speed": 0.08},
{"name": "Jupiter", "distance": 220, "size": 15, "color": "brown", "speed": 0.05},
{"name": "Saturn", "distance": 260, "size": 13, "color": "gold", "speed": 0.04},
{"name": "Uranus", "distance": 300, "size": 12, "color": "lightblue", "speed": 0.03},
{"name": "Neptune", "distance": 340, "size": 11, "color": "blue", "speed": 0.025}
]
Each planet has unique attributes:
- Distance (how far from the Sun it orbits)
- Size (scaled representation)
- Color (for realistic visuals)
- Speed (how fast it revolves)
Step 5: Create Planets and Orbits
We’ll create Turtle objects for each planet and draw their orbits using circles.
planet_turtles = []
orbits = []
for planet in planets:
# Create planet
t = turtle.Turtle()
t.shape("circle")
t.color(planet["color"])
t.shapesize(planet["size"] / 10) # Scale down size
t.penup()
planet_turtles.append(t)
# Draw orbit path
orbit = turtle.Turtle()
orbit.speed(0)
orbit.color("white")
orbit.penup()
orbit.goto(0, -planet["distance"])
orbit.pendown()
orbit.circle(planet["distance"]) # Draw orbit circle
orbit.hideturtle()
orbits.append(orbit)
Step 6: Animate the Planetary Motion
Now, let’s move the planets around the Sun using trigonometric functions (cosine and sine) to calculate their positions in circular orbits.
angle = 0
while True:
turtle.update()
time.sleep(0.02) # Smooth animation delay
angle += 1
for i, planet in enumerate(planets):
x = planet["distance"] * math.cos(math.radians(angle * planet["speed"]))
y = planet["distance"] * math.sin(math.radians(angle * planet["speed"]))
planet_turtles[i].goto(x, y) # Move planet to new position
πΉ How it works:
- We increment the angle to simulate rotation.
- The x, y coordinates are calculated using trigonometry (
cos
&sin
). - Each planet moves smoothly along its orbit.
Final Output: A Realistic Solar System Model
When you run this script, you’ll see planets orbiting the Sun in a realistic motion. The speeds differ based on their distance, creating a stunning solar system animation! πβ¨
Watch the Full Tutorial
For a step-by-step video guide, watch the YouTube tutorial:
Download the Full Code
You can access the complete source code on GitHub:
π GitHub – Solar System Animation
Conclusion
In this tutorial, we learned how to create a beautiful solar system animation using Python Turtle. This project is an excellent way to explore graphics programming, trigonometry, and animations in Python. ππ»
π‘ Next Steps:
β Experiment with more planets or moons π
β Adjust speed and sizes for a more realistic effect
β Try adding stars in the background β¨
If you found this tutorial helpful, donβt forget to:
π Like the post
π¬ Comment your thoughts
π Subscribe for more Python animation projects!
π Happy Coding! πβ¨