Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- import sys
- import math
- # Initialize Pygame
- pygame.init()
- # Screen settings
- screen_width = 800
- screen_height = 600
- screen = pygame.display.set_mode((screen_width, screen_height))
- pygame.display.set_caption("Solar System Simulation")
- # Colors
- WHITE = (255, 255, 255)
- YELLOW = (255, 255, 0)
- BLUE = (0, 0, 255)
- RED = (255, 0, 0)
- ORANGE = (255, 165, 0)
- GRAY = (128, 128, 128)
- CYAN = (0, 255, 255)
- BLACK = (0, 0, 0)
- # Constants
- G = 1.0 # Gravitational constant (scaled)
- sun_radius = 20
- scale_factor = 10 # pixels per unit
- dt = 0.01 # time step
- # Generate orbit points
- def generate_orbit_points(a, e, num_points=100):
- points = []
- b = a * math.sqrt(1 - e**2)
- for i in range(num_points):
- theta = 2 * math.pi * i / num_points
- x = a * math.cos(theta)
- y = b * math.sin(theta)
- points.append((x, y))
- return points
- def generate_hyperbola_points(a, e, num_points=100):
- points = []
- for i in range(num_points):
- theta = 2 * math.pi * i / num_points - math.pi
- denominator = 1 + e * math.cos(theta)
- if denominator <= 0:
- continue
- r = (a * (1 - e**2)) / denominator
- x = r * math.cos(theta)
- y = r * math.sin(theta)
- points.append((x, y))
- return points
- # Define planets
- planets = [
- {'name': 'Mercury', 'a': 0.387, 'e': 0.2056, 'color': GRAY, 'radius': 3},
- {'name': 'Venus', 'a': 0.723, 'e': 0.0067, 'color': ORANGE, 'radius': 5},
- {'name': 'Earth', 'a': 1.0, 'e': 0.0167, 'color': BLUE, 'radius': 5},
- {'name': 'Mars', 'a': 1.524, 'e': 0.0934, 'color': RED, 'radius': 4},
- {'name': 'Jupiter', 'a': 5.203, 'e': 0.0484, 'color': ORANGE, 'radius': 10},
- {'name': 'Saturn', 'a': 9.582, 'e': 0.0542, 'color': YELLOW, 'radius': 8},
- {'name': 'Uranus', 'a': 19.18, 'e': 0.0472, 'color': CYAN, 'radius': 6},
- {'name': 'Neptune', 'a': 30.07, 'e': 0.0086, 'color': BLUE, 'radius': 6},
- ]
- # Define comet
- comet = {
- 'name': 'Comet',
- 'a': -1.0,
- 'e': 2.0,
- 'color': WHITE,
- 'radius': 2,
- }
- # Create objects list
- objects = []
- # Sun
- objects.append({'position': (0, 0), 'velocity': (0, 0), 'mass': 1.0, 'type': 'sun'})
- # Planets
- for planet in planets:
- a = planet['a']
- e = planet['e']
- r_peri = a * (1 - e)
- initial_position = (r_peri, 0)
- r = r_peri
- v_squared = 2 / r - 1 / a
- v = math.sqrt(v_squared)
- initial_velocity = (0, v)
- objects.append({
- 'position': initial_position,
- 'velocity': initial_velocity,
- 'mass': 0.0,
- 'type': 'planet',
- 'color': planet['color'],
- 'radius': planet['radius'],
- })
- # Comet
- a = comet['a']
- e = comet['e']
- r_peri = a * (1 - e)
- initial_position = (r_peri, 0)
- r = r_peri
- v_squared = 2 / r - 1 / a
- v = math.sqrt(v_squared)
- initial_velocity = (0, v)
- objects.append({
- 'position': initial_position,
- 'velocity': initial_velocity,
- 'mass': 0.0,
- 'type': 'comet',
- 'color': comet['color'],
- 'radius': comet['radius'],
- })
- # Clock
- clock = pygame.time.Clock()
- # Main loop
- running = True
- while running:
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- running = False
- # Update positions and velocities
- for obj in objects:
- if obj['type'] == 'sun':
- continue
- x, y = obj['position']
- r_squared = x**2 + y**2
- r = math.sqrt(r_squared)
- if r == 0:
- continue
- ax = - (G * x) / (r**3)
- ay = - (G * y) / (r**3)
- vx, vy = obj['velocity']
- new_vx = vx + ax * dt
- new_vy = vy + ay * dt
- obj['velocity'] = (new_vx, new_vy)
- new_x = x + new_vx * dt
- new_y = y + new_vy * dt
- obj['position'] = (new_x, new_y)
- # Draw everything
- screen.fill(BLACK)
- # Draw orbits
- for planet in planets:
- a = planet['a']
- e = planet['e']
- points = generate_orbit_points(a, e)
- scaled_points = []
- for x, y in points:
- sx = x * scale_factor + screen_width / 2
- sy = y * scale_factor + screen_height / 2
- scaled_points.append((sx, sy))
- if len(scaled_points) > 1:
- pygame.draw.lines(screen, WHITE, False, scaled_points, 1)
- # Draw comet's orbit
- a = comet['a']
- e = comet['e']
- points = generate_hyperbola_points(a, e)
- scaled_points = []
- for x, y in points:
- sx = x * scale_factor + screen_width / 2
- sy = y * scale_factor + screen_height / 2
- scaled_points.append((sx, sy))
- if len(scaled_points) > 1:
- pygame.draw.lines(screen, WHITE, False, scaled_points, 1)
- # Draw sun
- sun_x = screen_width // 2
- sun_y = screen_height // 2
- pygame.draw.circle(screen, YELLOW, (sun_x, sun_y), sun_radius)
- # Draw planets and comet
- for obj in objects:
- if obj['type'] == 'sun':
- continue
- x, y = obj['position']
- sx = x * scale_factor + sun_x
- sy = y * scale_factor + sun_y
- pygame.draw.circle(screen, obj['color'], (int(sx), int(sy)), obj['radius'])
- pygame.display.flip()
- clock.tick(60)
- pygame.quit()
- sys.exit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement