forked from LeonMarqs/Flappy-bird-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflappy.py
More file actions
240 lines (185 loc) · 7.39 KB
/
flappy.py
File metadata and controls
240 lines (185 loc) · 7.39 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
import pygame
import random
import time
import math
from pygame.locals import *
# VARIABLES
SCREEN_WIDTH = 400
SCREEN_HEIGHT = 600
SPEED = 20
GRAVITY = 2.5
GAME_SPEED = 15
GROUND_WIDTH = 2 * SCREEN_WIDTH
GROUND_HEIGHT = 100
PIPE_WIDTH = 80
PIPE_HEIGHT = 500
PIPE_GAP = 150
pygame.init()
class Bird(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.images = [
pygame.image.load('assets/sprites/bluebird-upflap.png').convert_alpha(),
pygame.image.load('assets/sprites/bluebird-midflap.png').convert_alpha(),
pygame.image.load('assets/sprites/bluebird-downflap.png').convert_alpha()
]
self.speed = SPEED
self.current_image = 0
self.image = self.images[self.current_image]
self.mask = pygame.mask.from_surface(self.image)
self.rect = self.image.get_rect()
self.rect[0] = SCREEN_WIDTH / 6
self.rect[1] = SCREEN_HEIGHT / 2
def update(self):
self.current_image = (self.current_image + 1) % 3
self.image = self.images[self.current_image]
self.speed += GRAVITY
self.rect[1] += self.speed
def bump(self):
self.speed = -SPEED
def begin(self):
self.current_image = (self.current_image + 1) % 3
self.image = self.images[self.current_image]
class Pipe(pygame.sprite.Sprite):
def __init__(self, inverted, xpos, ysize):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load('assets/sprites/pipe-green.png').convert_alpha()
self.image = pygame.transform.scale(self.image, (PIPE_WIDTH, PIPE_HEIGHT))
self.rect = self.image.get_rect()
self.rect[0] = xpos
if inverted:
self.image = pygame.transform.flip(self.image, False, True)
self.rect[1] = - (self.rect[3] - ysize)
else:
self.rect[1] = SCREEN_HEIGHT - ysize
self.mask = pygame.mask.from_surface(self.image)
def update(self):
self.rect[0] -= GAME_SPEED
class Ground(pygame.sprite.Sprite):
def __init__(self, xpos):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load('assets/sprites/base.png').convert_alpha()
self.image = pygame.transform.scale(self.image, (GROUND_WIDTH, GROUND_HEIGHT))
self.mask = pygame.mask.from_surface(self.image)
self.rect = self.image.get_rect()
self.rect[0] = xpos
self.rect[1] = SCREEN_HEIGHT - GROUND_HEIGHT
def update(self):
self.rect[0] -= GAME_SPEED
def is_off_screen(sprite):
return sprite.rect[0] < -(sprite.rect[2])
def get_random_pipes(xpos):
size = random.randint(100, 300)
pipe = Pipe(False, xpos, size)
pipe_inverted = Pipe(True, xpos, SCREEN_HEIGHT - size - PIPE_GAP)
return pipe, pipe_inverted
def display_score(screen, score):
font = pygame.font.Font(None, 36)
text = font.render(f"Score: {score}", True, (255, 255, 255))
screen.blit(text, (10, 10))
def game_over_screen(screen, score):
font = pygame.font.Font(None, 40)
text = font.render(f"Game Over! Score: {score}", True, (255, 0, 0))
restart_text = font.render("Press SPACE to Restart", True, (255, 255, 255))
text_rect = text.get_rect(center=(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 3))
restart_text_rect = restart_text.get_rect(center=(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2))
screen.blit(text, text_rect)
screen.blit(restart_text, restart_text_rect)
pygame.display.update()
waiting = True
while waiting:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
exit()
if event.type == KEYDOWN:
if event.key == K_SPACE:
waiting = False
def main_game_loop(SPEED, GAME_SPEED):
bird_group = pygame.sprite.Group()
bird = Bird()
bird_group.add(bird)
ground_group = pygame.sprite.Group()
for i in range(2):
ground = Ground(GROUND_WIDTH * i)
ground_group.add(ground)
pipe_group = pygame.sprite.Group()
for i in range(2):
pipes = get_random_pipes(SCREEN_WIDTH * i + 800)
pipe_group.add(pipes[0])
pipe_group.add(pipes[1])
clock = pygame.time.Clock()
score = 0
begin = True
while begin:
clock.tick(15)
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
exit()
if event.type == KEYDOWN:
if event.key == K_SPACE or event.key == K_UP:
bird.bump()
begin = False
screen.blit(BACKGROUND, (0, 0))
screen.blit(BEGIN_IMAGE, (120, 150))
if is_off_screen(ground_group.sprites()[0]):
ground_group.remove(ground_group.sprites()[0])
new_ground = Ground(GROUND_WIDTH - 20)
ground_group.add(new_ground)
bird.begin()
ground_group.update()
bird_group.draw(screen)
ground_group.draw(screen)
pygame.display.update()
while True:
clock.tick(15)
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
exit()
if event.type == KEYDOWN:
if event.key == K_SPACE or event.key == K_UP:
bird.bump()
elif event.key == K_f:
bird.rect[0] += 150
elif event.key == K_d:
bird.rect[0] -= 150
screen.blit(BACKGROUND, (0, 0))
if is_off_screen(ground_group.sprites()[0]):
ground_group.remove(ground_group.sprites()[0])
new_ground = Ground(GROUND_WIDTH - 20)
ground_group.add(new_ground)
if is_off_screen(pipe_group.sprites()[0]):
pipe_group.remove(pipe_group.sprites()[0])
pipe_group.remove(pipe_group.sprites()[0])
pipes = get_random_pipes(SCREEN_WIDTH * 2)
pipe_group.add(pipes[0])
pipe_group.add(pipes[1])
score += 1
bird_group.update()
ground_group.update()
pipe_group.update()
bird_group.draw(screen)
pipe_group.draw(screen)
ground_group.draw(screen)
display_score(screen, score)
if(score == 2):
SPEED += 20
GAME_SPEED += 20
pygame.display.update()
#if (pygame.sprite.groupcollide(bird_group, ground_group, False, False, pygame.sprite.collide_mask) or
# pygame.sprite.groupcollide(bird_group, pipe_group, False, False, pygame.sprite.collide_mask)):
# time.sleep(1)
# game_over_screen(screen, score)
#return
# Initialize screen
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption('Flappy Bird')
# Load images
BACKGROUND = pygame.image.load('assets/sprites/background-day.png')
BACKGROUND = pygame.transform.scale(BACKGROUND, (SCREEN_WIDTH, SCREEN_HEIGHT))
BEGIN_IMAGE = pygame.image.load('assets/sprites/message.png').convert_alpha()
# Main entry point
while True:
main_game_loop(SPEED, GAME_SPEED)