-
Notifications
You must be signed in to change notification settings - Fork 0
/
river_raid.py
250 lines (210 loc) · 8.47 KB
/
river_raid.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
import pygame
import sys
import random
# Initialize Pygame
pygame.init()
# Constants
WIDTH, HEIGHT = 800, 600
FPS = 60
PLAYER_BASE_SPEED = 5
PLAYER_BOOSTED_SPEED = 8
BULLET_SPEED = 7
# Colors
BLUE = (0, 0, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
WHITE = (255, 255, 255)
DARK_GREEN = (0, 100, 0)
YELLOW = (255, 255, 0)
# Setup the screen
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Enhanced River Raid")
# Setup clock for FPS
clock = pygame.time.Clock()
# Player class
class Player(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = pygame.Surface((30, 30), pygame.SRCALPHA)
pygame.draw.polygon(self.image, RED, [(15, 0), (0, 30), (30, 30)]) # Triangle representing the plane
self.rect = self.image.get_rect()
self.rect.center = (WIDTH // 2, HEIGHT - 100)
self.speed = PLAYER_BASE_SPEED
self.fuel = 100 # Starting fuel
def update(self):
keys = pygame.key.get_pressed()
if keys[pygame.K_LSHIFT] or keys[pygame.K_RSHIFT]: # Increase speed when Shift key is held down
self.speed = PLAYER_BOOSTED_SPEED
else:
self.speed = PLAYER_BASE_SPEED
if keys[pygame.K_LEFT] and self.rect.left > 100: # Limit movement to within the riverbanks
self.rect.x -= self.speed
if keys[pygame.K_RIGHT] and self.rect.right < WIDTH - 100:
self.rect.x += self.speed
def refuel(self):
self.fuel = min(100, self.fuel + 30) # Refill fuel
# Bullet class
class Bullet(pygame.sprite.Sprite):
def __init__(self, x, y):
super().__init__()
self.image = pygame.Surface((5, 10))
self.image.fill(WHITE)
self.rect = self.image.get_rect()
self.rect.centerx = x
self.rect.bottom = y
self.speed = BULLET_SPEED
def update(self):
self.rect.y -= self.speed
# Remove bullet if it goes off screen
if self.rect.bottom < 0:
self.kill()
# Obstacle class (representing ships)
class Obstacle(pygame.sprite.Sprite):
def __init__(self, x, y):
super().__init__()
self.image = pygame.Surface((40, 20))
pygame.draw.rect(self.image, GREEN, [0, 0, 40, 20]) # Simple rectangle for a ship
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
self.speed = random.randint(2, 5) # Random speed for each obstacle
def update(self):
self.rect.y += self.speed
if self.rect.top > HEIGHT:
self.rect.y = -60
self.rect.x = random.randint(100, WIDTH - 140)
# Fuel class (representing fuel stations)
class Fuel(pygame.sprite.Sprite):
def __init__(self, x, y):
super().__init__()
self.image = pygame.Surface((30, 40))
pygame.draw.rect(self.image, YELLOW, [0, 0, 30, 40]) # Yellow rectangle for a fuel station
pygame.draw.line(self.image, RED, (15, 0), (15, 40), 3) # Vertical red line to resemble a fuel symbol
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
self.speed = 2 # Speed for fuel stations
def update(self):
self.rect.y += self.speed
if self.rect.top > HEIGHT:
self.rect.y = -30
self.rect.x = random.randint(100, WIDTH - 130)
# Bridge class
class Bridge(pygame.sprite.Sprite):
def __init__(self, y):
super().__init__()
self.image = pygame.Surface((WIDTH - 200, 20))
pygame.draw.rect(self.image, DARK_GREEN, [0, 0, WIDTH - 200, 20]) # Bridge representation
self.rect = self.image.get_rect()
self.rect.x = 100 # Fixed position to match river width
self.rect.y = y
self.speed = 2 # Bridge speed
def update(self):
self.rect.y += self.speed
if self.rect.top > HEIGHT:
self.rect.y = -20
# Function to create game objects
def create_game_objects():
player = Player()
obstacles = pygame.sprite.Group()
fuels = pygame.sprite.Group()
bridges = pygame.sprite.Group()
bullets = pygame.sprite.Group()
# Create obstacles, fuel, and bridges
for i in range(5):
x = random.randint(100, WIDTH - 140) # Place within riverbanks
y = random.randint(-100, -40)
obstacle = Obstacle(x, y)
obstacles.add(obstacle)
for i in range(3): # Adding fewer fuels and bridges for balance
fuel_x = random.randint(100, WIDTH - 130)
fuel_y = random.randint(-300, -40)
fuel = Fuel(fuel_x, fuel_y)
fuels.add(fuel)
# Group all sprites together
all_sprites = pygame.sprite.Group()
all_sprites.add(player)
all_sprites.add(obstacles)
all_sprites.add(fuels)
all_sprites.add(bridges)
all_sprites.add(bullets)
return player, obstacles, fuels, bridges, bullets, all_sprites
# Game over function
def game_over(screen, score, successful_shots):
font = pygame.font.SysFont(None, 72)
game_over_text = font.render('Game Over', True, WHITE)
score_text = font.render(f'Final Score: {score}', True, WHITE)
shots_text = font.render(f'Successful Shots: {successful_shots}', True, WHITE)
screen.blit(game_over_text, (WIDTH // 2 - game_over_text.get_width() // 2, HEIGHT // 2 - 100))
screen.blit(score_text, (WIDTH // 2 - score_text.get_width() // 2, HEIGHT // 2 - 50))
screen.blit(shots_text, (WIDTH // 2 - shots_text.get_width() // 2, HEIGHT // 2))
pygame.display.flip()
pygame.time.wait(3000)
# Main game loop
def main():
player, obstacles, fuels, bridges, bullets, all_sprites = create_game_objects()
# Score, level, and successful shots
score = 0
level = 1
successful_shots = 0
running = True
while running:
# Handle events
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE: # Shoot a bullet when Space key is pressed
bullet = Bullet(player.rect.centerx, player.rect.top)
bullets.add(bullet)
all_sprites.add(bullet)
# Update all sprites
all_sprites.update()
# Check for collisions
if pygame.sprite.spritecollide(player, obstacles, False) or pygame.sprite.spritecollide(player, bridges, False):
game_over(screen, score, successful_shots)
running = False
# Check if player refuels
if pygame.sprite.spritecollide(player, fuels, True):
player.refuel()
# Check if bullets hit obstacles
hits = pygame.sprite.groupcollide(bullets, obstacles, True, True)
for hit in hits:
successful_shots += 1 # Increment the successful shots counter
score += 10
# Optionally, add new obstacles
x = random.randint(100, WIDTH - 140)
y = random.randint(-100, -40)
obstacle = Obstacle(x, y)
obstacles.add(obstacle)
all_sprites.add(obstacle)
# Decrease fuel over time
player.fuel -= 0.05 # Slower fuel consumption
if player.fuel <= 0:
game_over(screen, score, successful_shots)
running = False
# Update score
score += 1
# Draw everything
screen.fill(BLUE) # Blue background to represent the river
# Draw the riverbanks
pygame.draw.rect(screen, DARK_GREEN, [0, 0, 100, HEIGHT]) # Left bank
pygame.draw.rect(screen, DARK_GREEN, [WIDTH - 100, 0, 100, HEIGHT]) # Right bank
all_sprites.draw(screen)
# Display score, fuel, level, and successful shots
font = pygame.font.SysFont(None, 36)
score_text = font.render(f'Score: {score}', True, WHITE)
fuel_text = font.render(f'Fuel: {int(player.fuel)}', True, WHITE)
level_text = font.render(f'Level: {level}', True, WHITE)
shots_text = font.render(f'Successful Shots: {successful_shots}', True, WHITE)
screen.blit(score_text, (10, 10))
screen.blit(fuel_text, (10, 50))
screen.blit(level_text, (10, 90))
screen.blit(shots_text, (10, 130))
pygame.display.flip()
# Cap the frame rate
clock.tick(FPS)
pygame.quit()
sys.exit()
if __name__ == "__main__":
main()