When it comes to the solar system, you may think of Copernicus and his heliocentric theory, or the champion Bruno who defended and developed the heliocentric theory. They lit up the night sky of that era like a ray of light. If you are interested in history, you can have a deeper understanding of it.
The solar system consists of the sun, 8 planets, 205 satellites and hundreds of thousands of asteroids. In this paper, we use Python to simply and dynamically simulate the operation of the solar system.
implementation
To achieve the function, we mainly need to go to the Python PyGame library. We import all the Python libraries we need first, the code is as follows:
import sys
import math
import pygame
from pygame.locals import *
Copy the code
Then define some constants (e.g., color, width and height) and create a window like this:
WHITE =(255, 255, 255) SILVER = (192, 192, 192) BLACK = (0, 0, 0) GREEN = (0, 255, 0) RED = (255, 0, 0) BLUE = (0, 0, 255) YELLOW = (255, 255, 0) SandyBrown = (244, 164, 96) PaleGodenrod = (238, 232, 170) PaleVioletRed = (219, 112, 147) Thistle = (216, 191, 216) size = width, height = 800, 600 Screen = Pygame.display. Set_mode (size) Pygame.display. Set_caption (" solar system" Pygame.time.clock () # define three empty lists pos_v = pos_e = pos_mm = [] # Rotation Angle of the earth, moon, etc. Roll_v = roll_e = roll_m = 0 roll_3 = roll_4 = Position = size[0] // 2, size[1] // 2Copy the code
Let’s start by drawing a sun in the window as follows:
pygame.draw.circle(screen, YELLOW, position, 60.0)
Copy the code
Take a look at the results:
Then draw a picture of the Earth orbiting the sun, as follows:
# to draw the earth
roll_e += 0.01 # Assume that the Earth rotates 0.01 PI per frame
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))
pygame.draw.circle(screen, BLUE, (pos_e_x, pos_e_y), 15.0)
# Earth's trajectory line
pos_e.append((pos_e_x, pos_e_y))
if len(pos_e) > 255:
pos_e.pop(0)
for i in range(len(pos_e)):
pygame.draw.circle(screen, SILVER, pos_e[i], 1.0)
Copy the code
Take a look at the results:
Let’s go ahead and draw the moon with the following code:
# to draw the moon
roll_m += 0.1
pos_m_x = int(pos_e_x + size[1] / /20 * math.sin(roll_m))
pos_m_y = int(pos_e_y + size[1] / /20 * math.cos(roll_m))
pygame.draw.circle(screen, SILVER, (pos_m_x, pos_m_y), 8.0)
# Trajectory line of the moon
pos_mm.append((pos_m_x, pos_m_y))
if len(pos_mm) > 255:
pos_mm.pop(0)
for i in range(len(pos_mm)):
pygame.draw.circle(screen, SILVER, pos_mm[i], 1.0)
Copy the code
Take a look at the results:
Several other planets have similar implementations, with the following code:
# Several other planets
roll_3 += 0.03
pos_3_x = int(size[0] / /2 + size[1] / /3.5 * math.sin(roll_3))
pos_3_y = int(size[1] / /2 + size[1] / /3.5 * math.cos(roll_3))
pygame.draw.circle(screen, GREEN, (pos_3_x, pos_3_y), 20.0)
roll_4 += 0.04
pos_4_x = int(size[0] / /2 + size[1] / /4 * math.sin(roll_4))
pos_4_y = int(size[1] / /2 + size[1] / /4 * math.cos(roll_4))
pygame.draw.circle(screen, SandyBrown, (pos_4_x, pos_4_y), 20.0)
roll_5 += 0.05
pos_5_x = int(size[0] / /2 + size[1] / /5 * math.sin(roll_5))
pos_5_y = int(size[1] / /2 + size[1] / /5 * math.cos(roll_5))
pygame.draw.circle(screen, PaleGodenrod, (pos_5_x, pos_5_y), 20.0)
roll_6 += 0.06
pos_6_x = int(size[0] / /2 + size[1] / /2.5 * math.sin(roll_6))
pos_6_y = int(size[1] / /2 + size[1] / /2.5 * math.cos(roll_6))
pygame.draw.circle(screen, PaleVioletRed, (pos_6_x, pos_6_y), 20.0)
roll_7 += 0.07
pos_7_x = int(size[0] / /2 + size[1] / /4.5 * math.sin(roll_7))
pos_7_y = int(size[1] / /2 + size[1] / /4.5 * math.cos(roll_7))
pygame.draw.circle(screen, Thistle, (pos_7_x, pos_7_y), 20.0)
roll_8 += 0.08
pos_8_x = int(size[0] / /2 + size[1] / /5.5 * math.sin(roll_8))
pos_8_y = int(size[1] / /2 + size[1] / /5.5 * math.cos(roll_8))
pygame.draw.circle(screen, WHITE, (pos_8_x, pos_8_y), 20.0)
Copy the code
Finally, let’s look at the dynamic effect of the overall implementation:
Is there an inside smell?
conclusion
In this article, we used Python to simulate the operation of the solar system. If you are interested, you can run the code or make further extensions to the function.
The source code is available in Python 200812.
This article was not originally published in the personal journal