-
Notifications
You must be signed in to change notification settings - Fork 0
/
FlappyBird.py
169 lines (128 loc) · 4.97 KB
/
FlappyBird.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
import pygame
import sys
import time
import random
pygame.init()
clock = pygame.time.Clock()
def draw_floor():
screen.blit(floor_img, (floor_x, 520))
screen.blit(floor_img, (floor_x + 448, 520))
def create_pipes():
pipe_y = random.choice(pipe_height)
top_pipe = pipe_img.get_rect(midbottom=(467, pipe_y - 300))
bottom_pipe = pipe_img.get_rect(midtop=(467, pipe_y))
return top_pipe, bottom_pipe
def pipe_animation():
global game_over, score_time
for pipe in pipes:
if pipe.top < 0:
flipped_pipe = pygame.transform.flip(pipe_img, False, True)
screen.blit(flipped_pipe, pipe)
else:
screen.blit(pipe_img, pipe)
pipe.centerx -= 3
if pipe.right < 0:
pipes.remove(pipe)
if bird_rect.colliderect(pipe):
game_over = True
def draw_score(game_state):
if game_state == "game_on":
score_text = score_font.render(str(score), True, (255, 255, 255))
score_rect = score_text.get_rect(center=(width // 2, 66))
screen.blit(score_text, score_rect)
elif game_state == "game_over":
score_text = score_font.render(f" Score: {score}", True, (255, 255, 255))
score_rect = score_text.get_rect(center=(width // 2, 66))
screen.blit(score_text, score_rect)
high_score_text = score_font.render(f"High Score: {high_score}", True, (255, 255, 255))
high_score_rect = high_score_text.get_rect(center=(width // 2, 506))
screen.blit(high_score_text, high_score_rect)
def score_update():
global score, score_time, high_score
if pipes:
for pipe in pipes:
if 65 < pipe.centerx < 69 and score_time:
score += 1
score_time = False
if pipe.left <= 0:
score_time = True
if score > high_score:
high_score = score
width, height = 350, 622
clock = pygame.time.Clock()
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption("Flappy Bird")
back_img = pygame.image.load("C:/Users/Tanmay Jain/Desktop/Coding/.vs/Learning py/images for flappy bird/img_46.png")
floor_img = pygame.image.load("C:/Users/Tanmay Jain/Desktop/Coding/.vs/Learning py/images for flappy bird/img_50.png")
floor_x = 0
bird_up = pygame.image.load("C:/Users/Tanmay Jain/Desktop/Coding/.vs/Learning py/images for flappy bird/img_47.png")
bird_down = pygame.image.load("C:/Users/Tanmay Jain/Desktop/Coding/.vs/Learning py/images for flappy bird/img_48.png")
bird_mid = pygame.image.load("C:/Users/Tanmay Jain/Desktop/Coding/.vs/Learning py/images for flappy bird/img_49.png")
birds = [bird_up, bird_mid, bird_down]
bird_index = 0
bird_flap = pygame.USEREVENT
pygame.time.set_timer(bird_flap, 200)
bird_img = birds[bird_index]
bird_rect = bird_img.get_rect(center=(67, 622 // 2))
bird_movement = 0
gravity = 0.2
pipe_img = pygame.image.load("C:/Users/Tanmay Jain/Desktop/Coding/.vs/Learning py/images for flappy bird/greenpipe.png")
pipe_height = [400, 350, 533, 490]
pipes = []
create_pipe = pygame.USEREVENT + 1
pygame.time.set_timer(create_pipe, 1200)
game_over = False
over_img = pygame.image.load("C:/Users/Tanmay Jain/Desktop/Coding/.vs/Learning py/images for flappy bird/img_45.png").convert_alpha()
over_rect = over_img.get_rect(center=(width // 2, height // 2))
score = 0
high_score = 0
score_time = True
score_font = pygame.font.Font("freesansbold.ttf", 27)
running = True
while running:
clock.tick(120)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE and not game_over:
bird_movement = 0
bird_movement = -7
if event.key == pygame.K_SPACE and game_over:
game_over = False
pipes = []
bird_movement = 0
bird_rect = bird_img.get_rect(center=(67, 622 // 2))
score_time = True
score = 0
if event.type == bird_flap:
bird_index += 1
if bird_index > 2:
bird_index = 0
bird_img = birds[bird_index]
bird_rect = bird_up.get_rect(center=bird_rect.center)
if event.type == create_pipe:
pipes.extend(create_pipes())
screen.blit(floor_img, (floor_x, 550))
screen.blit(back_img, (0, 0))
if not game_over:
bird_movement += gravity
bird_rect.centery += bird_movement
rotated_bird = pygame.transform.rotozoom(bird_img, bird_movement * -6, 1)
if bird_rect.top < 5 or bird_rect.bottom >= 550:
game_over = True
screen.blit(rotated_bird, bird_rect)
pipe_animation()
score_update()
draw_score("game_on")
elif game_over:
screen.blit(over_img, over_rect)
draw_score("game_over")
floor_x -= 1
if floor_x < -448:
floor_x = 0
draw_floor()
pygame.display.update()
pygame.quit()
sys.exit()