Skip to content

Commit 87a2517

Browse files
Merge pull request #2446 from andoriyaprashant/branch14
Gravity Simulation Game Added
2 parents 0abf403 + 3af0185 commit 87a2517

File tree

3 files changed

+169
-0
lines changed

3 files changed

+169
-0
lines changed

Gravity Simulation Game/README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Gravity Simulation Game
2+
3+
## Description
4+
5+
The Gravity Simulation Game is a 2D space-themed game developed in Python using the Pygame library. In this game, the player controls a spaceship that can be navigated using arrow keys or WASD. The objective is to reach a specific destination while avoiding collisions with celestial bodies that exert gravitational forces on the spaceship.
6+
7+
## Features
8+
9+
- Navigate the spaceship using arrow keys or WASD to control movement.
10+
- Experience realistic gravitational effects from celestial bodies on the spaceship's trajectory.
11+
- Multiple levels with increasing difficulty, each with different celestial bodies and positions.
12+
- Fuel management: The spaceship consumes fuel while moving, and you need to collect fuel power-ups to refill it.
13+
- Score and Timer: Try to reach the destination in the shortest time to achieve a higher score.
14+
- Thrusters Animation: Animated thrusters give a visual representation of propulsion while controlling the spaceship.
15+
- Sound Effects: Enjoy sound effects for spaceship movement, collisions, and other interactions.
16+
- Gravity Indicator: Get a visual indicator of the direction and strength of gravitational forces from nearby celestial bodies.
17+
- Background Music: Engage in the game with a background music track.
18+
19+
## How to Play
20+
21+
1. Install Python and Pygame.
22+
2. Clone this repository to your local machine.
23+
3. Run the game using the command: `python gravity_simulation_game.py`.
24+
4. Use arrow keys or WASD to control the spaceship's movement.
25+
5. Avoid collisions with celestial bodies by navigating carefully.
26+
6. Collect fuel power-ups to replenish your fuel tank.
27+
7. Reach the destination to progress to the next level.
28+
29+
## Dependencies
30+
31+
- Python 3.x
32+
- Pygame
33+
34+
Enjoy the game and have fun exploring space!
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
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()
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pygame
2+
random

0 commit comments

Comments
 (0)