1+ import sys
2+ import math
3+ import pygame
4+ from pygame .locals import *
5+
6+ pygame .init ()
7+ WHITE = (255 , 255 , 255 )
8+ SILVER = (192 , 192 , 192 )
9+ BLACK = (0 , 0 , 0 )
10+ GREEN = (0 , 255 , 0 )
11+ RED = (255 , 0 , 0 )
12+ BLUE = (0 , 0 , 255 )
13+ YELLOW = (255 , 255 , 0 )
14+ SandyBrown = (244 , 164 , 96 )
15+ PaleGodenrod = (238 , 232 , 170 )
16+ PaleVioletRed = (219 , 112 , 147 )
17+ Thistle = (216 , 191 , 216 )
18+ size = width , height = 800 , 600
19+ screen = pygame .display .set_mode (size )
20+ pygame .display .set_caption ("太阳系" )
21+ # 创建时钟(控制游戏循环频率)
22+ clock = pygame .time .Clock ()
23+ # 定义三个空列表
24+ pos_v = pos_e = pos_mm = []
25+ # 地球、月球等行星转过的角度
26+ roll_v = roll_e = roll_m = 0
27+ roll_3 = roll_4 = roll_5 = roll_6 = roll_7 = roll_8 = 0
28+ # 太阳的位置(中心)
29+ position = size [0 ] // 2 , size [1 ] // 2
30+
31+ while True :
32+ for event in pygame .event .get ():
33+ if event .type == QUIT :
34+ sys .exit ()
35+ # 背景颜色为黑色
36+ screen .fill (BLACK )
37+ # 画太阳
38+ pygame .draw .circle (screen , YELLOW , position , 60 , 0 )
39+ # 画地球
40+ roll_e += 0.01 # 假设地球每帧公转 0.01 pi
41+ pos_e_x = int (size [0 ] // 2 + size [1 ] // 6 * math .sin (roll_e ))
42+ pos_e_y = int (size [1 ] // 2 + size [1 ] // 6 * math .cos (roll_e ))
43+ pygame .draw .circle (screen , BLUE , (pos_e_x , pos_e_y ), 15 , 0 )
44+ # 地球的轨迹线
45+ pos_e .append ((pos_e_x , pos_e_y ))
46+ if len (pos_e ) > 255 :
47+ pos_e .pop (0 )
48+ for i in range (len (pos_e )):
49+ pygame .draw .circle (screen , SILVER , pos_e [i ], 1 , 0 )
50+ # 画月球
51+ roll_m += 0.1
52+ pos_m_x = int (pos_e_x + size [1 ] // 20 * math .sin (roll_m ))
53+ pos_m_y = int (pos_e_y + size [1 ] // 20 * math .cos (roll_m ))
54+ pygame .draw .circle (screen , WHITE , (pos_m_x , pos_m_y ), 8 , 0 )
55+ # 月球的轨迹线
56+ pos_mm .append ((pos_m_x , pos_m_y ))
57+ if len (pos_mm ) > 255 :
58+ pos_mm .pop (0 )
59+ for i in range (len (pos_mm )):
60+ pygame .draw .circle (screen , SILVER , pos_mm [i ], 1 , 0 )
61+ # 画金星
62+ roll_v += 0.015
63+ pos_v_x = int (size [0 ] // 2 + size [1 ] // 3 * math .sin (roll_v ))
64+ pos_v_y = int (size [1 ] // 2 + size [1 ] // 3 * math .cos (roll_v ))
65+ pygame .draw .circle (screen , RED , (pos_v_x , pos_v_y ), 20 , 0 )
66+ # 金星的轨迹线
67+ pos_v .append ((pos_v_x , pos_v_y ))
68+ if len (pos_v ) > 255 :
69+ pos_v .pop (0 )
70+ for i in range (len (pos_v )):
71+ pygame .draw .circle (screen , SILVER , pos_v [i ], 1 , 0 )
72+ # 其他几个行星
73+ roll_3 += 0.03
74+ pos_3_x = int (size [0 ] // 2 + size [1 ] // 3.5 * math .sin (roll_3 ))
75+ pos_3_y = int (size [1 ] // 2 + size [1 ] // 3.5 * math .cos (roll_3 ))
76+ pygame .draw .circle (screen , GREEN , (pos_3_x , pos_3_y ), 20 , 0 )
77+ roll_4 += 0.04
78+ pos_4_x = int (size [0 ] // 2 + size [1 ] // 4 * math .sin (roll_4 ))
79+ pos_4_y = int (size [1 ] // 2 + size [1 ] // 4 * math .cos (roll_4 ))
80+ pygame .draw .circle (screen , SandyBrown , (pos_4_x , pos_4_y ), 20 , 0 )
81+ roll_5 += 0.05
82+ pos_5_x = int (size [0 ] // 2 + size [1 ] // 5 * math .sin (roll_5 ))
83+ pos_5_y = int (size [1 ] // 2 + size [1 ] // 5 * math .cos (roll_5 ))
84+ pygame .draw .circle (screen , PaleGodenrod , (pos_5_x , pos_5_y ), 20 , 0 )
85+ roll_6 += 0.06
86+ pos_6_x = int (size [0 ] // 2 + size [1 ] // 2.5 * math .sin (roll_6 ))
87+ pos_6_y = int (size [1 ] // 2 + size [1 ] // 2.5 * math .cos (roll_6 ))
88+ pygame .draw .circle (screen , PaleVioletRed , (pos_6_x , pos_6_y ), 20 , 0 )
89+ roll_7 += 0.07
90+ pos_7_x = int (size [0 ] // 2 + size [1 ] // 4.5 * math .sin (roll_7 ))
91+ pos_7_y = int (size [1 ] // 2 + size [1 ] // 4.5 * math .cos (roll_7 ))
92+ pygame .draw .circle (screen , Thistle , (pos_7_x , pos_7_y ), 20 , 0 )
93+ roll_8 += 0.08
94+ pos_8_x = int (size [0 ] // 2 + size [1 ] // 5.5 * math .sin (roll_8 ))
95+ pos_8_y = int (size [1 ] // 2 + size [1 ] // 5.5 * math .cos (roll_8 ))
96+ pygame .draw .circle (screen , WHITE , (pos_8_x , pos_8_y ), 20 , 0 )
97+ # 刷新
98+ pygame .display .flip ()
99+ # 数值越大刷新越快,小球运动越快
100+ clock .tick (40 )
0 commit comments