-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
343 lines (285 loc) · 15.7 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
import pygame
import math
import random
from planets import planet_list
from planets import star_list
pygame.init()
WIDTH, HEIGHT = 1280, 800
win = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Solar System Simulation")
FONT = pygame.font.SysFont("Consolas", 18)
PAUSE_FONT = pygame.font.SysFont("Consolas", 24)
LEGEND_TEXTS = ["P Pause", "M Mute", "I Info off", "Q Quit",
"0 Sun", "1 Mercury", "2 Venus", "3 Earth", "4 Mars", "5 Jupiter", "6 Saturn", "7 Uranus", "8 Neptune"]
BG_MUSIC = pygame.mixer.Sound("stay.mp3")
BG_MUSIC.set_volume(0.9)
BG_MUSIC.play(loops=-1)
# You can use this fuction for moving stars, call this function at 104 and remove line 104 and 105
# def draw_stars():
# for _ in range(20): # Adjust the number of stars as needed
# x = random.randint(0, WIDTH)
# y = random.randint(0, HEIGHT)
# pygame.draw.circle(win, (255, 255, 255), (x, y), 1) # Small white circle for each star
# Main function
class Comet:
def __init__(self):
self.x = random.randint(0, WIDTH)
self.y = random.randint(0, HEIGHT)
self.radius = 1
self.color = (0, 255, 0) # Green color
self.speed = 6 # Adjust the speed as needed
self.direction = random.choice(['left', 'right', 'up', 'down'])
self.initialize_position()
def initialize_position(self):
if self.direction == 'left':
self.x = WIDTH
self.y = random.randint(0, HEIGHT)
elif self.direction == 'right':
self.x = 0
self.y = random.randint(0, HEIGHT)
elif self.direction == 'up':
self.x = random.randint(0, WIDTH)
self.y = HEIGHT
elif self.direction == 'down':
self.x = random.randint(0, WIDTH)
self.y = 0
def move(self):
if self.direction == 'left':
self.x -= self.speed
elif self.direction == 'right':
self.x += self.speed
elif self.direction == 'up':
self.y -= self.speed
elif self.direction == 'down':
self.y += self.speed
def draw_comet(self):
pygame.draw.circle(win, self.color, (self.x, self.y), self.radius)
def main():
clock = pygame.time.Clock()
running = True
paused = False
displayed_planet = None
time_passed = 0
pause_text = ""
muted = False
comet_interval = 3000 # 3 seconds in milliseconds
comet_timer = 0
comet = None
while running:
dt = clock.tick(60)
# Handle events
for event in pygame.event.get():
# If user quits it breaks out of the loop
if event.type == pygame.QUIT:
running = False
# Handle key presses
if event.type == pygame.KEYDOWN:
# If P is pressed pause/unpause
if event.key == pygame.K_p:
if paused:
paused = False
elif not paused:
paused = True
pause_text = PAUSE_FONT.render("PAUSED", 1, (255, 255, 255))
win.blit(pause_text, (WIDTH / 2 - pause_text.get_width() / 2, 20))
pygame.display.update()
# Check if user pressed the keys 1-8
if pygame.K_0 <= event.key <= pygame.K_8:
# Shift it by 48 to the lef tomatch their original indexes
# (K_0 ASCII value is 48, planets start from index 0, hence minus 48)
planet_index = event.key - pygame.K_0 # 48
displayed_planet = planet_list[planet_index]
# Toggle info text off if displayed
if event.key == pygame.K_i:
if displayed_planet:
displayed_planet = None
# Quit program when Q is pressed
if event.key == pygame.K_q:
running = False
# Mute background music when M is pressed
if event.key == pygame.K_m:
if muted:
BG_MUSIC.set_volume(0.3)
muted = False
elif not muted:
BG_MUSIC.set_volume(0)
muted = True
# Toggle information off with mouse click
if event.type == pygame.MOUSEBUTTONDOWN:
if displayed_planet:
displayed_planet = None
comet_timer += dt
if comet_timer >= comet_interval:
comet = [ Comet() for _ in range(3)]
comet_timer = 0
# Calculate gravitational forces exerted on planets
if not paused:
# Keep track of the time passed since start of the simulation to be displayed later
time_passed += planet_list[0].TIMESTEP
# Calculate force for each of the planets and move them in their orbit
for planet in planet_list:
planet.calculate_gravitational_force(planet_list)
# Fill the window before drawing the planets to avoid flickering
win.fill((0, 0, 0))
if comet:
for c in comet:
c.move()
c.draw_comet()
for s in star_list:
s.show(win)
# Draw planets
for planet in planet_list:
x, y = planet.draw_planet()
pygame.draw.circle(win, planet.color, (x, y), planet.radius)
# Draw legend to the top right corner of the screen
text_x = WIDTH - 105
text_y = 20
for legend_text in LEGEND_TEXTS:
# Differentiate with color if any celestial body is selected
if displayed_planet:
if displayed_planet.name.capitalize() in legend_text:
text = FONT.render(legend_text, 1, displayed_planet.color)
else:
text = FONT.render(legend_text, 1, (255, 255, 255))
win.blit(text, (text_x, text_y))
text_y += 30
else:
text = FONT.render(legend_text, 1, (255, 255, 255))
win.blit(text, (text_x, text_y))
text_y += 30
# User can also display info texts by hovering mouse on planets
for planet in planet_list:
# Get mouse position
mouse_x, mouse_y = pygame.mouse.get_pos()
# Get distance between mouse and the planet (scaled down)
distance = math.sqrt((planet.x * planet.SCALE + 1280 / 2 - mouse_x) ** 2 + (planet.y * planet.SCALE + 800 / 2 - mouse_y) ** 2)
if distance <= planet.radius and not paused:
idx = planet_list.index(planet)
displayed_planet = planet_list[idx]
# If one of 1-8 keys is pressed information text is blitted
if displayed_planet:
if displayed_planet.name != "Sun":
# Set variables to be shown
days_passed = time_passed / (3600 * 24)
days_left = int(displayed_planet.orbit_time - days_passed)
velocity = math.sqrt(displayed_planet.x_vel ** 2 + displayed_planet.y_vel ** 2) / 1000
distance_to_sun = displayed_planet.distance_to_sun / 1000
days_left_for_full_orbit = days_left % displayed_planet.orbit_time
years_passed_since_start = days_passed / displayed_planet.orbit_time
# Blit the info text to the top left corner of the screen
planet_info_text_1 = FONT.render(f"{displayed_planet.name}", 1, displayed_planet.color)
lines = [
f"Mass: {displayed_planet.mass} kg",
f"Radius: {displayed_planet.actual_radius:,} km",
f"Gravity: {displayed_planet.gravity} m/s^2",
f"Mean Temperature: {displayed_planet.mean_temp} °C",
f"Velocity: {velocity:.1f} km/s",
f"Distance to Sun: {distance_to_sun:,.1f} km",
f"Days left for one full orbit: {days_left_for_full_orbit} days",
f"Time passed since start: {years_passed_since_start:.2f} {displayed_planet.name} years",
"Symbol: "
]
# Render each line using a loop
rendered_lines = [FONT.render(line, 1, (255, 255, 255)) for line in lines]
# Combine the rendered lines into a single surface
planet_info_text_2 = pygame.Surface((max(line.get_width() for line in rendered_lines), sum(line.get_height() for line in rendered_lines)))
y_offset = 0
for line in rendered_lines:
planet_info_text_2.blit(line, (0, y_offset))
y_offset += line.get_height()
source_text = FONT.render("Source: NASA", 1, (255, 255, 255))
win.blit(planet_info_text_1, (10, 20))
win.blit(planet_info_text_2, (10, 40))
# Blit image to the bottom left corner of the screen
win.blit(source_text, (10, HEIGHT - 20))
win.blit(pygame.transform.scale(pygame.image.load(f"assets/images/planets//{displayed_planet.name.lower()}.jpg"), (250, 250)),
(0, HEIGHT - 270))
# Blit astronomical symbol of the planet as an image
win.blit(pygame.image.load(f"assets/images/symbols//{displayed_planet.name.lower()}.jpg"), (85, planet_info_text_2.get_height() + 20))
elif displayed_planet.name == "Sun":
days_passed = int(time_passed / (3600 * 24))
sun = displayed_planet
sun_info_text_1 = FONT.render("Sun", 1, sun.color)
sun_lines = [
f"Mass: {sun.mass} kg",
f"Radius: {sun.actual_radius:,} km",
f"Gravity (Surface): {sun.gravity} m/s^2",
f"Mean Temperature (Surface): {sun.mean_temp} °C",
f"Time passed since start: {days_passed} Sun days",
"Symbol: "
]
sun_rendered_lines = [FONT.render(line, 1, (255, 255, 255)) for line in sun_lines]
# Combine the rendered lines into a single surface for sun_info_text_2
sun_info_text_2 = pygame.Surface((max(line.get_width() for line in sun_rendered_lines), sum(line.get_height() for line in sun_rendered_lines)))
y_offset = 0
for line in sun_rendered_lines:
sun_info_text_2.blit(line, (0, y_offset))
y_offset += line.get_height()
source_text = FONT.render("Source: NASA", 1, (255, 255, 255))
win.blit(sun_info_text_1, (10, 20))
win.blit(sun_info_text_2, (10, 40))
# Blit Sun image to the bottom left corner of the screen
win.blit(source_text, (10, HEIGHT - 20))
win.blit(pygame.transform.scale(pygame.image.load("assets/images/planets//sun.jpg"), (250, 250)), (0, HEIGHT - 270))
# Blit astronomical symbol of Sun as an image
win.blit(pygame.image.load("assets/images/symbols//sun.jpg"), (85, sun_info_text_2.get_height() + 20))
# Draw a little circle around the planet to indicate it is selected
# Get planet's coordinates to the scale
x = int(displayed_planet.x * displayed_planet.SCALE + 1280 / 2)
y = int(displayed_planet.y * displayed_planet.SCALE + 800 / 2)
pygame.draw.circle(win, (255, 255, 255), (x, y), displayed_planet.radius + 5, width=1)
# Draw selected planet's orbit when it is selected
# Orbit will be shown as a regular circle whose radius is changed with the planet's distance to the Sun
current_x_distance = (displayed_planet.x - planet_list[0].x)
current_y_distance = (displayed_planet.y - planet_list[0].y)
center_x = int(planet_list[0].x * displayed_planet.SCALE + 1280 / 2)
center_y = int(planet_list[0].y * displayed_planet.SCALE + 800 / 2)
current_distance = math.sqrt(current_x_distance ** 2 + current_y_distance ** 2) * displayed_planet.SCALE
pygame.draw.circle(win, displayed_planet.color, (center_x, center_y), current_distance, width=1)
# If Uranus or Neptune gets out of the screen user is informed
if displayed_planet.name == "Neptune":
neptune = displayed_planet
neptune_out_up = neptune.y * neptune.SCALE + 800 / 2 < 0
neptune_out_down = neptune.y * neptune.SCALE + 800 / 2 > 800
if neptune_out_up or neptune_out_down:
text = FONT.render("Neptune is out of screen now", 1, neptune.color)
win.blit(text, (10, planet_info_text_2.get_height() + 40))
if neptune_out_up:
line_start = (WIDTH / 2, 50)
line_end = (neptune.x * neptune.SCALE + 1280 / 2, neptune.y * neptune.SCALE + 800 / 2)
neptune_indicator = FONT.render("Neptune", 1, (255, 255, 255))
pygame.draw.line(win, (255, 255, 255), line_start, line_end)
win.blit(neptune_indicator, (line_start[0] - neptune_indicator.get_width() / 2, line_start[1] + 20))
elif neptune_out_down:
line_start = (WIDTH / 2, HEIGHT - 50)
line_end = (neptune.x * neptune.SCALE + 1280 / 2, neptune.y * neptune.SCALE + 800 / 2)
neptune_indicator = FONT.render("Neptune", 1, (255, 255, 255))
pygame.draw.line(win, (255, 255, 255), line_start, line_end)
win.blit(neptune_indicator, (line_start[0] - neptune_indicator.get_width() / 2, line_start[1] - 20))
elif displayed_planet.name == "Uranus":
uranus = displayed_planet
uranus_out_up = uranus.y * uranus.SCALE + 800 / 2 < 0
uranus_out_down = uranus.y * uranus.SCALE + 800 / 2 > 800
if uranus_out_up or uranus_out_down:
text = FONT.render("Uranus is out of screen now", 1, uranus.color)
win.blit(text, (10, planet_info_text_2.get_height() + 40))
if uranus_out_up:
line_start = (WIDTH / 2, 50)
line_end = (uranus.x * uranus.SCALE + 1280 / 2, uranus.y * uranus.SCALE + 800 / 2)
uranus_indicator = FONT.render("Uranus", 1, (255, 255, 255))
pygame.draw.line(win, (255, 255, 255), line_start, line_end)
win.blit(uranus_indicator, (line_start[0] - neptune_indicator.get_width() / 2, line_start[1] + 20))
elif uranus_out_down:
line_start = (WIDTH / 2, HEIGHT - 50)
line_end = (uranus.x * uranus.SCALE + 1280 / 2, uranus.y * uranus.SCALE + 800 / 2)
uranus_indicator = FONT.render("Uranus", 1, (255, 255, 255))
pygame.draw.line(win, (255, 255, 255), line_start, line_end)
win.blit(uranus_indicator, (line_start[0] - uranus_indicator.get_width() / 2, line_start[1] - 20))
if not paused:
# Display scale info to the bottom right corner of the screen
scale_info_text = FONT.render("Distances are to scale, sizes are not to scale.", 1, (255, 255, 255))
win.blit(scale_info_text, (WIDTH - scale_info_text.get_width(), HEIGHT - scale_info_text.get_height()))
pygame.display.update()
pygame.quit()
if __name__ == "__main__":
main()