Solar System Simulation with Interactive Popups
Below is a Python program using Pygame to create a solar system simulation with interactive popups for celestial bodies. The simulation includes planets orbiting the sun and moons orbiting planets, with detailed information displayed when clicking on each body.
import pygame
import math
import sys
import json
from pygame.locals import *
# Initialize pygame
pygame.init()
# Screen dimensions
WIDTH, HEIGHT = 1200, 800
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Solar System Simulation")
# Colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
YELLOW = (255, 255, 0)
BLUE = (100, 149, 237)
RED = (188, 39, 50)
DARK_GREY = (80, 78, 81)
LIGHT_BLUE = (173, 216, 230)
ORANGE = (255, 165, 0)
LIGHT_GREY = (211, 211, 211)
BROWN = (165, 42, 42)
GREEN = (0, 128, 0)
PURPLE = (128, 0, 128)
# Fonts
font = pygame.font.SysFont('Arial', 12)
title_font = pygame.font.SysFont('Arial', 16, bold=True)
# Celestial body data
celestial_bodies = {
"Sun": {
"radius": 30,
"color": YELLOW,
"distance": 0,
"orbit_speed": 0,
"rotation_speed": 0.01,
"diameter": "1,392,700 km",
"temperature": "5,500°C (surface)",
"rotation_period": "25-35 days (equator to poles)",
"composition": "71% Hydrogen, 27% Helium, 2% other elements",
"atmosphere": "None (plasma)",
"satellites": 0,
"type": "star"
},
"Mercury": {
"radius": 8,
"color": DARK_GREY,
"distance": 50,
"orbit_speed": 0.02,
"rotation_speed": 0.005,
"diameter": "4,880 km",
"temperature": "-173°C to 427°C",
"orbit_period": "88 Earth days",
"rotation_period": "59 Earth days",
"composition": "70% metals, 30% silicates",
"atmosphere": "Trace amounts of oxygen, sodium, hydrogen",
"satellites": 0,
"type": "planet",
"moons": []
},
"Venus": {
"radius": 12,
"color": LIGHT_GREY,
"distance": 80,
"orbit_speed": 0.015,
"rotation_speed": -0.002, # Negative for retrograde rotation
"diameter": "12,104 km",
"temperature": "462°C (average)",
"orbit_period": "225 Earth days",
"rotation_period": "243 Earth days (retrograde)",
"composition": "Silicate rocks and metals",
"atmosphere": "96.5% CO2, 3.5% nitrogen, traces of sulfur dioxide",
"satellites": 0,
"type": "planet",
"moons": []
},
"Earth": {
"radius": 13,
"color": BLUE,
"distance": 120,
"orbit_speed": 0.01,
"rotation_speed": 0.01,
"diameter": "12,742 km",
"temperature": "-88°C to 58°C",
"orbit_period": "365.25 days",
"rotation_period": "23.9 hours",
"composition": "Iron, oxygen, silicon, magnesium, nickel",
"atmosphere": "78% nitrogen, 21% oxygen, 1% other gases",
"satellites": 1,
"type": "planet",
"moons": [
{
"name": "Moon",
"radius": 3,
"color": WHITE,
"distance": 15,
"orbit_speed": 0.05,
"rotation_speed": 0.005,
"diameter": "3,474 km",
"temperature": "-173°C to 127°C",
"orbit_period": "27.3 days",
"rotation_period": "27.3 days (synchronous)",
"composition": "Silicate rocks, metals",
"atmosphere": "Very thin (sodium, potassium)"
}
]
},
"Mars": {
"radius": 10,
"color": RED,
"distance": 160,
"orbit_speed": 0.008,
"rotation_speed": 0.009,
"diameter": "6,779 km",
"temperature": "-87°C to -5°C",
"orbit_period": "687 Earth days",
"rotation_period": "24.6 hours",
"composition": "Iron, magnesium, aluminum, calcium, potassium",
"atmosphere": "95% CO2, 2.7% nitrogen, 1.6% argon",
"satellites": 2,
"type": "planet",
"moons": [
{
"name": "Phobos",
"radius": 2,
"color": DARK_GREY,
"distance": 8,
"orbit_speed": 0.1,
"rotation_speed": 0.008,
"diameter": "22.4 km",
"temperature": "-40°C (average)",
"orbit_period": "7.7 hours",
"rotation_period": "7.7 hours (synchronous)",
"composition": "Carbon-rich rock",
"atmosphere": "None"
},
{
"name": "Deimos",
"radius": 1.5,
"color": LIGHT_GREY,
"distance": 12,
"orbit_speed": 0.06,
"rotation_speed": 0.005,
"diameter": "12.4 km",
"temperature": "-40°C (average)",
"orbit_period": "30.3 hours",
"rotation_period": "30.3 hours (synchronous)",
"composition": "Carbon-rich rock",
"atmosphere": "None"
}
]
},
"Jupiter": {
"radius": 25,
"color": ORANGE,
"distance": 220,
"orbit_speed": 0.004,
"rotation_speed": 0.02,
"diameter": "139,820 km",
"temperature": "-108°C (cloud tops)",
"orbit_period": "11.9 Earth years",
"rotation_period": "9.9 hours",
"composition": "90% hydrogen, 10% helium, traces of other elements",
"atmosphere": "90% hydrogen, 10% helium, traces of methane, water, ammonia",
"satellites": 79,
"type": "planet",
"moons": [
{
"name": "Io",
"radius": 4,
"color": YELLOW,
"distance": 15,
"orbit_speed": 0.08,
"rotation_speed": 0.01,
"diameter": "3,643 km",
"temperature": "-130°C (average)",
"orbit_period": "1.77 days",
"rotation_period": "1.77 days (synchronous)",
"composition": "Silicate rock, sulfur compounds",
"atmosphere": "Thin (sulfur dioxide)"
},
{
"name": "Europa",
"radius": 3.5,
"color": LIGHT_BLUE,
"distance": 20,
"orbit_speed": 0.06,
"rotation_speed": 0.008,
"diameter": "3,122 km",
"temperature": "-160°C (surface)",
"orbit_period": "3.55 days",
"rotation_period": "3.55 days (synchronous)",
"composition": "Silicate rock, water ice",
"atmosphere": "Very thin (oxygen)"
}
]
},
"Saturn": {
"radius": 22,
"color": LIGHT_GREY,
"distance": 280,
"orbit_speed": 0.003,
"rotation_speed": 0.018,
"diameter": "116,460 km",
"temperature": "-139°C (average)",
"orbit_period": "29.5 Earth years",
"rotation_period": "10.7 hours",
"composition": "96% hydrogen, 3% helium, traces of other elements",
"atmosphere": "96% hydrogen, 3% helium, traces of methane, ammonia",
"satellites": 82,
"type": "planet",
"moons": [
{
"name": "Titan",
"radius": 5,
"color": ORANGE,
"distance": 25,
"orbit_speed": 0.04,
"rotation_speed": 0.006,
"diameter": "5,150 km",
"temperature": "-179°C (average)",
"orbit_period": "15.9 days",
"rotation_period": "15.9 days (synchronous)",
"composition": "Water ice, rocky material",
"atmosphere": "95% nitrogen, 5% methane"
}
]
},
"Uranus": {
"radius": 18,
"color": LIGHT_BLUE,
"distance": 340,
"orbit_speed": 0.002,
"rotation_speed": 0.015,
"diameter": "50,724 km",
"temperature": "-197°C (average)",
"orbit_period": "84 Earth years",
"rotation_period": "17.2 hours (retrograde)",
"composition": "80% hydrogen, 15% helium, 2% methane",
"atmosphere": "83% hydrogen, 15% helium, 2% methane",
"satellites": 27,
"type": "planet",
"moons": [
{
"name": "Titania",
"radius": 3,
"color": LIGHT_GREY,
"distance": 12,
"orbit_speed": 0.03,
"rotation_speed": 0.004,
"diameter": "1,578 km",
"temperature": "-203°C (average)",
"orbit_period": "8.7 days",
"rotation_period": "8.7 days (synchronous)",
"composition": "Water ice, rocky material",
"atmosphere": "None"
}
]
},
"Neptune": {
"radius": 17,
"color": BLUE,
"distance": 400,
"orbit_speed": 0.0015,
"rotation_speed": 0.012,
"diameter": "49,244 km",
"temperature": "-201°C (average)",
"orbit_period": "165 Earth years",
"rotation_period": "16.1 hours",
"composition": "80% hydrogen, 19% helium, 1% methane",
"atmosphere": "80% hydrogen, 19% helium, 1% methane",
"satellites": 14,
"type": "planet",
"moons": [
{
"name": "Triton",
"radius": 3.5,
"color": LIGHT_GREY,
"distance": 14,
"orbit_speed": 0.02,
"rotation_speed": 0.003,
"diameter": "2,710 km",
"temperature": "-235°C (average)",
"orbit_period": "5.88 days (retrograde)",
"rotation_period": "5.88 days (synchronous)",
"composition": "Water ice, rocky material",
"atmosphere": "Very thin (nitrogen, methane)"
}
]
}
}
# Create celestial body objects
class CelestialBody:
def __init__(self, name, data, parent=None):
self.name = name
self.radius = data["radius"]
self.color = data["color"]
self.distance = data["distance"]
self.orbit_speed = data["orbit_speed"]
self.rotation_speed = data["rotation_speed"]
self.angle = 0
self.rotation_angle = 0
self.parent = parent
self.data = data
# Calculate initial position
if parent:
self.x = parent.x + math.cos(self.angle) * self.distance
self.y = parent.y + math.sin(self.angle) * self.distance
else:
self.x = WIDTH // 2
self.y = HEIGHT // 2
def update(self):
if self.parent:
# Update orbit angle
self.angle += self.orbit_speed
# Update position relative to parent
self.x = self.parent.x + math.cos(self.angle) * self.distance
self.y = self.parent.y + math.sin(self.angle) * self.distance
else:
# Sun doesn't move
pass
# Update rotation angle
self.rotation_angle += self.rotation_speed
def draw(self, screen):
# Draw orbit path (except for the Sun)
if self.parent:
pygame.draw.circle(screen, WHITE, (int(self.parent.x), int(self.parent.y)), int(self.distance), 1)
# Draw the body
pygame.draw.circle(screen, self.color, (int(self.x), int(self.y)), self.radius)
# Draw rings for Saturn
if self.name == "Saturn":
pygame.draw.ellipse(screen, LIGHT_GREY,
(int(self.x - self.radius * 2.5), int(self.y - self.radius * 0.5),
int(self.radius * 5), int(self.radius * 1)), 1)
def is_clicked(self, pos):
distance = math.sqrt((pos[0] - self.x) ** 2 + (pos[1] - self.y) ** 2)
return distance <= self.radius
def show_info(self, screen):
# Create a semi-transparent background for the popup
popup_rect = pygame.Rect(50, 50, 400, 300)
popup_surface = pygame.Surface((popup_rect.width, popup_rect.height), pygame.SRCALPHA)
popup_surface.fill((0, 0, 0, 200)) # Black with alpha
screen.blit(popup_surface, popup_rect)
# Draw the title
title_text = title_font.render(f"{self.name} Information", True, WHITE)
screen.blit(title_text, (60, 60))
# Draw the information
info_lines = [
f"Type: {self.data.get('type', 'N/A')}",
f"Diameter: {self.data['diameter']}",
f"Temperature: {self.data['temperature']}",
f"Rotation Period: {self.data['rotation_period']}",
f"Composition: {self.data['composition']}",
f"Atmosphere: {self.data['atmosphere']}"
]
if self.data['type'] == 'planet':
info_lines.extend([
f"Orbit Period: {self.data['orbit_period']}",
f"Number of Satellites: {self.data['satellites']}"
])
else:
info_lines.extend([
f"Orbit Period: {self.data['orbit_period']}",
f"Parent Body: {self.parent.name}"
])
for i, line in enumerate(info_lines):
text = font.render(line, True, WHITE)
screen.blit(text, (60, 90 + i * 25))
# Create all celestial bodies
sun = CelestialBody("Sun", celestial_bodies["Sun"])
planets = [CelestialBody(name, data) for name, data in celestial_bodies.items() if name != "Sun"]
# Create moons for each planet
moons = []
for planet in planets:
for moon_data in planet.data["moons"]:
moon = CelestialBody(moon_data["name"], moon_data, planet)
moons.append(moon)
# Combine all bodies for easy updating and drawing
all_bodies = [sun] + planets + moons
# Main game loop
clock = pygame.time.Clock()
selected_body = None
running = True
while running:
# Handle events
for event in pygame.event.get():
if event.type == QUIT:
running = False
elif event.type == MOUSEBUTTONDOWN:
if event.button == 1: # Left mouse button
pos = pygame.mouse.get_pos()
selected_body = None
# Check if any body was clicked (check in reverse order to prioritize moons)
for body in reversed(all_bodies):
if body.is_clicked(pos):
selected_body = body
break
# Update all bodies
for body in all_bodies:
body.update()
# Draw everything
screen.fill(BLACK)
for body in all_bodies:
body.draw(screen)
# Draw info popup if a body is selected
if selected_body:
selected_body.show_info(screen)
# Display instructions
instructions = font.render("Click on a planet or moon to see its information", True, WHITE)
screen.blit(instructions, (WIDTH - 400, HEIGHT - 30))
pygame.display.flip()
clock.tick(60)
pygame.quit()
sys.exit()
Features of this Solar System Simulation:
-
Accurate Representation:
- Sun at the center with planets orbiting at different distances
- Moons orbiting their respective planets
- Saturn with its distinctive rings
-
Interactive Elements:
- Click on any celestial body to see detailed information
- Semi-transparent popup displays key data about the selected body
-
Comprehensive Information:
- Diameter of each body
- Temperature range
- Rotation period (around its axis)
- Orbit period (around parent body)
- Composition and atmospheric data
- Number of satellites (for planets)
- Parent body information (for moons)
-
Visual Elements:
- Color-coded celestial bodies
- Orbit paths shown as white circles
- Proper scaling of sizes and distances (though not to exact astronomical scale)
-
Technical Details:
- Uses Pygame for graphics and interaction
- Object-oriented design with CelestialBody class
- Smooth animation with proper rotation and orbit mechanics
To run this program, you'll need to have Python and Pygame installed. The simulation provides an educational tool for exploring our solar system with accurate astronomical data presented in an interactive format.