Bird
https://simplegametutorials.github.io/pygamezero/
bird.py
1import pgzrun
2import random
3
4
5def new_pipe_space_y():# {{{
6 # global pipe_space_y
7 # global pipe_x
8 pipe_space_y_min = 54
9 pipe_space_y = random.randint(pipe_space_y_min, playing_area_height - pipe_space_y_min)
10 # pipe_x = playing_area_width
11 return pipe_space_y
12
13# }}}
14
15WIDTH = 300
16HEIGHT = 388
17
18playing_area_width = WIDTH
19playing_area_height = HEIGHT
20pipe_space_height = 160
21pipe_width = 54
22bird_x = 62
23bird_width = 30
24bird_height = 25
25
26bird_y = 200
27bird_y_speed = 200
28pipe_space_y_min = 54
29pipe_x = playing_area_width
30pipe_space_y = random.randint(pipe_space_y_min, playing_area_height - pipe_space_y_min)
31
32
33def reset():# {{{
34 global upcoming_pipe
35 global pipe_1_x
36 global pipe_2_x
37 global pipe_1_x
38 global pipe_1_space_y
39 global pipe_2_x
40 global pipe_2_space_y
41 global bird_y
42 global score
43 score = 0
44 upcoming_pipe = 1
45
46
47 pipe_1_x = playing_area_width
48 pipe_2_x = pipe_1_x + ((playing_area_width + pipe_width) / 2)
49 pipe_1_x = playing_area_width
50 pipe_1_space_y = new_pipe_space_y()
51 pipe_2_x = playing_area_width + ((playing_area_width + pipe_width) / 2)
52 pipe_2_space_y = new_pipe_space_y()
53 bird_y = 200
54# }}}
55def update(dt):# {{{
56 global upcoming_pipe
57 global bird_y, bird_y_speed
58 global pipe_width
59 global pipe_1_x, pipe_2_x
60 global pipe_1_space_y, pipe_2_space_y
61 global score
62
63 if (upcoming_pipe == 1 and bird_x > (pipe_1_x + pipe_width)):
64 score += 1
65 upcoming_pipe = 2
66 print(str(upcoming_pipe))
67
68 if upcoming_pipe == 2 and bird_x > (pipe_2_x + pipe_width):
69 score += 1
70 upcoming_pipe = 1
71 print(str(upcoming_pipe))
72
73 bird_y_speed += 516 * dt
74 bird_y += bird_y_speed * dt
75
76 def move_pipe(pipe_x, pipe_space_y):
77 pipe_x -= 60 * dt
78
79 if pipe_x + pipe_width < 0:
80 pipe_x = playing_area_width
81 pipe_space_y = new_pipe_space_y()
82
83 return pipe_x, pipe_space_y
84 pipe_1_x, pipe_1_space_y = move_pipe(pipe_1_x, pipe_1_space_y)
85 pipe_2_x, pipe_2_space_y = move_pipe(pipe_2_x, pipe_2_space_y)
86
87
88 def is_bird_colliding_with_the_pipe(pipe_x, pipe_space_y):
89 bird_collide = (
90 bird_x < pipe_x + pipe_width
91 and
92 bird_x + bird_width > pipe_x
93 and
94 (
95 bird_y < pipe_space_y
96 or
97 bird_y + bird_height > pipe_space_y + pipe_space_height
98 )
99 )
100 return bird_collide
101
102 if (is_bird_colliding_with_the_pipe(pipe_1_x, pipe_1_space_y)
103 or
104 is_bird_colliding_with_the_pipe(pipe_2_x, pipe_2_space_y)
105 or
106 bird_y > playing_area_height
107 ):
108 reset()
109
110
111
112
113
114# }}}
115def draw():# {{{
116 def draw_pipe(pipe_x, pipe_space_y):
117 # Pipe
118 screen.draw.filled_rect(
119 Rect((pipe_x, 0), (pipe_width, pipe_space_y)),
120 color=(94, 201, 72)
121 )
122
123 screen.draw.filled_rect(
124 Rect((pipe_x, pipe_space_y + pipe_space_height), (pipe_width, playing_area_height - pipe_space_y - pipe_space_height)),
125 color=(94, 201, 72)
126 )
127
128 screen.fill((0, 0, 0))
129
130 # Background
131 screen.draw.filled_rect(
132 Rect((0, 0), (playing_area_width, playing_area_height)),
133 color=(35, 92, 118)
134 )
135
136 draw_pipe(pipe_1_x, pipe_1_space_y)
137 draw_pipe(pipe_2_x, pipe_2_space_y)
138
139 # Bird
140 screen.draw.filled_rect(
141 Rect((bird_x, bird_y), (bird_width, bird_height)),
142 color=(224, 214, 68)
143 )
144
145 screen.draw.text("%d" % score, (15, 15))
146# }}}
147def on_key_down():# {{{
148 global bird_y_speed
149
150 if bird_y > 0:
151 bird_y_speed = -165
152 # global pipe_1_space_y, pipe_2_space_y
153 # pipe_1_space_y = new_pipe_space_y()
154 # pipe_2_space_y = new_pipe_space_y()
155
156
157# }}}
158
159reset()
160pgzrun.go()