Skip to content

Commit 385d528

Browse files
committed
game added
1 parent 9f7d583 commit 385d528

File tree

1 file changed

+156
-0
lines changed

1 file changed

+156
-0
lines changed

game.py

+156
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
import pygame
2+
import random
3+
import os
4+
from home import save_high_score
5+
6+
# Initialize Pygame
7+
pygame.init()
8+
9+
# Set up display
10+
WIDTH, HEIGHT = 400, 600
11+
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
12+
pygame.display.set_caption("Flappy Bird")
13+
14+
# Load background image
15+
BACKGROUND_IMG = pygame.image.load(os.path.join("resources", "background.png"))
16+
BACKGROUND_IMG = pygame.transform.scale(BACKGROUND_IMG, (WIDTH, HEIGHT))
17+
18+
# Load sound effects
19+
JUMP_SOUND = pygame.mixer.Sound(os.path.join("resources", "sound_effects", "sfx_wing.wav"))
20+
HIT_SOUND = pygame.mixer.Sound(os.path.join("resources", "sound_effects", "sfx_hit.wav"))
21+
DIE_SOUND = pygame.mixer.Sound(os.path.join("resources", "sound_effects", "sfx_die.wav"))
22+
POINT_SOUND = pygame.mixer.Sound(os.path.join("resources", "sound_effects", "sfx_point.wav"))
23+
24+
# Colors
25+
WHITE = (255, 255, 255)
26+
BLACK = (0, 0, 0)
27+
28+
# Bird parameters
29+
bird_frames = [pygame.transform.scale(pygame.image.load(os.path.join("resources", "bird", f"bird{i:02}.png")), (50, 38)) for i in range(17)]
30+
bird_index = 0
31+
bird_img = bird_frames[bird_index]
32+
bird_rect = bird_img.get_rect()
33+
bird_rect.center = (100, HEIGHT // 2)
34+
bird_y_speed = 0
35+
36+
# Pipe parameters
37+
pipe_img = pygame.image.load(os.path.join("resources", "pipe.png"))
38+
pipe_img = pygame.transform.scale(pipe_img, (70, 400))
39+
pipe_list = []
40+
SPAWNPIPE = pygame.USEREVENT
41+
pygame.time.set_timer(SPAWNPIPE, 1500)
42+
pipe_speed = 5
43+
44+
# Gravity
45+
gravity = 0.25
46+
47+
# Score
48+
score = 0
49+
font = pygame.font.Font(None, 36)
50+
51+
# Load highest score
52+
def load_high_score():
53+
try:
54+
with open("high_score.txt", "r") as file:
55+
content = file.read()
56+
if content:
57+
return int(content)
58+
else:
59+
return 0
60+
except FileNotFoundError:
61+
return 0
62+
63+
# Save highest score
64+
def save_high_score(score):
65+
with open("high_score.txt", "w") as file:
66+
file.write(str(score))
67+
68+
# Game over
69+
game_over = False
70+
71+
# Flag to track if new high score sound has been played
72+
new_high_score_sound_played = False
73+
74+
def draw_window():
75+
WIN.blit(BACKGROUND_IMG, (0, 0)) # Draw background
76+
for pipe in pipe_list:
77+
WIN.blit(pipe_img, pipe)
78+
WIN.blit(bird_img, bird_rect)
79+
score_text = font.render(f"Score: {score}", True, WHITE)
80+
WIN.blit(score_text, (10, 10))
81+
pygame.display.update()
82+
83+
def move_bird():
84+
global bird_y_speed, bird_index
85+
bird_y_speed += gravity
86+
bird_rect.centery += bird_y_speed
87+
bird_index = (bird_index + 1) % len(bird_frames)
88+
bird_img = bird_frames[bird_index]
89+
90+
def move_pipes():
91+
for pipe in pipe_list:
92+
pipe.centerx -= pipe_speed
93+
pipe_list[:] = [pipe for pipe in pipe_list if pipe.right > 0]
94+
95+
def spawn_pipe():
96+
pipe_heights = [200, 300, 400]
97+
random_height = random.choice(pipe_heights)
98+
bottom_pipe = pipe_img.get_rect(midtop=(500, random_height))
99+
top_pipe = pipe_img.get_rect(midbottom=(500, random_height - 150))
100+
pipe_list.extend([bottom_pipe, top_pipe])
101+
102+
def check_collision():
103+
for pipe in pipe_list:
104+
if bird_rect.colliderect(pipe):
105+
HIT_SOUND.play() # Play hit sound
106+
return True
107+
if bird_rect.top <= 0 or bird_rect.bottom >= HEIGHT:
108+
DIE_SOUND.play() # Play death sound
109+
return True
110+
return False
111+
112+
def update_score():
113+
global score, new_high_score_sound_played
114+
score += 1
115+
# Check if new high score is achieved
116+
high_score = load_high_score()
117+
if score > high_score:
118+
save_high_score(score)
119+
if not new_high_score_sound_played:
120+
POINT_SOUND.play() # Play point sound for new high score
121+
new_high_score_sound_played = True
122+
123+
clock = pygame.time.Clock()
124+
125+
# Main game loop
126+
running = True
127+
while running:
128+
clock.tick(60)
129+
130+
for event in pygame.event.get():
131+
if event.type == pygame.QUIT:
132+
running = False
133+
if event.type == pygame.KEYDOWN:
134+
if event.key == pygame.K_SPACE and not game_over:
135+
bird_y_speed = -8
136+
JUMP_SOUND.play() # Play jump sound
137+
if event.key == pygame.K_SPACE and game_over:
138+
bird_rect.center = (100, HEIGHT // 2)
139+
pipe_list.clear()
140+
bird_y_speed = 0
141+
score = 0
142+
game_over = False
143+
new_high_score_sound_played = False # Reset flag
144+
if event.type == SPAWNPIPE:
145+
spawn_pipe()
146+
147+
if not game_over:
148+
move_bird()
149+
move_pipes()
150+
game_over = check_collision()
151+
if not game_over:
152+
update_score()
153+
154+
draw_window()
155+
156+
pygame.quit()

0 commit comments

Comments
 (0)