-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEPQ_Solar_System.py
More file actions
887 lines (679 loc) · 39.1 KB
/
EPQ_Solar_System.py
File metadata and controls
887 lines (679 loc) · 39.1 KB
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
import os
import pygame
import math
pygame.init() # 1) Initialising the module pygame
# Loading images of planets
ASSETS = os.path.join(os.path.dirname(__file__), "assets")
run = True
mercury_img = pygame.image.load(os.path.join(ASSETS, "Mercury_img.png"))
mercury_img = pygame.transform.scale(mercury_img, (40, 40))
mercury_dimension = 40
venus_img = pygame.image.load(os.path.join(ASSETS, "Venus_img.png"))
venus_img = pygame.transform.scale(venus_img, (46, 46))
venus_dimension = 30
earth_img = pygame.image.load(os.path.join(ASSETS, "Earth_img.png"))
earth_img = pygame.transform.scale(earth_img, (50, 50))
earth_dimension = 50
mars_img = pygame.image.load(os.path.join(ASSETS, "Mars_img.png"))
mars_img = pygame.transform.scale(mars_img, (45, 45))
mars_dimension = 40
sun_img = pygame.image.load(os.path.join(ASSETS, "Sun_img.png"))
sun_img = pygame.transform.scale(sun_img, (60, 60))
total_time = 0
# Setup pygame window
background_img = pygame.image.load(os.path.join(ASSETS, "backgroundStars3.webp"))
background_img = pygame.transform.scale(background_img, (800, 800))
WIDTH, HEIGHT = 800, 800
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Solar System simulation using Kepler's laws")
LIGHT_GREY = (128, 128, 128)
WHITE = (255, 255, 255)
YELLOW = (255, 255, 0)
DARK_YELLOW = (193, 143, 23)
BLUE = (100, 149, 237)
RED = (188, 39, 50)
DARK_GREY = (255, 0, 255)
FONT = pygame.font.SysFont("arial", 18)
class Planet:
#Class Variables
AU = 1.496e11 #1 Astronomical Unit = 149,600,000,000 metres
G = 6.67428e-11 #Gravitational Constant
SCALE = 250 / AU # 250/ AU (1 astronomical unit is 250 pixels)
TIMESTEP = 3600 * 24 # How much time I want to simulate is being elapsed
def __init__(self, x, y, radius, colour, mass, avg_dist = None, orbital_period=None, image=None, dimension=None,name=None):
#Instance Variables
self.x = x
self.y = y
self.radius = radius
self.colour = colour
self.mass = mass
self.average_distance_to_sun = avg_dist
self.image = image
self.dimension = dimension
self.name = name
self.conjunctions = []
self.orbital_period = orbital_period
self.aphelions = []
self.perihelions = []
self.orbit = [] # Keeps track of all the points this planet has travelled along so the orbit can be drawn
self.sun = False # We need to draw the orbit of planets around the sun so we need to identify what is not the sun.
self.distance_to_sun = 0
self.distances_to_sun = [] #Stores the distance to the sun at each point in the orbit
self.distances_to_sun_standardform = [] #Stores the distances to the sun in standard form
self.x_vel = 0
self.y_vel = 0
def draw(self, win, total_time):
x_pos = self.x * self.SCALE + WIDTH / 2 #Scales the x coordinate to the window
y_pos = self.y * self.SCALE + HEIGHT / 2 #Scales the y coordinate to the window
if len(self.orbit) > 2:
updated_points = [] # list of updated points to scale in x and y coordinates.
# Draws the orbital path by iterating through each point in the orbit
# Each point is scaled to the window and added to a list of scaled points
# A line is drawn between each point, creating the orbital path
for point in self.orbit:
x, y = point
x = x * self.SCALE + WIDTH / 2
y = y * self.SCALE + HEIGHT / 2
updated_points.append((x, y))
pygame.draw.lines(win, self.colour, False, updated_points, 2)
# if there is no image, the planet is drawn as a circle
if self.image == None:
pygame.draw.circle(win, self.colour, (x_pos, y_pos), self.radius)
# else draw the image of the planet at its current x and y coordinates
else:
WIN.blit(self.image, (x_pos - 10, y_pos - 10))
# If the object is not the sun, display its distance from the sun
if not self.sun:
dist_sun = self.distances_to_sun_standardform[total_time - 1]
distance_text = FONT.render(f" {(dist_sun)} ", 1, WHITE)
win.blit(distance_text, (x_pos - (distance_text.get_width() / 2), y_pos - ((distance_text.get_height() / 2) + 5)))
# Method that can calculate the force of attraction between objects
def attraction(self, other): # Takes in two bodies (both can be planets or 1 planet and the Sun)
# Calculates distance between the two bodies
other_x, other_y = other.x, other.y
distance_x = other_x - self.x # X distance between each body
distance_y = other_y - self.y # Y distance between each body
distance = math.sqrt(distance_x ** 2 + distance_y ** 2)
#Converts the distance into standard form
base = distance
power = 0
while base > 10:
base = base / 10
power += 1
base = round(base, 3)
standard_form = str(base) + " * 10^" + str(power) + " m"
# Determines if the other object is the sun so self must be a planet
if other.sun:
self.distance_to_sun = distance
self.distances_to_sun.append(distance)
self.distances_to_sun_standardform.append(standard_form)
# Calculates Force of attraction using Newton's law of universal gravitation
force = self.G * self.mass * other.mass / distance ** 2
# Calculates theta - the angle between the two distances
theta = math.atan2(distance_y, distance_x) # atan2 takes y over x and returns the angle
# Calculates x and y component of force
force_x = math.cos(theta) * force
force_y = math.sin(theta) * force
return force_x, force_y
# Updates the position of each planet based on the force of attraction with every other planet and the Sun
# Loop through each planet, calculate the force of attraction between the current planet and all the other planets...
# ... as well as the sun. Then calculates what the velocity is and moves them by that velocity
def update_position(self, planets):
global total_time
# Total forces exerted on the planet from all other planets that are not itself
total_fx = total_fy = 0
for planet in planets:
if self == planet: #No force of attraction for the same planet
continue
fx, fy = self.attraction(planet) #Attraction method returns force_x and force_y
total_fx += fx
total_fy += fy
# Uses F = ma to find acceleration and then multiplies a by time passed to get velocity
self.x_vel += total_fx / self.mass * self.TIMESTEP
self.y_vel += total_fy / self.mass * self.TIMESTEP
# Update the y and x position given the velocity for y and x
self.x += self.x_vel * self.TIMESTEP
self.y += self.y_vel * self.TIMESTEP
self.orbit.append((self.x, self.y))
def Find_conjunction(self, earth, sun, total_time):
for day in range(total_time - 1):
# Coordinates of the inferior planet
planet_x, planet_y = self.orbit[day]
planet_coords = [planet_x, planet_y]
# Coordinates of the Earth
earth_x, earth_y = earth.orbit[day]
earth_coords = [earth_x, earth_y]
# Coordinates of the sun
sun_x, sun_y = sun.orbit[day]
sun_coords = [sun_x, sun_y]
# Skip if planet is directly above or below the sun (would cause division by zero)
if planet_x == sun_x or earth_x == sun_x:
continue
# Gradient of the line between the inferior planet and the sun and the earth and the sun
planet_sun_gradient = (planet_y - sun_y) / (planet_x - sun_x)
earth_sun_gradient = (earth_y - sun_y) / (earth_x - sun_x)
# Earth and (mercury or Venus)'s distance to the sun
earth_sun_dist = earth.distances_to_sun[day]
planet_sun_dist = self.distances_to_sun[day]
# Earth to Mercury or Venus distance
earth_planet_x_dist = earth_x - planet_x
earth_planet_y_dist = earth_y - planet_y
earth_planet_dist = math.sqrt(earth_planet_x_dist ** 2 + earth_planet_y_dist ** 2)
# Initialises the boolean flags to check whether the conjunction is inferior or superior
superior = False
inferior = False
#Checks if the Earth, planet and Sun lie on the same line, if so a conjunction has occurred
if round(planet_sun_gradient, 2) == round(earth_sun_gradient, 2):
# Checks whether the conjunction is superior or inferior
if earth_sun_dist > earth_planet_dist:
inferior = True
else:
superior = True
information = [day + 1, planet_coords, earth_coords, sun_coords]
if inferior == True:
information.append("Inferior")
else:
information.append("Superior")
self.conjunctions.append(information)
def show_conjunction(self, win, earth, sun):
#Initialises the font to be used later
FONTtwo = pygame.font.SysFont("Comic Sans MS", 30)
#iterates through each "information" list appended to self.conjunctions
for conjunction in self.conjunctions:
next = False
# Boolean variable to keep the window running until condition satisfied
while next == False:
for event in pygame.event.get(): # Checks all inputs from the user
if event.type == pygame.QUIT: # Checks if the user pressed the X on the window
next = True
return None # Quits the method and returns control to wherever method was called
if event.type == pygame.MOUSEBUTTONDOWN: # Checks if the user pressed mouse anywhere on screen
next = True # While loop is exited and for loop is incremented to point to next conjunction
win.fill((0, 0, 0)) # Wipes and resets the window
win.blit(background_img, (0, 0))
times = conjunction[0] # Day that the conjunction occurred
planet_x, planet_y = conjunction[1] # Planet's x and y coordinate the day the conjunction occurred
earth_x, earth_y = conjunction[2] # Earth's x and y coordinates the day the conjunction occurred
sun_x, sun_y = conjunction[3] # Sun's x and y coordinates the day the conjunction occurred
conjunction_type = conjunction[4] # Type of conjunction that is taking place
# Scales the coordinates to the window. 1AU = 250 pixels
planet_x = planet_x * self.SCALE + WIDTH / 2
planet_y = planet_y * self.SCALE + WIDTH / 2
earth_x = earth_x * self.SCALE + WIDTH / 2
earth_y = earth_y * self.SCALE + WIDTH / 2
sun_x = sun_x * self.SCALE + WIDTH / 2
sun_y = sun_y * self.SCALE + WIDTH / 2
# Decides the text that needs to be rendered on screen depending on type of conjunction
if conjunction_type == "Opposition":
time_keeper = FONTtwo.render(self.name + "'s " + conjunction_type + ": Day " + str(times), 1, WHITE)
else:
time_keeper = FONTtwo.render(self.name + "'s " + conjunction_type + " Conjunction: Day " + str(times), 1, WHITE)
# Displays the date of conjunction, planet, Earth and Sun onto the screen
win.blit(time_keeper, (0, 0))
win.blit(self.image, (planet_x, planet_y))
win.blit(earth.image, (earth_x, earth_y))
win.blit(sun.image, (sun_x, sun_y))
pygame.display.update() #Updates the window so that changes can be displayed
def Find_and_Show_aphelion(self, total_time, win, sun,mercury, venus, mercury_elongations, venus_elongations, earth, mars):
# Initialises the variables for the furthest distance to the sun and time aphelion occurred
furthest = 0
time_aphelion = 0
# Iterates through each point in the planet's orbit
for day in range(total_time - 1):
# Aphelion should occur at same position and time each year
# Therefore not necessary to include the other dates that aphelion occurred
if day > self.orbital_period:
break
# Checks if this point in orbit is further from the sun than ...
# ... the current furthest position from the sun
# If so date, position of sun and planet are noted
if self.distances_to_sun[day] > furthest:
furthest = self.distances_to_sun[day]
sun_x, sun_y = sun.orbit[day]
planet_x, planet_y = self.orbit[day]
time_aphelion = (day + 1)
win.fill((0, 0, 0)) # Wipes and resets the window
win.blit(background_img, (0, 0))
# Scaling the x and y coordinates of the planet and sun to the window
planet_x = planet_x * self.SCALE + WIDTH / 2
planet_y = planet_y * self.SCALE + WIDTH / 2
sun_x = sun_x * self.SCALE + WIDTH / 2
sun_y = sun_y * self.SCALE + WIDTH / 2
average_sun_dist = self.average_distance_to_sun + " m"
furthest = self.distances_to_sun_standardform[time_aphelion - 1]
# Initialises the fonts to be used
FONTone = pygame.font.SysFont("Arial", 16)
FONTtwo = pygame.font.SysFont("Comic Sans MS", 30)
#The date of aphelion, planet's furthest distance to the sun and average distance to the sun ...
# ... are displayed in the top left of the screen. The furthest distance to the sun is also plot on the planet
time_keeper = FONTtwo.render(self.name + "'s aphelion: Day " + str(time_aphelion), 1, WHITE)
distance_text = FONTone.render(str(furthest), 1, WHITE)
furthest_text = FONTtwo.render(self.name + "'s furthest distance to sun: " + str(furthest), 1, WHITE)
average_dist = FONTtwo.render(self.name + "'s average distance to sun: " + average_sun_dist, 1, WHITE)
next = False
while next == False:
for event in pygame.event.get(): # Checks all the inputs from the user
# Checks if the user pressed the X on the window
# If they did then the current window closes and the menu window opens
if event.type == pygame.QUIT:
next = True
menu(win, mercury, venus, mercury_elongations, venus_elongations, earth, sun, total_time, mars)
if event.type == pygame.MOUSEBUTTONDOWN:
next = True
# Window wiped, images of the background, sun and planet are plot on screen
# The distances are also plot on the screen
win.fill((0, 0, 0))
win.blit(background_img, (0, 0))
win.blit(sun.image, (sun_x, sun_y))
win.blit(self.image, (planet_x - 5, planet_y - 30))
win.blit(distance_text, (planet_x, planet_y - 30))
win.blit(time_keeper, (0, 0))
win.blit(average_dist, (0, 30))
win.blit(furthest_text, (0, 60))
pygame.display.update()
def Find_and_Show_perihelion(self, total_time, win, sun, mercury, venus, mercury_elongations, venus_elongations, earth, mars):
closest = self.distance_to_sun * 100 # Sets the comparison variable to a distance larger than possible
time_perihelion = 0 # Sets the variable for the date that the perihelion will occur
for day in range(total_time - 1):
if day > self.orbital_period:
break
if self.distances_to_sun[day] < closest:
closest = self.distances_to_sun[day]
sun_x, sun_y = sun.orbit[day]
planet_x, planet_y = self.orbit[day]
time_perihelion = (day + 1)
win.fill((0, 0, 0))
win.blit(background_img, (0, 0))
# Scaling the x and y coordinates of the planet and sun to the window
planet_x = planet_x * self.SCALE + WIDTH / 2
planet_y = planet_y * self.SCALE + WIDTH / 2
sun_x = sun_x * self.SCALE + WIDTH / 2
sun_y = sun_y * self.SCALE + WIDTH / 2
average_sun_dist = self.average_distance_to_sun + " m"
# Each day is output on the screen
closest = self.distances_to_sun_standardform[time_perihelion - 1]
FONTone = pygame.font.SysFont("Arial", 15)
FONTtwo = pygame.font.SysFont("Comic Sans MS", 30)
time_keeper = FONTtwo.render(self.name + "'s perihelion: Day " + str(time_perihelion), 1, WHITE)
distance_text = FONTone.render(str(closest), 1, WHITE)
furthest_text = FONTtwo.render(self.name + "'s closest distance to sun: " + str(closest), 1, WHITE)
average_dist = FONTtwo.render(self.name + "'s average distance to sun: " + average_sun_dist, 1, WHITE)
clock = pygame.time.Clock()
next = False
while next == False:
clock.tick(30)
for event in pygame.event.get():
if event.type == pygame.QUIT:
next = True
menu(win, mercury, venus, mercury_elongations, venus_elongations, earth, sun, total_time, mars)
if event.type == pygame.MOUSEBUTTONDOWN:
next = True
win.fill((0, 0, 0))
win.blit(background_img, (0, 0))
win.blit(sun.image, (sun_x, sun_y))
win.blit(self.image, (planet_x - 5, planet_y - 20))
win.blit(distance_text, (planet_x, planet_y - 20))
win.blit(time_keeper, (0, 0))
win.blit(average_dist, (0, 30))
win.blit(furthest_text, (0, 60))
pygame.display.update()
# Subclass for superior planets
class Superiorplanet(Planet):
def __init__(self, x, y, radius, colour, mass, average_distance_to_sun, orbital_period, image=None, dimension=None, name=None,
conjunctions=None):
super().__init__(x, y, radius, colour, mass, average_distance_to_sun, orbital_period, image, dimension, name)
self.conjunctions = []
self.quadratures = []
def Find_superior_conjunction_opposition(self, earth, sun, total_time):
for day in range(total_time - 1):
# Coordinates of the superior planet
planet_x, planet_y = self.orbit[day]
planet_coords = [planet_x, planet_y]
# Coordinates of the Earth
earth_x, earth_y = earth.orbit[day]
earth_coords = [earth_x, earth_y]
# Coordinates of the sun
sun_x, sun_y = sun.orbit[day]
sun_coords = [sun_x, sun_y]
# Skip if planet is directly above or below the sun (would cause division by zero)
if planet_x == sun_x or earth_x == sun_x:
continue
# Gradient of the line between the superior planet and the sun and the earth and the sun
planet_sun_gradient = (planet_y - sun_y) / (planet_x - sun_x)
earth_sun_gradient = (earth_y - sun_y) / (earth_x - sun_x)
# Earth and Mars's distance to the sun
earth_sun_dist = earth.distances_to_sun[day]
planet_sun_dist = self.distances_to_sun[day]
# Earth to Mars distance
earth_planet_x_dist = earth_x - planet_x
earth_planet_y_dist = earth_y - planet_y
earth_planet_dist = math.sqrt(earth_planet_x_dist ** 2 + earth_planet_y_dist ** 2)
# Initialises the boolean flags to check whether the conjunction is superior or opposition
superior = False
Opposition = False
if earth_sun_dist > earth_planet_dist:
Opposition = True
else:
superior = True
if round(planet_sun_gradient, 2) == round(earth_sun_gradient, 2):
information = [day + 1, planet_coords, earth_coords, sun_coords]
if Opposition == True:
information.append("Opposition")
else:
information.append("Superior")
self.conjunctions.append(information)
# Finds quadrature
def Find_quadrature(self, earth, total_time, sun):
#iterates through each day and point in superior planet's orbit
for day in range(total_time - 1):
# Coordinates of the superior planet on a day
planet_x, planet_y = self.orbit[day]
planet_coords = [planet_x, planet_y]
# Coordinates of the earth on a day
earth_x, earth_y = earth.orbit[day]
earth_coords = [earth_x, earth_y]
# Coordinates of the sun on a day
sun_x, sun_y = sun.orbit[day]
sun_coords = [sun_x, sun_y]
# Mars's distance to the sun on a day
superior_sun_dist = self.distances_to_sun[day]
# Earth to Mars distance
earth_planet_x_dist = earth_x - planet_x
earth_planet_y_dist = earth_y - planet_y
earth_planet_dist = math.sqrt(earth_planet_x_dist ** 2 + earth_planet_y_dist ** 2)
# Uses pythagoras theorem to identify quadratures
earth_sun_dist = earth.distances_to_sun[day]
if round((superior_sun_dist ** 2), -21) == round((earth_planet_dist ** 2) + (earth_sun_dist ** 2), -21):
information = [day + 1, planet_coords, earth_coords, sun_coords]
self.quadratures.append(information)
# Shows Quadrature
def Show_quadrature(self, win, earth, sun, total_time, mercury, venus, mars, venus_elongations, mercury_elongations):
win.fill((0, 0, 0))
win.blit(background_img, (0, 0))
FONTtwo = pygame.font.SysFont("Comic Sans MS", 30)
# Displays each quadrature onto the window
for quadrature in self.quadratures:
next = False
# Decides when to show the next quadrature: If the mouse is clicked.
while next == False:
# If user clicks the X button on the window, control is returned to the main menu
for event in pygame.event.get():
if event.type == pygame.QUIT:
next = True
menu(win, mercury, venus, mercury_elongations, venus_elongations, earth, sun, total_time, mars)
if event.type == pygame.MOUSEBUTTONDOWN:
next = True
win.fill((0, 0, 0))
win.blit(background_img, (0, 0))
# Unpacks the variables required from the 2D list self.quadratures
times = quadrature[0]
planet_x, planet_y = quadrature[1]
earth_x, earth_y = quadrature[2]
sun_x, sun_y = quadrature[3]
# Scales the coordinates to the window
planet_x = planet_x * self.SCALE + WIDTH / 2
planet_y = planet_y * self.SCALE + WIDTH / 2
earth_x = earth_x * self.SCALE + WIDTH / 2
earth_y = earth_y * self.SCALE + WIDTH / 2
sun_x = sun_x * self.SCALE + WIDTH / 2
sun_y = sun_y * self.SCALE + WIDTH / 2
# Text that is going to be displayed onto the screen
time_keeper = FONTtwo.render(self.name + "'s quadrature: Day " + str(times), 1, WHITE)
# Displays date of quadrature, Earth, Mars and Sun onto the screen
win.blit(time_keeper, (0, 0))
win.blit(self.image, (planet_x, planet_y))
win.blit(earth.image, (earth_x, earth_y))
win.blit(sun.image, (sun_x, sun_y))
pygame.display.update()
# Subclass for inferior planets: mercury and venus to keep track of their inferior conjunction and elongations
class Inferiorplanet(Planet):
def __init__(self, x, y, radius, colour, mass, average_distance_to_sun, orbital_period, image=None, dimension=None, name=None, Elongations=None):
super().__init__(x, y, radius, colour, mass, average_distance_to_sun, orbital_period, image, dimension, name)
self.elongations = [] # Attribute to hold all the elongations that will occur for the inferior planet
def Find_greatest_elongation(self, Elongations, earth, total_time, sun):
# Iterates through every day in the simulation
# Finds the x and y coordinate of the planet on that day
# Finds the x and y coordinate of the earth on that day
# Uses pythagoras to find whether it is an elongation
for day in range(total_time - 1):
# Coordinates of the inferior planet
planet_x, planet_y = self.orbit[day]
planet_coords = [planet_x, planet_y]
# Coordinates of the Earth
earth_x, earth_y = earth.orbit[day]
earth_coords = [earth_x, earth_y]
# Coordinates of the Sun
sun_x, sun_y = sun.orbit[day]
sun_coords = [sun_x, sun_y]
# Mercury or Venus's distance to the Sun
inferior_sun_dist = self.distances_to_sun[day]
# Earth to Mercury or Venus distance
earth_planet_x_dist = earth_x - planet_x
earth_planet_y_dist = earth_y - planet_y
earth_planet_dist = math.sqrt(earth_planet_x_dist ** 2 + earth_planet_y_dist ** 2)
# Uses pythagoras theorem to identify greatest elongations
# Finds elongation angle tan = opposite / adjacent
if round((earth.distances_to_sun[day] ** 2), -21) == round((earth_planet_dist ** 2) + (inferior_sun_dist ** 2), -21):
elongation_angle = math.atan2((inferior_sun_dist), (earth_planet_dist))
elongation_angle = math.degrees(elongation_angle)
elongation_angle = round(elongation_angle, 1)
information = [day + 1, planet_coords, earth_coords, sun_coords, elongation_angle]
self.elongations.append(information)
# Shows an image of the position of the Earth, Sun and the inferior planet at its Greatest elongation
def show_greatest_elongations(self, win, earth, sun):
FONTtwo = pygame.font.SysFont("Comic Sans MS", 30)
# Displays each greatest elongation onto the window
for elongation in self.elongations:
next = False
# Next greatest elongation is only displayed if condition is met by left-clicking on screen
while next == False:
# If user clicks the X button on the window, control is returned to the main menu
for event in pygame.event.get():
if event.type == pygame.QUIT:
return None
if event.type == pygame.MOUSEBUTTONDOWN:
next = True
win.fill((0, 0, 0))
win.blit(background_img, (0, 0))
# Unpacks inferior planet's position, earth's position and the sun's position for current elongation
# Unpacks the elongation angle and date of elongation
times = elongation[0]
planet_x, planet_y = elongation[1]
earth_x, earth_y = elongation[2]
sun_x, sun_y = elongation[3]
elongation_angle = elongation[4]
# Scales the inferior planet, Earth and Sun for the window
planet_x = planet_x * self.SCALE + WIDTH / 2
planet_y = planet_y * self.SCALE + WIDTH / 2
earth_x = earth_x * self.SCALE + WIDTH / 2
earth_y = earth_y * self.SCALE + WIDTH / 2
sun_x = sun_x * self.SCALE + WIDTH / 2
sun_y = sun_y * self.SCALE + WIDTH / 2
time_keeper = FONTtwo.render(self.name + "'s greatest elongation: Day " + str(times), 1, WHITE)
angle_display = FONTtwo.render(self.name + "'s angle of elongation " + str(elongation_angle) + "°", 1,
WHITE)
# Date of elongation, elongation angle, inferior planet, Earth and Sun are displayed onto the screen
win.blit(time_keeper, (0, 0))
win.blit(angle_display, (0, 40))
win.blit(self.image, (planet_x, planet_y))
win.blit(earth.image, (earth_x, earth_y))
win.blit(sun.image, (sun_x, sun_y))
pygame.display.update()
# Allows the simulation to pause until the mouse is clicked on screen again.
def is_paused(WIN, mercury, venus, mercury_elongations, venus_elongations, earth, sun, total_time, mars):
global run
while run == False:
# If the mouse is clicked on the screen again the simulation can begin again
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONDOWN:
run = True
# If the X button on the window is clicked while the simulation is paused control is passed to the menu
if event.type == pygame.QUIT:
run = False
menu(WIN, mercury, venus, mercury_elongations, venus_elongations, earth, sun, total_time, mars)
quit()
break
# Loads and scales the image of the button
button = pygame.image.load(os.path.join(ASSETS, "menu_button.png"))
button = pygame.transform.scale(button, (400, 100)) # Width = 400 pixels Height = 100 pixels
class Button():
def __init__(self, x, y, image, text):
self.image = image
self.rect = self.image.get_rect()
self.rect.topleft = (x,y)
self.clicked = False
self.text = text
def draw(self, window):
FONTtwo = pygame.font.SysFont("Comic Sans MS", 30)
display_text = FONTtwo.render(self.text, True, WHITE)
action = False # A function is called if this variable returns True
self.clicked = False # Checks whether the user has clicked the screen
#Get mouse position on screen
position = pygame.mouse.get_pos()
# x and y positions of the text to be displayed on screen
x_coord = self.rect.x + (display_text.get_width() / 4)
y_coord = self.rect.y + (display_text.get_height() / 2)
#Displays the button and the text onto the screen
window.blit(self.image, (self.rect.x, self.rect.y))
window.blit(display_text, (x_coord,y_coord))
pygame.display.update()
#Check if the mouse is hovering over a button
if self.rect.collidepoint(position):
#Checks if the mouse is pressed
if pygame.mouse.get_pressed()[0] == True and self.clicked == False:
self.clicked = True
action = True
if pygame.mouse.get_pressed()[0] == 0:
self.clicked = False
return action
def menu(WIN, mercury, venus, mercury_elongations, venus_elongations, earth, sun, total_time, mars):
# Creates a different window for the Menu screen. Width = 600 pixels. Height = 800 pixels.
# The background image is scaled to fit the new window and is displayed on the menu.
WINT = pygame.display.set_mode((600, 800))
background_img_menu = pygame.transform.scale(background_img,(600,800))
WINT.blit(background_img_menu, (0, 0))
run = True
while run == True:
for event in pygame.event.get():
# If the user presses the X button on the window the Menu quits and the whole program is terminated.
if event.type == pygame.QUIT:
run = False
exit()
#Aphelion button
#Displayed at coordinates (100,50)
#Upon clicking the button the window is rescaled to the old simulation
#The same method is called for each planet to display their aphelions
#Once all methods executed, control is returned to the menu.
aphelion_button = Button(100, 50, button, "Show Aphelions")
if aphelion_button.draw(WINT):
WINT = pygame.display.set_mode((800, 800))
mercury.Find_and_Show_aphelion(total_time, WIN, sun, mercury, venus, mercury_elongations, venus_elongations, earth, mars)
venus.Find_and_Show_aphelion(total_time, WIN, sun, mercury, venus, mercury_elongations, venus_elongations, earth, mars)
earth.Find_and_Show_aphelion(total_time, WIN, sun, mercury, venus, mercury_elongations, venus_elongations, earth, mars)
mars.Find_and_Show_aphelion(total_time, WIN, sun, mercury, venus, mercury_elongations, venus_elongations, earth, mars)
menu(WIN, mercury, venus, mercury_elongations, venus_elongations, earth, sun, total_time, mars)
#Conjunction button
#Displayed at coordinates (100,200)
#Upon clicking the button the window is rescaled to the old simulation
#The same methods are called for the inferior planets but different methods are called for the superior planets
#Once all methods executed, control is returned to the menu
conjunction_button = Button(100,200, button, "Show Conjunctions")
if conjunction_button.draw(WINT):
WINT = pygame.display.set_mode((800,800))
#Superior conjunctions and oppositions (superior planets)
mars.Find_superior_conjunction_opposition(earth, sun, total_time)
mars.show_conjunction(WIN, earth, sun)
# Inferior and superior conjunctions (inferior planets)
mercury.Find_conjunction(earth, sun, total_time)
mercury.show_conjunction(WIN, earth, sun)
venus.Find_conjunction(earth, sun, total_time)
venus.show_conjunction(WIN, earth, sun)
menu(WIN, mercury, venus, mercury_elongations, venus_elongations, earth, sun, total_time, mars)
#Elongation button
#Displayed at coordinates (100,350)
#Upon clicking the button the window is rescaled to the old simulation
#The same methods are called for each inferior planet
#Once all methods executed, control is returned to the menu.
elongation_button = Button(100,350, button, "Show Elongations")
if elongation_button.draw(WINT):
WINT = pygame.display.set_mode((800,800))
mercury.Find_greatest_elongation(mercury_elongations, earth, total_time, sun)
mercury.show_greatest_elongations(WIN, earth, sun)
venus.Find_greatest_elongation(venus_elongations, earth, total_time, sun)
venus.show_greatest_elongations(WIN, earth, sun)
menu(WIN, mercury, venus, mercury_elongations, venus_elongations, earth, sun, total_time, mars)
#Perihelion button
#Displayed at coordinates (100,500)
#Upon clicking the button the window is rescaled to the old simulation
#The same method is called for each planet to display their perihelions
#Once all methods executed, control is returned to the menu.
perihelion_button = Button(100,500,button,"Show Perihelions")
if perihelion_button.draw(WINT):
WINT = pygame.display.set_mode((800,800))
mercury.Find_and_Show_perihelion(total_time, WIN, sun, mercury, venus, mercury_elongations, venus_elongations, earth, mars)
venus.Find_and_Show_perihelion(total_time, WIN, sun, mercury, venus, mercury_elongations, venus_elongations, earth, mars)
earth.Find_and_Show_perihelion(total_time, WIN, sun, mercury, venus, mercury_elongations, venus_elongations, earth, mars)
mars.Find_and_Show_perihelion(total_time, WIN, sun, mercury, venus, mercury_elongations, venus_elongations, earth, mars)
menu(WIN, mercury, venus, mercury_elongations, venus_elongations, earth, sun, total_time, mars)
#Quadrature button
#Displayed at coordinates (100,650)
#Upon clicking the button the window is rescaled to the old simulation
#One method finds the quadrature and the other displays the quadrature.
#Once the method is finished executing control is passed back to the menu
quadrature_button = Button(100, 650, button, "Show Quadratures")
if quadrature_button.draw(WINT):
WINT = pygame.display.set_mode((800,800))
mars.Find_quadrature(earth, total_time, sun)
mars.Show_quadrature(WIN, earth, sun,total_time, mercury, venus, mars, venus_elongations, mercury_elongations)
menu(WIN, mercury, venus, mercury_elongations, venus_elongations, earth, sun, total_time, mars)
pygame.display.update()
def main():
global run
global total_time
mercury_elongations = []
venus_elongations = []
clock = pygame.time.Clock() # A clock prevents the frame rate from going above a limit by regulating the frame rate.
# Instantiating all the planet objects.
sun = Planet(0, 0, 30, YELLOW, 1.98892 * 10 ** 30, 0,0, sun_img)
sun.sun = True
earth = Planet(-1 * Planet.AU, 0, 16, BLUE, 5.9742 * 10 ** 24, "1.496 * 10^11" , 365, earth_img, earth_dimension, "Earth")
earth.y_vel = 29.783 * 1000
mars = Superiorplanet(-1.524 * Planet.AU, 0, 12, RED, 6.39 * 10 ** 23,"2.279 * 10^11" , 687 ,mars_img, mars_dimension, "Mars")
mars.y_vel = 24.077 * 1000
mercury = Inferiorplanet(0.387 * Planet.AU, 0, 8, LIGHT_GREY, 3.30 * 10 ** 23, "5.790 * 10^10" ,88 , mercury_img, mercury_dimension,"Mercury")
mercury.y_vel = -47.4 * 1000
venus = Inferiorplanet(0.723 * Planet.AU, 0, 14, YELLOW, 4.8685 * 10 ** 24, "1.082 * 10^11" ,225, venus_img, venus_dimension,"Venus")
venus.y_vel = -35.02 * 1000
planets = [sun, earth, mars, mercury, venus] # List of all planet in the simulation
# Makes the window run constantly until someone presses the x button on the window
while run:
# Maximum frames per second. How many days will pass each second.
clock.tick(60)
total_time += 1
# Need to fill the screen each frame otherwise you can see the old position of the planets as well
WIN.fill((0, 0, 0))
WIN.blit(background_img, (0, 0))
# Each day is output on the screen. 60 days in 1 second.
FONTtwo = pygame.font.SysFont("Comic Sans MS", 30)
time_keeper = FONTtwo.render("Day " + str(total_time), 1, WHITE)
WIN.blit(time_keeper, (0, 0))
# For every planet we have instantiated the position is calculated and then drawn onto the screen
for planet in planets:
planet.update_position(planets)
planet.draw(WIN, total_time)
# Need a constantly running loop to keep track of different events so window doesn't instantly close
# Pauses the simulation if the mouse button is clicked on the screen
# Exits the simulation to the main menu if the X on the window is clicked.
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONDOWN:
run = False
is_paused(WIN, mercury, venus, mercury_elongations, venus_elongations, earth, sun, total_time, mars)
if event.type == pygame.QUIT:
run = False
# Updates the display to account for the changes in position of the planets
pygame.display.update()
# Calls the menu function if the X is clicked
menu(WIN, mercury, venus, mercury_elongations, venus_elongations, earth, sun, total_time, mars)
pygame.quit()
# Calls the main function to start the simulation
main()