-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathflappybird.py
executable file
·100 lines (76 loc) · 1.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
import pgzrun
import random
TITLE = 'Flappy Bird'
WIDTH = 400
HEIGHT = 708
# These constants control the difficulty of the game
GAP = 130
GRAVITY = 0.3
FLAP_STRENGTH = 6.5
SPEED = 3
bird = Actor('bird1', (75, 200))
bird.dead = False
bird.score = 0
bird.vy = 0
storage.setdefault('highscore', 0)
def reset_pipes():
pipe_gap_y = random.randint(200, HEIGHT - 200)
pipe_top.pos = (WIDTH, pipe_gap_y - GAP // 2)
pipe_bottom.pos = (WIDTH, pipe_gap_y + GAP // 2)
pipe_top = Actor('top', anchor=('left', 'bottom'))
pipe_bottom = Actor('bottom', anchor=('left', 'top'))
reset_pipes() # Set initial pipe positions.
def update_pipes():
pipe_top.left -= SPEED
pipe_bottom.left -= SPEED
if pipe_top.right < 0:
reset_pipes()
if not bird.dead:
bird.score += 1
if bird.score > storage['highscore']:
storage['highscore'] = bird.score
def update_bird():
uy = bird.vy
bird.vy += GRAVITY
bird.y += (uy + bird.vy) / 2
bird.x = 75
if not bird.dead:
if bird.vy < -3:
bird.image = 'bird2'
else:
bird.image = 'bird1'
if bird.colliderect(pipe_top) or bird.colliderect(pipe_bottom):
bird.dead = True
bird.image = 'birddead'
if not 0 < bird.y < 720:
bird.y = 200
bird.dead = False
bird.score = 0
bird.vy = 0
reset_pipes()
def update():
update_pipes()
update_bird()
def on_key_down():
if not bird.dead:
bird.vy = -FLAP_STRENGTH
def draw():
screen.blit('background', (0, 0))
pipe_top.draw()
pipe_bottom.draw()
bird.draw()
screen.draw.text(
str(bird.score),
color='white',
midtop=(WIDTH // 2, 10),
fontsize=70,
shadow=(1, 1)
)
screen.draw.text(
"Best: {}".format(storage['highscore']),
color=(200, 170, 0),
midbottom=(WIDTH // 2, HEIGHT - 10),
fontsize=30,
shadow=(1, 1)
)
pgzrun.go()