Python Pygame Solar System Simulation Tutorial
This tutorial demonstrates how to use Python and the Pygame library to create a real‑time visual simulation of the solar system, complete with background images, rotating planet sprites, optional music, and frame‑rate control, providing a hands‑on example of basic game‑style graphics programming.
The article walks through building a simple solar‑system animation with Python and Pygame, showing how to load images, render text, play background music, and animate planets orbiting the Sun.
Preparation : Gather a space‑background picture and transparent PNGs for the Sun, Mercury, Venus, Earth, Moon, Mars, Jupiter, Saturn, Uranus, and Neptune. All assets are loaded from local file paths.
Import required modules :
import pygame
import sys
import math
from pygame.locals import *Initialize the window and fonts :
size = width, height = 1206, 780
screen = pygame.display.set_mode(size)
pygame.display.set_caption("太阳系行星运转示意图")
myfont = pygame.font.Font(None, 60)
clock = pygame.time.Clock()Load the background image (replace the placeholder with the actual path):
background = pygame.image.load(r"这里填背景图片本地路径")
screen.blit(background, (0, 0))Add optional background music :
pygame.mixer.music.load('F:/music.mp3')
pygame.mixer.music.play(-1, 40)
pygame.mixer.music.set_volume(0.5)Main loop – handles events, draws the scene, updates planet positions, and refreshes the display:
while True:
for event in pygame.event.get():
if event.type == QUIT:
sys.exit()
# draw background
background = pygame.image.load(r"E:/3.PNG")
screen.blit(background, (0, 0))
# render title text
textImage = myfont.render("Solar System", True, (255, 255, 0))
screen.blit(textImage, (100, 100))
# draw Sun and planet labels (example for Sun)
my_font = pygame.font.SysFont("arial", 15)
text_surface = my_font.render("Sun", True, (255, 0, 0), (0, 0, 0))
screen.blit(text_surface, (1020, 30))
sun = pygame.image.load(r"F:/solar-system/image/sun_bg.png")
screen.blit(pygame.transform.scale(sun, (170, 170)), (527, 307))
# ... (similar code for other planets) ...
# update display and control frame rate
pygame.display.flip()
clock.tick(50)Planet orbital calculations (illustrated for Mercury, Venus, Earth and Moon):
# Mercury
roll_3 += 0.077
pos_3_x = int(size[0] // 2 + size[1] // 8 * math.sin(roll_3))
pos_3_y = int(size[1] // 2 + size[1] // 8 * math.cos(roll_3))
mercury = pygame.image.load(r"F:/solar-system/image/mercury_bg.png")
screen.blit(pygame.transform.scale(mercury, (8, 8)), (pos_3_x, pos_3_y))
# Venus
roll_2 += 0.069
pos_2_x = int(size[0] // 2 + size[1] // 7 * math.sin(roll_2))
pos_2_y = int(size[1] // 2 + size[1] // 7 * math.cos(roll_2))
venus = pygame.image.load(r"F:/solar-system/image/venus_bg.png")
screen.blit(pygame.transform.scale(venus, (10, 10)), (pos_2_x, pos_2_y))
# Earth
roll_e += 0.060
pos_e_x = int(size[0] // 2 + size[1] // 6 * math.sin(roll_e))
pos_e_y = int(size[1] // 2 + size[1] // 6 * math.cos(roll_e))
earth = pygame.image.load(r"F:/solar-system/image/earth_min_bg.png")
screen.blit(pygame.transform.scale(earth, (15, 15)), (pos_e_x, pos_e_y))
# Moon (orbiting Earth)
roll_m += 0.2
pos_m_x = int(pos_e_x + size[1] // 50 * math.sin(roll_m))
pos_m_y = int(pos_e_y + size[1] // 50 * math.cos(roll_m))
moon = pygame.image.load(r"F:/solar-system/image/mercury_bg.png")
screen.blit(pygame.transform.scale(moon, (6, 6)), (pos_m_x, pos_m_y))By combining these elements, the script produces a continuously animated view of the planets revolving around the Sun, with the Moon revolving around Earth, and optional background music to enhance the experience.
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.