-
Notifications
You must be signed in to change notification settings - Fork 0
/
simulation.py
179 lines (131 loc) · 4.98 KB
/
simulation.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
import pygame
import math
import requests
WIDTH, HEIGHT = 2000, 1000
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Planet simulator")
request = requests.get("http://api.open-notify.org/astros.json")
requests = requests.get("https://api.wheretheiss.at/v1/satellites/25544")
data_space_craft = requests.json()
data = request.json()
background = pygame.image.load("backgroundSpace.jpg")
RED = (255, 0, 0)
VIOLET = (148, 0, 211)
INDIGO = (75, 0, 130)
BLUE = (0, 0, 255)
GREEN = (0, 255, 0)
YELLOW = (255, 255, 0)
ORANGE = (255, 127, 0)
WHITE = (255, 255, 255)
GREY = (192, 192, 192)
FPS = 60
run = True
pygame.init()
clock = pygame.time.Clock()
default = 24
G = 6.67428e-11
AU = 149.6e6 * 1000
SCALE = 120 / AU
TIMESTEP = 3600 * 24 # seconds in day
FONT = pygame.font.SysFont("comicsansms", 15)
class Planet:
def __init__(self, x, y, mass, colour, radius):
self.x = x
self.y = y
self.mass = mass
self.colour = colour
self.radius = radius
self.x_vel = 0
self.y_vel = 0
self.distance_to_sun = 0
self.is_sun = False
self.forces = []
def draw_planet(self, window):
x = self.x * SCALE + WIDTH / 2
y = self.y * SCALE + HEIGHT / 2
pygame.draw.circle(window, self.colour, (x, y), self.radius)
new_points = []
if len(self.forces) > 2:
for coordinate in self.forces:
x, y = coordinate
x = x * SCALE + WIDTH // 2
y = y * SCALE + HEIGHT // 2
new_points.append((x, y))
pygame.draw.lines(window, WHITE, False, new_points, 3)
if not self.is_sun:
text = FONT.render(f"{round(self.distance_to_sun / 1000)}km", True, ORANGE)
window.blit(text, (x - text.get_width() // 2, y - text.get_height() // 2 * SCALE))
def calculate_force_attraction(self, other_planet):
other_planet_X, other_planet_Y = other_planet.x, other_planet.y
distance_X = other_planet_X - self.x
distance_Y = other_planet_Y - self.y
distance = math.sqrt(distance_X ** 2 + distance_Y ** 2)
if other_planet.is_sun:
self.distance_to_sun = distance
force = G * self.mass * other_planet.mass / distance ** 2
angle = math.atan2(distance_Y, distance_X)
fx = force * (math.cos(angle))
fy = force * (math.sin(angle))
return fx, fy
def update_position(self, planets):
fxtotal = fytotal = 0
for planet in planets:
if self is planet:
continue
fx, fy = self.calculate_force_attraction(planet)
fxtotal += fx
fytotal += fy
# f = ma
self.x_vel += fxtotal / self.mass * TIMESTEP
self.y_vel += fytotal / self.mass * TIMESTEP
self.x += self.x_vel * TIMESTEP
self.y += self.y_vel * TIMESTEP
self.forces.append((self.x, self.y))
def display_data(window):
num_people_in_space = data['number']
list_of_data = data['people']
peoples_name = ""
for info in list_of_data:
peoples_name += info['name'] + " "
name = data_space_craft['name']
position_lat = round(data_space_craft['latitude'])
position_long = round(data_space_craft['longitude'])
space_craft_alt = round(data_space_craft['altitude'])
space_craft_vel = round(data_space_craft['velocity'])
space_craft_data_text = f"Name: {name} Latitude: {position_lat} Longitude: {position_long} Altitude: {space_craft_alt}km Velocity: {space_craft_vel}km/h "
num_people = FONT.render(f"Current people in space: {num_people_in_space}", True, GREEN)
space_craft_data = FONT.render(space_craft_data_text, True, GREEN)
names = FONT.render(f"Astronauts: {peoples_name}", True, GREEN)
screen.blit(num_people, (0, 0))
screen.blit(space_craft_data, (0, 40))
screen.blit(names, (0, 20))
sun = Planet(0, 0, 1.98892e30, YELLOW, 30)
sun.is_sun = True
earth = Planet(-1 * AU, 0, 5.9742e24, BLUE, 16)
earth.y_vel = 29.783e3
mercury = Planet(.387 * AU, 0, 3.30e23, GREY, 9)
mercury.y_vel = 47.4e3
venus = Planet(0.723 * AU, 0, 4.8685e24, ORANGE, 12)
venus.y_vel = -35.02e3
mars = Planet(-1.524 * AU, 0, 6.39e23, RED, 14)
mars.y_vel = 24.077e3
jupiter = Planet(4.2 * AU, 0, 1.89e24, GREEN, 20)
jupiter.y_vel = -13.6e3
planets = [sun, earth, mercury, venus, mars, jupiter]
while run:
clock.tick(FPS)
screen.blit(background, (0, 0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
keys = pygame.key.get_pressed()
if keys[pygame.K_h]:
TIMESTEP = 3600
if keys[pygame.K_d]:
TIMESTEP = 3600 * 24
for planet in planets:
planet.update_position(planets)
planet.draw_planet(screen)
display_data(screen)
pygame.display.update()
pygame.quit()