Advertisement
Xnork

pygame-law-of-universal-gravitation

Dec 3rd, 2023
845
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.48 KB | Source Code | 0 0
  1. import pygame, math
  2. from pygame.locals import *
  3.  
  4. SCALE = 10 ** -6.58
  5. REAL_RADIUS_OF_EARTH = 6_371 * 10 ** 3
  6. REAL_RADIUS_OF_SUN = 696_340 * 10 ** 3
  7. G =  9.807
  8.  
  9. pygame.init()
  10.  
  11. display = pygame.display.set_mode((600, 600))
  12. game_is_running = True
  13. clock = pygame.time.Clock()
  14.  
  15. class Body:
  16.   def __init__(self, position, radius = 50.0, density = 0.89) -> None:
  17.     self.position = position
  18.     self.radius = radius
  19.     self.density = density
  20.     self.mass = self.radius ** 2 * math.pi * self.density
  21.  
  22.     self.acceleration = [0, 0]
  23.     self.velocity = [0, 0]
  24.  
  25.   def draw(self) -> None:
  26.     pygame.draw.circle(display, (255, 255, 255), self.position, self.radius)
  27.  
  28. class LawOfUniversalGravitatoin:
  29.   def __init__(self, body_a: Body, body_b: Body) -> None:
  30.     self.body_a = body_a
  31.     self.body_b = body_b
  32.  
  33.   def step(self) -> None:
  34.     angle = math.atan2(self.body_b.position[1] - self.body_a.position[1], self.body_b.position[0] - self.body_a.position[0])
  35.     magnitude = math.sqrt((self.body_b.position[0] - self.body_a.position[0]) ** 2 + (self.body_b.position[1] - self.body_a.position[1]) ** 2)
  36.  
  37.     F = G * (self.body_a.mass * self.body_b.mass) / magnitude
  38.  
  39.     self.body_a.acceleration[0] += math.cos(angle) * F / self.body_a.mass * SCALE
  40.     self.body_a.velocity[0] = self.body_a.acceleration[0]
  41.     self.body_a.acceleration[1] += math.sin(angle) * F / self.body_a.mass * SCALE
  42.     self.body_a.velocity[1] = self.body_a.acceleration[1]
  43.     self.body_a.position[1] += self.body_a.velocity[1]
  44.     self.body_a.position[0] += self.body_a.velocity[0]
  45.  
  46.     self.body_b.acceleration[0] += math.cos(angle) * F / self.body_b.mass * SCALE
  47.     self.body_b.velocity[0] = self.body_b.acceleration[1]
  48.     self.body_b.acceleration[1] += math.sin(angle) * F / self.body_b.mass * SCALE
  49.     self.body_b.velocity[1] = self.body_b.acceleration[1]
  50.     self.body_b.position[1] += self.body_b.velocity[1]
  51.     self.body_b.position[0] += self.body_b.velocity[0]
  52.  
  53. earth_body = Body([400, 100], REAL_RADIUS_OF_EARTH * SCALE)
  54. earth_body.acceleration[0] = -0.5
  55.  
  56. sun_body = Body([400, 400], REAL_RADIUS_OF_SUN * SCALE)
  57.  
  58. law_of_universal_gravitation = LawOfUniversalGravitatoin(earth_body, sun_body)
  59.  
  60. while game_is_running:
  61.   law_of_universal_gravitation.step()
  62.  
  63.   for event in pygame.event.get():
  64.     if event.type == pygame.QUIT:
  65.       game_is_running = False
  66.  
  67.   display.fill([20, 20, 20])  
  68.  
  69.   earth_body.draw()
  70.   sun_body.draw()
  71.  
  72.   pygame.display.flip()
  73.   clock.tick(120)
  74.  
  75. pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement