Advertisement
Guest User

30B-A3B-planets

a guest
May 1st, 2025
74
0
4 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.20 KB | None | 0 0
  1. import pygame
  2. import sys
  3. import math
  4.  
  5. # Initialize Pygame
  6. pygame.init()
  7.  
  8. # Screen settings
  9. screen_width = 800
  10. screen_height = 600
  11. screen = pygame.display.set_mode((screen_width, screen_height))
  12. pygame.display.set_caption("Solar System Simulation")
  13.  
  14. # Colors
  15. WHITE = (255, 255, 255)
  16. YELLOW = (255, 255, 0)
  17. BLUE = (0, 0, 255)
  18. RED = (255, 0, 0)
  19. ORANGE = (255, 165, 0)
  20. GRAY = (128, 128, 128)
  21. CYAN = (0, 255, 255)
  22. BLACK = (0, 0, 0)
  23.  
  24. # Constants
  25. G = 1.0  # Gravitational constant (scaled)
  26. sun_radius = 20
  27. scale_factor = 10  # pixels per unit
  28. dt = 0.01  # time step
  29.  
  30. # Generate orbit points
  31. def generate_orbit_points(a, e, num_points=100):
  32.     points = []
  33.     b = a * math.sqrt(1 - e**2)
  34.     for i in range(num_points):
  35.         theta = 2 * math.pi * i / num_points
  36.         x = a * math.cos(theta)
  37.         y = b * math.sin(theta)
  38.         points.append((x, y))
  39.     return points
  40.  
  41. def generate_hyperbola_points(a, e, num_points=100):
  42.     points = []
  43.     for i in range(num_points):
  44.         theta = 2 * math.pi * i / num_points - math.pi
  45.         denominator = 1 + e * math.cos(theta)
  46.         if denominator <= 0:
  47.             continue
  48.         r = (a * (1 - e**2)) / denominator
  49.         x = r * math.cos(theta)
  50.         y = r * math.sin(theta)
  51.         points.append((x, y))
  52.     return points
  53.  
  54. # Define planets
  55. planets = [
  56.     {'name': 'Mercury', 'a': 0.387, 'e': 0.2056, 'color': GRAY, 'radius': 3},
  57.     {'name': 'Venus', 'a': 0.723, 'e': 0.0067, 'color': ORANGE, 'radius': 5},
  58.     {'name': 'Earth', 'a': 1.0, 'e': 0.0167, 'color': BLUE, 'radius': 5},
  59.     {'name': 'Mars', 'a': 1.524, 'e': 0.0934, 'color': RED, 'radius': 4},
  60.     {'name': 'Jupiter', 'a': 5.203, 'e': 0.0484, 'color': ORANGE, 'radius': 10},
  61.     {'name': 'Saturn', 'a': 9.582, 'e': 0.0542, 'color': YELLOW, 'radius': 8},
  62.     {'name': 'Uranus', 'a': 19.18, 'e': 0.0472, 'color': CYAN, 'radius': 6},
  63.     {'name': 'Neptune', 'a': 30.07, 'e': 0.0086, 'color': BLUE, 'radius': 6},
  64. ]
  65.  
  66. # Define comet
  67. comet = {
  68.     'name': 'Comet',
  69.     'a': -1.0,
  70.     'e': 2.0,
  71.     'color': WHITE,
  72.     'radius': 2,
  73. }
  74.  
  75. # Create objects list
  76. objects = []
  77.  
  78. # Sun
  79. objects.append({'position': (0, 0), 'velocity': (0, 0), 'mass': 1.0, 'type': 'sun'})
  80.  
  81. # Planets
  82. for planet in planets:
  83.     a = planet['a']
  84.     e = planet['e']
  85.     r_peri = a * (1 - e)
  86.     initial_position = (r_peri, 0)
  87.     r = r_peri
  88.     v_squared = 2 / r - 1 / a
  89.     v = math.sqrt(v_squared)
  90.     initial_velocity = (0, v)
  91.     objects.append({
  92.         'position': initial_position,
  93.         'velocity': initial_velocity,
  94.         'mass': 0.0,
  95.         'type': 'planet',
  96.         'color': planet['color'],
  97.         'radius': planet['radius'],
  98.     })
  99.  
  100. # Comet
  101. a = comet['a']
  102. e = comet['e']
  103. r_peri = a * (1 - e)
  104. initial_position = (r_peri, 0)
  105. r = r_peri
  106. v_squared = 2 / r - 1 / a
  107. v = math.sqrt(v_squared)
  108. initial_velocity = (0, v)
  109. objects.append({
  110.     'position': initial_position,
  111.     'velocity': initial_velocity,
  112.     'mass': 0.0,
  113.     'type': 'comet',
  114.     'color': comet['color'],
  115.     'radius': comet['radius'],
  116. })
  117.  
  118. # Clock
  119. clock = pygame.time.Clock()
  120.  
  121. # Main loop
  122. running = True
  123. while running:
  124.     for event in pygame.event.get():
  125.         if event.type == pygame.QUIT:
  126.             running = False
  127.  
  128.     # Update positions and velocities
  129.     for obj in objects:
  130.         if obj['type'] == 'sun':
  131.             continue
  132.         x, y = obj['position']
  133.         r_squared = x**2 + y**2
  134.         r = math.sqrt(r_squared)
  135.         if r == 0:
  136.             continue
  137.         ax = - (G * x) / (r**3)
  138.         ay = - (G * y) / (r**3)
  139.         vx, vy = obj['velocity']
  140.         new_vx = vx + ax * dt
  141.         new_vy = vy + ay * dt
  142.         obj['velocity'] = (new_vx, new_vy)
  143.         new_x = x + new_vx * dt
  144.         new_y = y + new_vy * dt
  145.         obj['position'] = (new_x, new_y)
  146.  
  147.     # Draw everything
  148.     screen.fill(BLACK)
  149.  
  150.     # Draw orbits
  151.     for planet in planets:
  152.         a = planet['a']
  153.         e = planet['e']
  154.         points = generate_orbit_points(a, e)
  155.         scaled_points = []
  156.         for x, y in points:
  157.             sx = x * scale_factor + screen_width / 2
  158.             sy = y * scale_factor + screen_height / 2
  159.             scaled_points.append((sx, sy))
  160.         if len(scaled_points) > 1:
  161.             pygame.draw.lines(screen, WHITE, False, scaled_points, 1)
  162.  
  163.     # Draw comet's orbit
  164.     a = comet['a']
  165.     e = comet['e']
  166.     points = generate_hyperbola_points(a, e)
  167.     scaled_points = []
  168.     for x, y in points:
  169.         sx = x * scale_factor + screen_width / 2
  170.         sy = y * scale_factor + screen_height / 2
  171.         scaled_points.append((sx, sy))
  172.     if len(scaled_points) > 1:
  173.         pygame.draw.lines(screen, WHITE, False, scaled_points, 1)
  174.  
  175.     # Draw sun
  176.     sun_x = screen_width // 2
  177.     sun_y = screen_height // 2
  178.     pygame.draw.circle(screen, YELLOW, (sun_x, sun_y), sun_radius)
  179.  
  180.     # Draw planets and comet
  181.     for obj in objects:
  182.         if obj['type'] == 'sun':
  183.             continue
  184.         x, y = obj['position']
  185.         sx = x * scale_factor + sun_x
  186.         sy = y * scale_factor + sun_y
  187.         pygame.draw.circle(screen, obj['color'], (int(sx), int(sy)), obj['radius'])
  188.  
  189.     pygame.display.flip()
  190.     clock.tick(60)
  191.  
  192. pygame.quit()
  193. sys.exit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement