-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGravitationalSlingshot.py
114 lines (87 loc) Β· 3.03 KB
/
GravitationalSlingshot.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
import pygame
import math
pygame.init()
WIDTH, HEIGHT = 1400, 800
win = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Gravitational Slingshot Effect")
PLANET_MASS = 200
SHIP_MASS = 1
G = 10
FPS = 60
PLANET_SIZE = 50
OBJ_SIZE = 5
VEL_SCALE = 50
BG = pygame.transform.scale(pygame.image.load("background.jpg"), (WIDTH, HEIGHT))
PLANET = pygame.transform.scale(pygame.image.load("jupiter.png"), (PLANET_SIZE * 2, PLANET_SIZE * 2))
WHITE = (255, 255, 255)
RED = (255, 0, 0)
BLUE = (0, 0, 255)
class Planet:
def __init__(self, x, y, mass):
self.x = x
self.y = y
self.mass = mass
def draw(self):
win.blit(PLANET, (self.x - PLANET_SIZE, self.y - PLANET_SIZE))
class Spacecraft:
def __init__(self, x, y, vel_x, vel_y, mass):
self.x = x
self.y = y
self.vel_x = vel_x
self.vel_y = vel_y
self.mass = mass
def move(self, planet=None):
distance = math.sqrt((self.x - planet.x)**2 + (self.y - planet.y)**2)
force = (G * self.mass * planet.mass) / distance ** 2
acceleration = force / self.mass
angle = math.atan2(planet.y - self.y, planet.x - self.x)
acceleration_x = acceleration * math.cos(angle)
acceleration_y = acceleration * math.sin(angle)
self.vel_x += acceleration_x
self.vel_y += acceleration_y
self.x += self.vel_x
self.y += self.vel_y
def draw(self):
pygame.draw.circle(win, RED, (int(self.x), int(self.y)), OBJ_SIZE)
def create_ship(location, mouse):
t_x, t_y = location
m_x, m_y = mouse
vel_x = (m_x - t_x) / VEL_SCALE
vel_y = (m_y - t_y) / VEL_SCALE
obj = Spacecraft(t_x, t_y, vel_x, vel_y, SHIP_MASS)
return obj
def main():
running = True
clock = pygame.time.Clock()
planet = Planet(WIDTH // 2, HEIGHT // 2, PLANET_MASS)
objects = []
temp_obj_pos = None
while running:
clock.tick(FPS)
mouse_pos = pygame.mouse.get_pos()
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.MOUSEBUTTONDOWN:
if temp_obj_pos:
obj = create_ship(temp_obj_pos, mouse_pos)
objects.append(obj)
temp_obj_pos = None
else:
temp_obj_pos = mouse_pos
win.blit(BG, (0, 0))
if temp_obj_pos:
pygame.draw.line(win, WHITE, temp_obj_pos, mouse_pos, 2)
pygame.draw.circle(win, RED, temp_obj_pos, OBJ_SIZE)
for obj in objects[:]:
obj.draw()
obj.move(planet)
off_screen = obj.x < 0 or obj.x > WIDTH or obj.y < 0 or obj.y > HEIGHT
collided = math.sqrt((obj.x - planet.x)**2 + (obj.y - planet.y)**2) <= PLANET_SIZE
if off_screen or collided:
objects.remove(obj)
planet.draw()
pygame.display.update()
pygame.quit()
if __name__ == "__main__":
main()