-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.py
executable file
·248 lines (193 loc) · 7.25 KB
/
game.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
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
241
242
243
244
245
246
247
248
#!/usr/bin/env python
__version__ = '1.0'
import itertools
import random
from kivy.app import App
from kivy.clock import Clock
from kivy.core.window import Window
from kivy.uix.image import Image
from kivy.uix.label import Label
from kivy.uix.widget import Widget
from kivy.core.audio import SoundLoader
sfx_flap = SoundLoader.load('audio/flap.wav')
sfx_score = SoundLoader.load('audio/score.wav')
sfx_die = SoundLoader.load('audio/die.wav')
from config import config
import PID
class Sprite(Image):
def __init__(self, **kwargs):
super(Sprite, self).__init__(**kwargs)
self.size = self.texture_size
class Menu(Widget):
def __init__(self):
super(Menu, self).__init__()
self.add_widget(Sprite(source='images/background.png'))
self.size = self.children[0].size
# self.size = (200, 1820)
self.add_widget(Ground(source='images/ground.png'))
self.add_widget(Label(center=self.center, text="Tap to Start"))
def on_touch_down(self, *ignore):
parent = self.parent
parent.remove_widget(self)
parent.add_widget(Game())
class Bird(Sprite):
def __init__(self, pos):
super(Bird, self).__init__(source='atlas://images/bird_anim/wing-up', pos=pos)
min_vel = config['min_velocity']
max_vel = config['max_velocity']
self._pid = PID.PID(KP=13.197,
KI=0.0,
KD=0.256,
min_cor=min_vel,
max_cor=max_vel)
self.velocity_y = 0
self._gravity = config['gravity']
self._flap_images = itertools.cycle(('atlas://images/bird_anim/wing-up',
'atlas://images/bird_anim/wing-mid',
'atlas://images/bird_anim/wing-down',
'atlas://images/bird_anim/wing-mid'))
self._glide_image = 'atlas://images/bird_anim/wing-mid'
def update(self, dt):
self.velocity_y += (self._gravity * dt)
self.y += (self.velocity_y * dt)
# print "Velocity: {0}, position: {1}".format(self.velocity_y, self.y)
if abs(self.velocity_y) > 3:
self.source = next(self._flap_images)
else:
self.source = self._glide_image
def fly_to(self, height):
self._pid.target = height - (self.height / 2)
def flap(self, passed_time):
'''
Increase, or decrease power of flapping of the flappy bird.
@param passed_time - amount of time passed since last flap
'''
self.velocity_y = self._pid.make_correction(self.y, passed_time)
def on_touch_down(self, touch):
# self.velocity_y = 2.5
self.source = 'atlas://images/bird_anim/wing-down'
sfx_flap.play()
x, y = touch.pos
self.fly_to(y)
class Background(Widget):
def __init__(self, source):
super(Background, self).__init__()
self.image = Sprite(source=source)
self.add_widget(self.image)
self.size = self.image.size
self.image_dupe = Sprite(source=source, x=self.width)
self.add_widget(self.image_dupe)
def update(self):
self.image.x -= 2
self.image_dupe.x -= 2
if self.image.right <= 0:
self.image.x = 0
self.image_dupe.x = self.width
class Ground(Sprite):
def update(self):
self.x -= 2
if self.x < -24:
self.x += 24
class Pipe(Widget):
def __init__(self, pos):
super(Pipe, self).__init__(pos=pos)
self.top_image = Sprite(source='images/pipe_top.png')
self.top_image.pos = (self.x, self.y + 3.5 * 24)
self.add_widget(self.top_image)
self.bottom_image = Sprite(source='images/pipe_bottom.png')
self.bottom_image.pos = (self.x, self.y - self.bottom_image.height)
self.add_widget(self.bottom_image)
self.width = self.top_image.width
self.scored = False
@property
def mid_point(self):
'''
The openinig in between the pipes - mid-point.
'''
top_bottom = self.top_image.y
bottom_top = self.bottom_image.y + self.bottom_image.height
return (top_bottom + bottom_top) / 2
@property
def x_pos(self):
return self.top_image.pos[0]
def update(self):
self.x -= 2
self.top_image.x = self.bottom_image.x = self.x
if self.right < 0:
self.parent.remove_widget(self)
class Pipes(Widget):
add_pipe = 0
def update(self, dt):
for child in list(self.children):
child.update()
self.add_pipe -= dt
if self.add_pipe < 0:
y = random.randint(self.y + 50, self.height - 50 - 3.5 * 24)
self.add_widget(Pipe(pos=(self.width, y)))
self.add_pipe = 1.5
class Game(Widget):
def __init__(self):
super(Game, self).__init__()
self.background = Background(source='images/background.png')
self.size = self.background.size
self.add_widget(self.background)
self.painter = Widget()
self.painter.size = self.background.size
self.add_widget(self.painter)
self.bird = Bird(pos=(50, self.height / 2))
self.bird.fly_to(100)
self.add_widget(self.bird)
self.ground = Ground(source='images/ground.png')
self.pipes = Pipes(pos=(0, self.ground.height), size=self.size)
self.add_widget(self.pipes)
self.add_widget(self.ground)
self.score_label = Label(center_x=self.center_x, top=self.top - 30, text='0')
self.add_widget(self.score_label)
self.over_label = Label(center=self.center, opacity=0, text="Game over!")
self.add_widget(self.over_label)
Clock.schedule_interval(self.update, 1.0/config['fps'])
self.game_over = False
self.score = 0
def update(self, dt):
self.painter.canvas.clear()
if self.game_over:
Clock.unschedule(self.update)
self.background.update()
self.bird.update(dt)
self.bird.flap(dt)
self.ground.update()
self.pipes.update(dt)
with self.painter.canvas:
for pipe in self.pipes.children:
if (pipe.x_pos - self.bird.x) <= 100:
self.bird.fly_to(pipe.mid_point)
if self.bird.collide_widget(self.ground):
self.game_over = True
for pipe in self.pipes.children:
if pipe.top_image.collide_widget(self.bird):
self.game_over = True
elif pipe.bottom_image.collide_widget(self.bird):
self.game_over = True
elif not pipe.scored and pipe.right < self.bird.x:
pipe.scored = True
self.score += 1
self.score_label.text = str(self.score)
sfx_score.play()
if self.game_over:
sfx_die.play()
self.over_label.opacity = 1
self.bind(on_touch_down=self._on_touch_down)
def _on_touch_down(self, *ignore):
parent = self.parent
parent.remove_widget(self)
parent.add_widget(Menu())
class GameApp(App):
def build(self):
top = Widget()
top.add_widget(Menu())
Window.size = top.children[0].size
return top
def main():
GameApp().run()
if __name__ == "__main__":
main()