forked from MaxRohowsky/flappy-bird
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
217 lines (174 loc) · 6.13 KB
/
main.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
import pygame
from sys import exit
import random
pygame.init()
clock = pygame.time.Clock()
# Window
win_height = 720
win_width = 551
window = pygame.display.set_mode((win_width, win_height))
# Images
bird_images = [pygame.image.load("assets/bird_down.png"),
pygame.image.load("assets/bird_mid.png"),
pygame.image.load("assets/bird_up.png")]
skyline_image = pygame.image.load("assets/background.png")
ground_image = pygame.image.load("assets/ground.png")
top_pipe_image = pygame.image.load("assets/pipe_top.png")
bottom_pipe_image = pygame.image.load("assets/pipe_bottom.png")
game_over_image = pygame.image.load("assets/game_over.png")
start_image = pygame.image.load("assets/start.png")
# Game
scroll_speed = 1
bird_start_position = (100, 250)
score = 0
font = pygame.font.SysFont('Segoe', 26)
game_stopped = True
class Bird(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = bird_images[0]
self.rect = self.image.get_rect()
self.rect.center = bird_start_position
self.image_index = 0
self.vel = 0
self.flap = False
self.alive = True
def update(self, user_input):
# Animate Bird
if self.alive:
self.image_index += 1
if self.image_index >= 30:
self.image_index = 0
self.image = bird_images[self.image_index // 10]
# Gravity and Flap
self.vel += 0.5
if self.vel > 7:
self.vel = 7
if self.rect.y < 500:
self.rect.y += int(self.vel)
if self.vel == 0:
self.flap = False
# Rotate Bird
self.image = pygame.transform.rotate(self.image, self.vel * -7)
# User Input
if user_input[pygame.K_SPACE] and not self.flap and self.rect.y > 0 and self.alive:
self.flap = True
self.vel = -7
class Pipe(pygame.sprite.Sprite):
def __init__(self, x, y, image, pipe_type):
pygame.sprite.Sprite.__init__(self)
self.image = image
self.rect = self.image.get_rect()
self.rect.x, self.rect.y = x, y
self.enter, self.exit, self.passed = False, False, False
self.pipe_type = pipe_type
def update(self):
# Move Pipe
self.rect.x -= scroll_speed
if self.rect.x <= -win_width:
self.kill()
# Score
global score
if self.pipe_type == 'bottom':
if bird_start_position[0] > self.rect.topleft[0] and not self.passed:
self.enter = True
if bird_start_position[0] > self.rect.topright[0] and not self.passed:
self.exit = True
if self.enter and self.exit and not self.passed:
self.passed = True
score += 1
class Ground(pygame.sprite.Sprite):
def __init__(self, x, y):
pygame.sprite.Sprite.__init__(self)
self.image = ground_image
self.rect = self.image.get_rect()
self.rect.x, self.rect.y = x, y
def update(self):
# Move Ground
self.rect.x -= scroll_speed
if self.rect.x <= -win_width:
self.kill()
def quit_game():
# Exit Game
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
# Game Main Method
def main():
global score
# Instantiate Bird
bird = pygame.sprite.GroupSingle()
bird.add(Bird())
# Setup Pipes
pipe_timer = 0
pipes = pygame.sprite.Group()
# Instantiate Initial Ground
x_pos_ground, y_pos_ground = 0, 520
ground = pygame.sprite.Group()
ground.add(Ground(x_pos_ground, y_pos_ground))
run = True
while run:
# Quit
quit_game()
# Reset Frame
window.fill((0, 0, 0))
# User Input
user_input = pygame.key.get_pressed()
# Draw Background
window.blit(skyline_image, (0, 0))
# Spawn Ground
if len(ground) <= 2:
ground.add(Ground(win_width, y_pos_ground))
# Draw - Pipes, Ground and Bird
pipes.draw(window)
ground.draw(window)
bird.draw(window)
# Show Score
score_text = font.render('Score: ' + str(score), True, pygame.Color(255, 255, 255))
window.blit(score_text, (20, 20))
# Update - Pipes, Ground and Bird
if bird.sprite.alive:
pipes.update()
ground.update()
bird.update(user_input)
# Collision Detection
collision_pipes = pygame.sprite.spritecollide(bird.sprites()[0], pipes, False)
collision_ground = pygame.sprite.spritecollide(bird.sprites()[0], ground, False)
if collision_pipes or collision_ground:
bird.sprite.alive = False
if collision_ground:
window.blit(game_over_image, (win_width // 2 - game_over_image.get_width() // 2,
win_height // 2 - game_over_image.get_height() // 2))
if user_input[pygame.K_r]:
score = 0
break
# Spawn Pipes
if pipe_timer <= 0 and bird.sprite.alive:
x_top, x_bottom = 550, 550
y_top = random.randint(-600, -480)
y_bottom = y_top + random.randint(90, 130) + bottom_pipe_image.get_height()
pipes.add(Pipe(x_top, y_top, top_pipe_image, 'top'))
pipes.add(Pipe(x_bottom, y_bottom, bottom_pipe_image, 'bottom'))
pipe_timer = random.randint(180, 250)
pipe_timer -= 1
clock.tick(60)
pygame.display.update()
# Menu
def menu():
global game_stopped
while game_stopped:
quit_game()
# Draw Menu
window.fill((0, 0, 0))
window.blit(skyline_image, (0, 0))
window.blit(ground_image, Ground(0, 520))
window.blit(bird_images[0], (100, 250))
window.blit(start_image, (win_width // 2 - start_image.get_width() // 2,
win_height // 2 - start_image.get_height() // 2))
# User Input
user_input = pygame.key.get_pressed()
if user_input[pygame.K_SPACE]:
main()
pygame.display.update()
menu()