-
Notifications
You must be signed in to change notification settings - Fork 0
/
fredde.py
424 lines (352 loc) · 15 KB
/
fredde.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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
import pygame
import random
import sys
pygame.init()
# Constants for the game window
WINDOW_WIDTH = 800
WINDOW_HEIGHT = 600
FPS = 60
# Constants for colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
# Constants for game objects
PLAYER_WIDTH = 50
PLAYER_HEIGHT = 50
ENEMY_WIDTH = 40
ENEMY_HEIGHT = 40
ENEMY_SPEED = 5
# Constants for the game
SCORE_INCREMENT = 10
MAX_LIVES = 3
ENEMY_SPAWN_TIME = 1000 # milliseconds
BEST_SCORE_FILE = "best_score.txt"
# Function to end the game and show results
def game_over_screen(window, score, best_score, clock):
# Display the game over screen with score and best score
# Provide the option to restart the game
# Parameters:
# window (pygame.Surface): The game window surface
# score (int): The player's current score
# best_score (int): The best score achieved in the game
# clock (pygame.time.Clock): The game's clock for controlling the frame rate
window.fill(BLACK)
draw_text(window, "Game Over!", 48, WINDOW_WIDTH // 2, WINDOW_HEIGHT // 2 - 50, WHITE)
draw_text(window, f"Your Score: {score} points", 36, WINDOW_WIDTH // 2, WINDOW_HEIGHT // 2, WHITE)
draw_text(window, f"Best Score: {best_score} points", 36, WINDOW_WIDTH // 2, WINDOW_HEIGHT // 2 + 50, WHITE)
draw_text(window, "Press 'R' to Restart", 24, WINDOW_WIDTH // 2, WINDOW_HEIGHT // 2 + 100, WHITE)
pygame.display.update()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_r:
restart_game()
game()
# Function to end the game
def game_over():
# Quit the game and exit the program
pygame.quit()
sys.exit()
# Function to display text on the screen
def draw_text(window, text, size, x, y, color):
# Draw text on the specified window at the given position
# Parameters:
# window (pygame.Surface): The game window surface
# text (str): The text to display
# size (int): The font size
# x (int): X-coordinate of the text's center
# y (int): Y-coordinate of the text's center
# color (tuple): RGB color tuple (e.g., WHITE)
font = pygame.font.SysFont(None, size)
text_surface = font.render(text, True, color)
text_rect = text_surface.get_rect()
text_rect.centerx = x
text_rect.centery = y
window.blit(text_surface, text_rect)
# Function to restart the game
def restart_game():
# Reset the game state to start a new game
# Set the enemy speed, score, and lives to initial values
global ENEMY_SPEED, score, lives
ENEMY_SPEED = 5
score = 0
lives = MAX_LIVES
def load_best_score():
# Attempt to read the best score from the "best_score.txt" file
# If the file does not exist or is empty, return 0 as the best score
# If the file contains a valid integer, return it as the best score
try:
with open(BEST_SCORE_FILE, "r") as file:
content = file.read().strip()
if content.isdigit():
best_score = int(content)
return best_score
else:
return 0
except FileNotFoundError:
return 0
def save_best_score(best_score):
# Save the best score to the "best_score.txt" file
# Parameters:
# best_score (int): The best score achieved in the game
with open(BEST_SCORE_FILE, "w") as file:
file.write(str(best_score))
# Flappy Bird mini-game
def flappy_bird_game(window):
# The Flappy Bird mini-game
# Parameters:
# window (pygame.Surface): The game window surface
BIRD_WIDTH = 50
BIRD_HEIGHT = 50
BIRD_GRAVITY = 0.3
BIRD_FLAP_FORCE = -7
bird_x = WINDOW_WIDTH // 3
bird_y = WINDOW_HEIGHT // 2
bird_velocity = 0
# Load the bird image and resize it to match the bird's dimensions
BIRD_IMAGE = pygame.image.load('bird.png')
BIRD_IMAGE = pygame.transform.scale(BIRD_IMAGE, (BIRD_WIDTH, BIRD_HEIGHT))
# List to store the pipes
pipes = []
# Timer for spawning pipes
pipe_spawn_timer = pygame.time.get_ticks()
# Game loop for Flappy Bird
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_over()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
bird_velocity += BIRD_FLAP_FORCE
bird_velocity += BIRD_GRAVITY
bird_y += bird_velocity
if bird_y < 0:
bird_y = 0
bird_velocity = 0
elif bird_y + BIRD_HEIGHT > WINDOW_HEIGHT:
bird_y = WINDOW_HEIGHT - BIRD_HEIGHT
bird_velocity = 0
# Update pipes positions
for pipe in pipes:
pipe['x'] -= ENEMY_SPEED
# Remove off-screen pipes
pipes = [pipe for pipe in pipes if pipe['x'] + ENEMY_WIDTH > 0]
# Spawn new pipes every ENEMY_SPAWN_TIME milliseconds
current_time = pygame.time.get_ticks()
if current_time - pipe_spawn_timer > ENEMY_SPAWN_TIME:
pipe_height = random.randint(100, WINDOW_HEIGHT - 200)
pipes.append({'x': WINDOW_WIDTH, 'y': 0, 'height': pipe_height, 'counted': False})
pipes.append({'x': WINDOW_WIDTH, 'y': pipe_height + 200, 'height': WINDOW_HEIGHT, 'counted': False})
pipe_spawn_timer = current_time
# Check for collisions with pipes
for pipe in pipes:
if bird_x + BIRD_WIDTH > pipe['x'] and bird_x < pipe['x'] + ENEMY_WIDTH:
if bird_y < pipe['height'] or bird_y + BIRD_HEIGHT > pipe['y']:
# Collision with a pipe, game over
collision_sound.play()
return score
# Check if bird passes through a pipe for scoring
for pipe in pipes:
if pipe['x'] + ENEMY_WIDTH < bird_x and not pipe['counted']:
score += 1
pipe['counted'] = True
score_sound.play()
window.fill(BLACK)
draw_bird(window, bird_x, bird_y) # Use a custom function to draw the bird
# Draw pipes
for pipe in pipes:
pygame.draw.rect(window, WHITE, (pipe['x'], pipe['y'], ENEMY_WIDTH, pipe['height']))
pygame.draw.rect(window, WHITE, (pipe['x'], pipe['y'] + pipe['height'] + 200, ENEMY_WIDTH, WINDOW_HEIGHT))
draw_text(window, f"Score: {score}", 24, 70, 30, WHITE)
draw_text(window, f"Lives: {lives}", 24, 730, 30, WHITE)
draw_text(window, f"Best Score: {best_score}", 24, WINDOW_WIDTH // 2, 30, WHITE)
pygame.display.update()
clock.tick(FPS)
# Flappy Fredde mini-game
def flappy_fredde_game(window, best_score, clock):
# The Flappy Fredde mini-game
# Parameters:
# window (pygame.Surface): The game window surface
# best_score (int): The best score achieved in the main game
# clock (pygame.time.Clock): The game's clock for controlling the frame rate
FREDDE_WIDTH = 50
FREDDE_HEIGHT = 50
FREDDE_GRAVITY = 0.3
FREDDE_FLAP_FORCE = -7
fredde_x = WINDOW_WIDTH // 3
fredde_y = WINDOW_HEIGHT // 2
fredde_velocity = 0
score = 0 # Initialize score for Flappy Fredde game
# Load the Flappy Fredde image and resize it to match its dimensions
FREDDE_IMAGE = pygame.image.load('fredde.png')
FREDDE_IMAGE = pygame.transform.scale(FREDDE_IMAGE, (FREDDE_WIDTH, FREDDE_HEIGHT))
# List to store the pipes
pipes = []
# Timer for spawning pipes
pipe_spawn_timer = pygame.time.get_ticks()
# Game loop for Flappy Fredde
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_over()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
fredde_velocity += FREDDE_FLAP_FORCE
fredde_velocity += FREDDE_GRAVITY
fredde_y += fredde_velocity
if fredde_y < 0:
fredde_y = 0
fredde_velocity = 0
elif fredde_y + FREDDE_HEIGHT > WINDOW_HEIGHT:
fredde_y = WINDOW_HEIGHT - FREDDE_HEIGHT
fredde_velocity = 0
# Update pipes positions
for pipe in pipes:
pipe['x'] -= ENEMY_SPEED
# Remove off-screen pipes
pipes = [pipe for pipe in pipes if pipe['x'] + ENEMY_WIDTH > 0]
# Spawn new pipes every ENEMY_SPAWN_TIME milliseconds
current_time = pygame.time.get_ticks()
if current_time - pipe_spawn_timer > ENEMY_SPAWN_TIME:
pipe_height = random.randint(100, WINDOW_HEIGHT - 200)
pipes.append({'x': WINDOW_WIDTH, 'y': 0, 'height': pipe_height, 'counted': False})
pipes.append({'x': WINDOW_WIDTH, 'y': pipe_height + 200, 'height': WINDOW_HEIGHT, 'counted': False})
pipe_spawn_timer = current_time
# Check for collisions with pipes
for pipe in pipes:
if fredde_x + FREDDE_WIDTH > pipe['x'] and fredde_x < pipe['x'] + ENEMY_WIDTH:
if fredde_y < pipe['height'] or fredde_y + FREDDE_HEIGHT > pipe['y']:
# Collision with a pipe, game over
#collision_sound.play()
return score
# Check if Flappy Fredde passes through a pipe for scoring
for pipe in pipes:
if pipe['x'] + ENEMY_WIDTH < fredde_x and not pipe['counted']:
score += 1
pipe['counted'] = True
score_sound.play()
window.fill(BLACK)
draw_fredde(window, fredde_x, fredde_y) # Use a custom function to draw Flappy Fredde
# Draw pipes
for pipe in pipes:
pygame.draw.rect(window, WHITE, (pipe['x'], pipe['y'], ENEMY_WIDTH, pipe['height']))
pygame.draw.rect(window, WHITE, (pipe['x'], pipe['y'] + pipe['height'] + 200, ENEMY_WIDTH, WINDOW_HEIGHT))
draw_text(window, f"Score: {score}", 24, 70, 30, WHITE)
draw_text(window, f"Lives: {lives}", 24, 730, 30, WHITE)
draw_text(window, f"Best Score: {best_score}", 24, WINDOW_WIDTH // 2, 30, WHITE)
pygame.display.update()
clock.tick(FPS)
# Load the Fredde image and resize it to match the Fredde's dimensions
FREDDE_IMAGE = pygame.image.load('fredde.png')
FREDDE_IMAGE = pygame.transform.scale(FREDDE_IMAGE, (ENEMY_WIDTH, ENEMY_HEIGHT))
# Function to draw the bird
def draw_bird(window, x, y):
# Draw the bird on the specified window at the given position
# Parameters:
# window (pygame.Surface): The game window surface
# x (int): X-coordinate of the bird's top-left corner
# y (int): Y-coordinate of the bird's top-left corner
window.blit(BIRD_IMAGE, (x, y))
# Function to draw Flappy Fredde
def draw_fredde(window, x, y):
# Draw Flappy Fredde on the specified window at the given position
# Parameters:
# window (pygame.Surface): The game window surface
# x (int): X-coordinate of Flappy Fredde's top-left corner
# y (int): Y-coordinate of Flappy Fredde's top-left corner
window.blit(FREDDE_IMAGE, (x, y))
# Main game function
# ... (your existing code)
# Main game function
def game():
# The main game function that manages the gameplay
# Initialize the game window, clock, and other variables
# Run the game loop and update the game state accordingly
global ENEMY_SPEED
ENEMY_SPEED = 5
window = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
pygame.display.set_caption('Fredde the Enemy')
clock = pygame.time.Clock()
collision_sound = pygame.mixer.Sound('collision.wav')
score_sound = pygame.mixer.Sound('score.wav')
player_x = WINDOW_WIDTH // 2 - PLAYER_WIDTH // 2
player_y = WINDOW_HEIGHT - PLAYER_HEIGHT
enemy_x = random.randint(0, WINDOW_WIDTH - ENEMY_WIDTH)
enemy_y = 0
score = 0
lives = MAX_LIVES
best_score = load_best_score()
last_enemy_spawn_time = pygame.time.get_ticks()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_over()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_p:
pause = True
while pause:
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_over()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_p:
pause = False
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
player_x -= 5
if keys[pygame.K_RIGHT]:
player_x += 5
if keys[pygame.K_UP]:
player_y -= 5
if keys[pygame.K_DOWN]:
player_y += 5
player_x = max(0, min(player_x, WINDOW_WIDTH - PLAYER_WIDTH))
player_y = max(0, min(player_y, WINDOW_HEIGHT - PLAYER_HEIGHT))
if player_y <= 0: # Player reached the upper boundary
pygame.time.delay(500) # Delay to display a transitional message
draw_text(window, "Entering Flappy Fredde...", 36, WINDOW_WIDTH // 2, WINDOW_HEIGHT // 2, WHITE)
pygame.display.update()
pygame.time.delay(1000) # Delay before entering Flappy Fredde
if score >= 10: # 1000
# Start Flappy Fredde game if score is 1000 or more
score_in_flappy_fredde = flappy_fredde_game(window, best_score, clock)
score += score_in_flappy_fredde
player_y = WINDOW_HEIGHT - PLAYER_HEIGHT
enemy_y += ENEMY_SPEED
if player_x < enemy_x + ENEMY_WIDTH and player_x + PLAYER_WIDTH > enemy_x and player_y < enemy_y + ENEMY_HEIGHT and player_y + PLAYER_HEIGHT > enemy_y:
if lives <= 0:
if score > best_score:
best_score = score
save_best_score(best_score)
game_over_screen(window, score, best_score)
else:
collision_sound.play()
lives -= 1
enemy_x = random.randint(0, WINDOW_WIDTH - ENEMY_WIDTH)
enemy_y = 0
if enemy_y >= WINDOW_HEIGHT:
score_sound.play()
score += SCORE_INCREMENT
enemy_x = random.randint(0, WINDOW_WIDTH - ENEMY_WIDTH)
enemy_y = 0
current_time = pygame.time.get_ticks()
if current_time - last_enemy_spawn_time > ENEMY_SPAWN_TIME:
ENEMY_SPEED += 1
last_enemy_spawn_time = current_time
window.fill(BLACK)
pygame.draw.rect(window, WHITE, (player_x, player_y, PLAYER_WIDTH, PLAYER_HEIGHT))
window.blit(FREDDE_IMAGE, (enemy_x, enemy_y))
draw_text(window, f"Score: {score}", 24, 70, 30, WHITE)
draw_text(window, f"Lives: {lives}", 24, 730, 30, WHITE)
draw_text(window, f"Best Score: {best_score}", 24, WINDOW_WIDTH // 2, 30, WHITE)
pygame.display.update()
clock.tick(FPS)
if __name__ == "__main__":
restart_game()
game()
if __name__ == "__main__":
restart_game()
game()