-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFlappy_Bird.py
181 lines (144 loc) · 4.81 KB
/
Flappy_Bird.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
import pygame
from random import *
pygame.init()
window_width = 500
window_height = 500
window = pygame.display.set_mode((window_width, window_height))
WHITE = (255, 255, 255)
YELLOW = (255, 255, 0)
BLUE = (0, 0, 255)
RED = (255, 0, 0)
PINK = (255, 192, 203)
ORANGE = (255, 165, 0)
LIGHT_BLUE = (135,206,235)
GREEN = (0, 200, 0)
BLACK = (0, 0, 0)
def freeze():
global game
display("You lost", (50, 50), 100)
pygame.display.update()
while True:
for event in pygame.event.get():
# print(event, type(pygame.QUIT), pygame.KEYUP)
if event.type == pygame.QUIT:
quit()
if event.type == pygame.KEYUP:
print("i got here")
del game
game = FlappyBird()
game.play_game()
# print(1)
def display(msg, pos=(10, 10), font_size=20, color=BLACK):
font = pygame.font.SysFont(pygame.font.get_default_font(), font_size)
score_surface = font.render(msg, False, color)
window.blit(score_surface, pos)
class Tube:
tube_width = 60
color = GREEN
def __init__(self, x, gap):
self.x = x
self.height1 = randint(0, window_height-gap)
self.height2 = window_height - self.height1 - gap
self.gap = gap
def tick(self, progression_speed):
self.x -= progression_speed
#print(self.x)
def draw(self):
tube1 = [self.x, 0, self.tube_width, self.height1]
tube2 = [self.x, window_height - self.height2, self.tube_width, self.height2]
pygame.draw.rect(window, self.color, tube1)
pygame.draw.rect(window, self.color, tube2)
return tube1, tube2
def hit_tube(self, x, y):
in_width = self.x <= x+Bird.radius and x-Bird.radius <= self.x + self.tube_width
correct_y = y-Bird.radius < self.height1 or y+Bird.radius > window_height - self.height2
return in_width and correct_y
def is_off_screen(self):
return self.x < -self.tube_width
def __repr__(self):
return f"Tube @ {self.x}: gap = {self.gap}"
class Bird:
radius = 15
flap_power = 20
deceleration_rate = 3
color = YELLOW
def __init__(self):
self.x = 50
self.y = window_height//2
self.y_vel = 0
def flap(self):
self.y_vel = self.flap_power
def tick(self):
self.y_vel -= self.deceleration_rate
self.adjust_height()
def adjust_height(self):
self.y -= self.y_vel
def draw(self):
pygame.draw.circle(window, self.color, (self.x, self.y), self.radius)
class FlappyBird:
space_between_tubes = 300
def __init__(self):
self.progression = 0
self.progression_speed = 5
self.tube_gap = 200
self.score = 0
self.bird = Bird()
self.tubes = [Tube(-Tube.tube_width-1, 0)]
self.generate_inital_tubes()
self.game_running = True
def play_game(self):
while self.game_running:
for event in pygame.event.get():
if event == pygame.QUIT:
quit()
self.tick()
self.draw()
def tick(self):
for tube in reversed(self.tubes):
tube.tick(self.progression_speed)
if tube.hit_tube(self.bird.x, self.bird.y):
print("You hit a tube")
self.draw()
freeze()
self.manual_input()
self.bird.tick()
if self.bird.x > self.tubes[1].x:
self.pass_through_tube()
if self.bird.y > window_height - self.bird.radius:
self.draw()
print("You hit the ground")
freeze()
def pass_through_tube(self):
self.tubes.pop(0)
self.make_tube(self.tubes[-1].x + self.space_between_tubes)
self.score += 1
self.progression_speed += 1
self.tube_gap -= 1
def generate_inital_tubes(self):
for i in range(2, 5):
self.tubes.append(Tube(self.space_between_tubes*i, self.tube_gap))
def make_tube(self, x):
self.tubes.append(Tube(x, self.tube_gap))
def manual_input(self):
for event in pygame.event.get():
if event == pygame.KEYDOWN:
self.bird.flap()
keys = pygame.key.get_pressed()
if keys[pygame.K_SPACE]:
self.bird.flap()
def draw_score(self):
scr = f"Score: {self.score}"
msg_color = RED
font = pygame.font.SysFont(pygame.font.get_default_font(), 20)
score_surface = font.render(scr, False, msg_color)
window.blit(score_surface, (10, 10))
def draw(self):
window.fill(LIGHT_BLUE)
for tube in self.tubes:
tube.draw()
self.bird.draw()
self.draw_score()
pygame.display.update()
if __name__ == '__main__':
game = FlappyBird()
game.play_game()