-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflappy_bird.py
More file actions
143 lines (100 loc) · 3.32 KB
/
Copy pathflappy_bird.py
File metadata and controls
143 lines (100 loc) · 3.32 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
import pygame
import time
import random
pygame.init()
display_width = 800 # exercise
display_height = 600 # exercise
black = (0, 0, 0) # exercise
white = (255, 255, 255) # exercise
red = (255, 0, 0) # exercise
pipe_color = (53, 115, 255) # exercise
bird_size = 5
pipe_gap = 100
game_display = pygame.display.set_mode((display_width, display_height))
pygame.display.set_caption('Flappy Bird')
clock = pygame.time.Clock()
class Pipe:
def __init__(self, x, y, height, width):
self.x = x
self.top_y = y
self.height = height
self.bottom_y = self.top_y + self.height + pipe_gap
self.width = width
self.color = pipe_color
def show_score(score):
# Displays score at the top left of screen
font = pygame.font.SysFont(None, 25)
text = font.render("Score " + str(score), True, black)
game_display.blit(text, (0, 0))
def draw_pipe(pipe):
# Draws pipe at coordniate(x, y) with given height, weight, and color
pygame.draw.rect(game_display, pipe.color, [pipe.x, pipe.top_y, pipe.width, pipe.height])
pygame.draw.rect(game_display, pipe.color, [pipe.x, pipe.bottom_y, pipe.width, pipe.height])
def draw_bird(x, y):
# Draws the bird at coordinate (x, y)
pygame.draw.circle(game_display, black, (x, y), 5)
def display_message(text):
# Displays a message in the middle of the screen
font = pygame.font.Font('freesansbold.ttf', 115)
text_surface = font.render(text, True, black)
text_rectangle = text_surface.get_rect()
text_rectangle.center = ((display_width / 2), (display_height / 2))
game_display.blit(text_surface, text_rectangle)
pygame.display.update()
def start_game():
x = int(display_width * 0.3)
y = int(display_height * 0.5)
y_change = 0
gravity = 3
pipe_x = 1200
pipe_y = display_height * -0.3
pipe_speed = 4
pipe_width = 100
pipe_height = 400
pipe = Pipe(pipe_x, pipe_y, pipe_height, pipe_width)
score = 0
game_over = False
while not game_over:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
y_change = -12
in_key_press = True
if event.type == pygame.KEYUP:
if event.key == pygame.K_SPACE:
y_change = 0
in_key_press = False
y = y + y_change + gravity
game_display.fill(white)
draw_pipe(pipe)
pipe.x -= pipe_speed
draw_bird(x, y)
show_score(score)
# establishes boundaries of our game
if y < 0 or y > display_height - bird_size:
display_message("Game Over")
game_over = True
time.sleep(5)
# once pipe goes off screen
if pipe.x < 0:
pipe.x = display_width + pipe_width
pipe.top_y = random.randrange(-0.5 * display_height, 0.2 * display_height)
pipe.bottom_y = pipe.top_y + pipe_height + pipe_gap
score += 1
# check for collisions between bird and pipe
if x > pipe.x and x < pipe.x + pipe.width:
top_pipe_collision = y > pipe.top_y and y < pipe.top_y + pipe_height or y + bird_size > pipe.top_y and y + bird_size < pipe.top_y + bird_size
bottom_pipe_collision = y > pipe.bottom_y and y < pipe.bottom_y + pipe_height or y + bird_size > pipe.bottom_y and y + bird_size < pipe.bottom_y + bird_size
if top_pipe_collision or bottom_pipe_collision:
display_message("Game Over")
game_over = True
time.sleep(5)
pygame.display.update()
clock.tick(60)
if __name__ == '__main__':
start_game()
pygame.quit()
quit()