Repository navigation
Expand file tree
/
Copy pathflappy bird.py
More file actions
259 lines (215 loc) · 8.2 KB
/
Copy pathflappy bird.py
File metadata and controls
259 lines (215 loc) · 8.2 KB
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
import pygame
import random
import sys
import math
# Initialize Pygame
pygame.init()
# Constants
WINDOW_WIDTH = 400
WINDOW_HEIGHT = 600
GRAVITY = 0.25
JUMP_SPEED = -7
PIPE_SPEED = 3
PIPE_GAP = 150
PIPE_FREQUENCY = 1500 # milliseconds
FPS = 60
# Colors
WHITE = (255, 255, 255)
SKY_BLUE = (113, 197, 207)
PIPE_GREEN = (95, 168, 37)
GROUND_COLOR = (222, 216, 149)
# Set up the display
screen = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
pygame.display.set_caption('Flappy Bird')
clock = pygame.time.Clock()
def create_bird_surface():
surface = pygame.Surface((40, 30), pygame.SRCALPHA)
# Body
pygame.draw.ellipse(surface, (255, 255, 0), (0, 0, 30, 30)) # Yellow body
# Eye
pygame.draw.circle(surface, WHITE, (25, 10), 6)
pygame.draw.circle(surface, (0, 0, 0), (25, 10), 3)
# Beak
pygame.draw.polygon(surface, (255, 165, 0), [(30, 15), (40, 10), (30, 20)])
# Wing
pygame.draw.ellipse(surface, (218, 218, 0), (5, 10, 15, 10))
return surface
def create_pipe_surface():
surface = pygame.Surface((50, WINDOW_HEIGHT), pygame.SRCALPHA)
# Main pipe body
pygame.draw.rect(surface, PIPE_GREEN, (0, 0, 50, WINDOW_HEIGHT))
# Pipe cap
cap_height = 30
pygame.draw.rect(surface, (82, 147, 32), (0, 0, 50, cap_height))
pygame.draw.rect(surface, (82, 147, 32), (-5, cap_height, 60, 10))
# Highlight
pygame.draw.rect(surface, (108, 183, 41), (5, 0, 10, WINDOW_HEIGHT))
return surface
# Create game assets
bird_surface = create_bird_surface()
pipe_surface = create_pipe_surface()
class Bird:
def __init__(self):
self.x = WINDOW_WIDTH // 3
self.y = WINDOW_HEIGHT // 2
self.velocity = 0
self.rect = pygame.Rect(self.x + 5, self.y + 5, 25, 25)
self.angle = 0
self.animation_time = 0
self.wing_up = False
def jump(self):
self.velocity = JUMP_SPEED
self.angle = 30
def update(self):
self.velocity += GRAVITY
self.y += self.velocity
self.rect.y = self.y + 5
# Update rotation based on velocity
self.angle = max(-70, min(30, -self.velocity * 4))
# Wing flap animation
self.animation_time += 1
if self.animation_time >= 15:
self.animation_time = 0
self.wing_up = not self.wing_up
def draw(self):
bird_copy = bird_surface.copy()
if self.wing_up:
# Animate wing
pygame.draw.ellipse(bird_copy, (218, 218, 0), (5, 8, 15, 10))
rotated_bird = pygame.transform.rotate(bird_copy, self.angle)
screen.blit(rotated_bird, (self.x - rotated_bird.get_width()//2,
self.y - rotated_bird.get_height()//2))
class Pipe:
def __init__(self):
self.gap_y = random.randint(200, WINDOW_HEIGHT - 200)
self.x = WINDOW_WIDTH
self.width = 50
self.passed = False
# Create rectangles for collision detection
self.top_pipe = pygame.Rect(
self.x,
0,
self.width,
self.gap_y - PIPE_GAP // 2
)
self.bottom_pipe = pygame.Rect(
self.x,
self.gap_y + PIPE_GAP // 2,
self.width,
WINDOW_HEIGHT - (self.gap_y + PIPE_GAP // 2)
)
def update(self):
self.x -= PIPE_SPEED
self.top_pipe.x = self.x
self.bottom_pipe.x = self.x
def draw(self):
# Draw top pipe (flipped)
top_pipe = pygame.transform.flip(pipe_surface, False, True)
top_pipe = pygame.transform.scale(top_pipe, (self.width, self.top_pipe.height))
screen.blit(top_pipe, self.top_pipe)
# Draw bottom pipe
bottom_pipe = pygame.transform.scale(pipe_surface, (self.width, self.bottom_pipe.height))
screen.blit(bottom_pipe, self.bottom_pipe)
def draw_ground():
ground_rect = pygame.Rect(0, WINDOW_HEIGHT - 50, WINDOW_WIDTH, 50)
pygame.draw.rect(screen, GROUND_COLOR, ground_rect)
# Add stripes
for i in range(0, WINDOW_WIDTH, 30):
pygame.draw.line(screen, (209, 203, 139),
(i, WINDOW_HEIGHT - 50),
(i + 15, WINDOW_HEIGHT),
3)
def main():
bird = Bird()
pipes = []
score = 0
high_score = 0
last_pipe = pygame.time.get_ticks()
font = pygame.font.Font(None, 48)
game_active = False
# Game loop
while True:
current_time = pygame.time.get_ticks()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
if game_active:
bird.jump()
else:
bird = Bird()
pipes = []
score = 0
last_pipe = current_time
game_active = True
if event.type == pygame.MOUSEBUTTONDOWN:
if game_active:
bird.jump()
else:
bird = Bird()
pipes = []
score = 0
last_pipe = current_time
game_active = True
# Draw background
screen.fill(SKY_BLUE)
if game_active:
# Update bird
bird.update()
# Create new pipes
if current_time - last_pipe > PIPE_FREQUENCY:
pipes.append(Pipe())
last_pipe = current_time
# Update and check pipes
for pipe in pipes[:]:
pipe.update()
# Remove off-screen pipes
if pipe.x + pipe.width < 0:
pipes.remove(pipe)
# Check for collisions
if (bird.rect.colliderect(pipe.top_pipe) or
bird.rect.colliderect(pipe.bottom_pipe)):
game_active = False
if score > high_score:
high_score = score
# Score points
if not pipe.passed and pipe.x < bird.x:
score += 1
pipe.passed = True
# Check if bird hits the ground or ceiling
if bird.y < 0 or bird.y + bird.rect.height > WINDOW_HEIGHT - 50:
game_active = False
if score > high_score:
high_score = score
# Draw game elements
for pipe in pipes:
pipe.draw()
draw_ground()
bird.draw()
# Draw score
score_text = font.render(f'{score}', True, WHITE)
screen.blit(score_text, (WINDOW_WIDTH // 2 - score_text.get_width() // 2, 50))
if not game_active:
# Draw game over screen
game_over_text = font.render('Game Over!', True, WHITE)
screen.blit(game_over_text,
(WINDOW_WIDTH // 2 - game_over_text.get_width() // 2,
WINDOW_HEIGHT // 3))
score_text = font.render(f'Score: {score}', True, WHITE)
screen.blit(score_text,
(WINDOW_WIDTH // 2 - score_text.get_width() // 2,
WINDOW_HEIGHT // 2))
high_score_text = font.render(f'Best: {high_score}', True, WHITE)
screen.blit(high_score_text,
(WINDOW_WIDTH // 2 - high_score_text.get_width() // 2,
WINDOW_HEIGHT // 2 + 50))
prompt_text = font.render('Click to play!', True, WHITE)
screen.blit(prompt_text,
(WINDOW_WIDTH // 2 - prompt_text.get_width() // 2,
WINDOW_HEIGHT * 2 // 3))
pygame.display.flip()
clock.tick(FPS)
if __name__ == "__main__":
main()