|
| 1 | +import pygame |
| 2 | +import math |
| 3 | +import random |
| 4 | + |
| 5 | +# Initialize Pygame |
| 6 | +pygame.init() |
| 7 | + |
| 8 | +# Screen dimensions |
| 9 | +WIDTH, HEIGHT = 800, 600 |
| 10 | +screen = pygame.display.set_mode((WIDTH, HEIGHT)) |
| 11 | +pygame.display.set_caption("Gravity Simulation Game") |
| 12 | + |
| 13 | +# Colors |
| 14 | +WHITE = (255, 255, 255) |
| 15 | +BLACK = (0, 0, 0) |
| 16 | +RED = (255, 0, 0) |
| 17 | +YELLOW = (255, 255, 0) |
| 18 | + |
| 19 | +# Spaceship properties |
| 20 | +spaceship_radius = 15 |
| 21 | +spaceship_pos = [WIDTH // 2, HEIGHT // 2] |
| 22 | +spaceship_vel = [0, 0] |
| 23 | + |
| 24 | +# Celestial bodies properties (position, mass) |
| 25 | +celestial_bodies = [ |
| 26 | + {"pos": [WIDTH // 4, HEIGHT // 4], "mass": 2000}, |
| 27 | + {"pos": [WIDTH * 3 // 4, HEIGHT * 3 // 4], "mass": 3000}, |
| 28 | +] |
| 29 | + |
| 30 | +# Gravity constant |
| 31 | +G = 0.1 |
| 32 | + |
| 33 | +# Load sound effects |
| 34 | +thrust_sound = pygame.mixer.Sound("thrust.wav") |
| 35 | +collision_sound = pygame.mixer.Sound("collision.wav") |
| 36 | + |
| 37 | +# Fuel properties |
| 38 | +fuel = 100 |
| 39 | +fuel_consumption_rate = 0.5 |
| 40 | +fuel_powerup_amount = 50 |
| 41 | + |
| 42 | +# Scoring properties |
| 43 | +score = 0 |
| 44 | +level = 1 |
| 45 | + |
| 46 | +# Font |
| 47 | +font = pygame.font.Font(None, 30) |
| 48 | + |
| 49 | +# Game loop |
| 50 | +running = True |
| 51 | +clock = pygame.time.Clock() |
| 52 | + |
| 53 | +while running: |
| 54 | + screen.fill(BLACK) |
| 55 | + |
| 56 | + for event in pygame.event.get(): |
| 57 | + if event.type == pygame.QUIT: |
| 58 | + running = False |
| 59 | + |
| 60 | + # Handle spaceship movement |
| 61 | + keys = pygame.key.get_pressed() |
| 62 | + if keys[pygame.K_LEFT] or keys[pygame.K_a]: |
| 63 | + spaceship_vel[0] -= 0.1 |
| 64 | + thrust_sound.play() |
| 65 | + if keys[pygame.K_RIGHT] or keys[pygame.K_d]: |
| 66 | + spaceship_vel[0] += 0.1 |
| 67 | + thrust_sound.play() |
| 68 | + if keys[pygame.K_UP] or keys[pygame.K_w]: |
| 69 | + spaceship_vel[1] -= 0.1 |
| 70 | + thrust_sound.play() |
| 71 | + if keys[pygame.K_DOWN] or keys[pygame.K_s]: |
| 72 | + spaceship_vel[1] += 0.1 |
| 73 | + thrust_sound.play() |
| 74 | + |
| 75 | + # Update spaceship position |
| 76 | + spaceship_pos[0] += spaceship_vel[0] |
| 77 | + spaceship_pos[1] += spaceship_vel[1] |
| 78 | + |
| 79 | + # Calculate gravity forces on the spaceship |
| 80 | + for body in celestial_bodies: |
| 81 | + dist_x = body["pos"][0] - spaceship_pos[0] |
| 82 | + dist_y = body["pos"][1] - spaceship_pos[1] |
| 83 | + distance = math.sqrt(dist_x**2 + dist_y**2) |
| 84 | + |
| 85 | + # Apply gravity force to the spaceship's velocity |
| 86 | + spaceship_vel[0] += G * body["mass"] * dist_x / distance**3 |
| 87 | + spaceship_vel[1] += G * body["mass"] * dist_y / distance**3 |
| 88 | + |
| 89 | + # Check for collisions with celestial bodies |
| 90 | + if distance < body["mass"] + spaceship_radius: |
| 91 | + collision_sound.play() |
| 92 | + pygame.draw.circle(screen, RED, body["pos"], int(body["mass"] / 10)) |
| 93 | + pygame.draw.circle(screen, YELLOW, spaceship_pos, spaceship_radius) |
| 94 | + pygame.display.flip() |
| 95 | + pygame.time.delay(2000) |
| 96 | + spaceship_pos = [WIDTH // 2, HEIGHT // 2] |
| 97 | + spaceship_vel = [0, 0] |
| 98 | + fuel = 100 |
| 99 | + |
| 100 | + # Consume fuel while moving |
| 101 | + if keys[pygame.K_LEFT] or keys[pygame.K_a] or keys[pygame.K_RIGHT] or keys[pygame.K_d] or \ |
| 102 | + keys[pygame.K_UP] or keys[pygame.K_w] or keys[pygame.K_DOWN] or keys[pygame.K_s]: |
| 103 | + fuel -= fuel_consumption_rate |
| 104 | + |
| 105 | + # Draw celestial bodies |
| 106 | + for body in celestial_bodies: |
| 107 | + pygame.draw.circle(screen, WHITE, body["pos"], int(body["mass"] / 10)) |
| 108 | + |
| 109 | + # Draw the spaceship |
| 110 | + pygame.draw.circle(screen, YELLOW, spaceship_pos, spaceship_radius) |
| 111 | + |
| 112 | + # Draw fuel indicator and score |
| 113 | + fuel_indicator = pygame.Rect(50, 50, fuel, 10) |
| 114 | + pygame.draw.rect(screen, YELLOW, fuel_indicator) |
| 115 | + score_text = f"Score: {score} - Level: {level}" |
| 116 | + score_surface = font.render(score_text, True, YELLOW) |
| 117 | + screen.blit(score_surface, (50, 80)) |
| 118 | + |
| 119 | + # Check for level completion |
| 120 | + if spaceship_pos[0] > WIDTH or spaceship_pos[0] < 0 or \ |
| 121 | + spaceship_pos[1] > HEIGHT or spaceship_pos[1] < 0: |
| 122 | + score += fuel + (level * 100) # Increase score based on remaining fuel and level |
| 123 | + level += 1 |
| 124 | + fuel = 100 |
| 125 | + spaceship_pos = [WIDTH // 2, HEIGHT // 2] |
| 126 | + spaceship_vel = [0, 0] |
| 127 | + celestial_bodies = [{"pos": [random.randint(50, WIDTH - 50), random.randint(50, HEIGHT - 50)], |
| 128 | + "mass": random.randint(1000, 5000)} for _ in range(level + 1)] |
| 129 | + |
| 130 | + pygame.display.flip() |
| 131 | + clock.tick(60) |
| 132 | + |
| 133 | +pygame.quit() |
0 commit comments