-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
365 lines (289 loc) · 11 KB
/
main.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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
__version__ = "0.2.0"
from functools import partial
from random import randint, choice
from kivy.utils import platform
from kivy.animation import Animation
from kivy.core.window import Window
from kivy.core.audio import SoundLoader
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.button import Button
from kivy.properties import (
ObjectProperty, NumericProperty, BooleanProperty, StringProperty,
ListProperty,
)
from kivy.vector import Vector
from kivy.clock import Clock
from kivy.core.image import Image
class Explosion(Widget):
frame = NumericProperty(0)
texture = ObjectProperty()
sound = SoundLoader.load('sounds/explosion.ogg')
def __init__(self, **kwargs):
super(Explosion, self).__init__(**kwargs)
self.sound.play()
texture = Image('images/explosion.png').texture
self.textures = [texture.get_region(i*128, 0, 128, 128) for i in range(23)]
self.texture = self.textures[self.frame]
self.bind(frame=self.change_texture)
self.animation = Animation(
frame=22, t='linear', duration=0.6
)
self.animation.start(self)
self.animation.bind(on_complete=self.remove_from_parent)
def change_texture(self, called_by, value):
self.texture = self.textures[int(value)]
def remove_from_parent(self, called_by, value):
self.parent.remove_widget(self)
class Asteroid(Widget):
speed = NumericProperty(4)
angle = NumericProperty(0)
def update(self, dt):
for i in [0, 1]:
if self.pos[i] < 0:
self.pos[i] = Window.size[i]
elif self.pos[i] > Window.size[i]:
self.pos[i] = 0
self.pos = Vector(self.speed, 0).rotate(self.angle) + self.pos
class Shot(Widget):
lifetime = NumericProperty(1)
speed = NumericProperty(4)
angle = NumericProperty(0)
sound = SoundLoader.load('sounds/gun.ogg')
spaceship = ObjectProperty()
def __init__(self, **kwargs):
super(Shot, self).__init__(**kwargs)
self.sound.play()
def update(self, dt):
for i in [0, 1]:
if self.pos[i] < 0:
self.pos[i] = Window.size[i]
elif self.pos[i] > Window.size[i]:
self.pos[i] = 0
self.pos = Vector(self.speed, 0).rotate(self.angle) + self.pos
class Spaceship(Widget):
lives = NumericProperty(3)
points = NumericProperty(0)
speed = NumericProperty(0)
thrust = BooleanProperty(False)
angle = NumericProperty(0)
angle_rotation = NumericProperty(0)
rotate = StringProperty()
sound = SoundLoader.load('sounds/engine.ogg')
def update(self, dt):
for i in [0, 1]:
if self.pos[i] < 0:
self.pos[i] = Window.size[i]
elif self.pos[i] > Window.size[i]:
self.pos[i] = 0
self.angle += self.angle_rotation
if self.thrust:
self.speed = 3
else:
self.speed *= 0.98
self.pos = Vector(self.speed, 0).rotate(self.angle) + self.pos
def thrust_on(self):
if not self.thrust:
self.sound.play()
self.thrust = True
def thrust_off(self):
self.thrust = False
self.sound.stop()
def turn_left(self):
self.angle_rotation = 3
def turn_right(self):
self.angle_rotation = -3
def stop_rotation(self):
self.angle_rotation = 0
def shot(self):
shot = Shot(spaceship=self)
shot.pos = (
Vector(*self.size)/2 - Vector(*shot.size)/2 +
self.pos + Vector(self.size[0] / 2, 0).rotate(self.angle)
)
shot.angle = self.angle
shot.speed += self.speed
self.parent.shots.append(shot)
Clock.schedule_interval(
partial(self.parent.remove_shot, shot), shot.lifetime)
self.parent.add_widget(shot)
class SpaceshipStatus(Widget):
spaceship = ObjectProperty()
class Splash(Button):
pass
class ScreenButtonsLayout(Widget):
pass
class ControlsManager(Widget):
spaceship = ObjectProperty()
def __init__(self, **kwargs):
super(ControlsManager, self).__init__(**kwargs)
self._keyboard = Window.request_keyboard(
self._keyboard_closed, self, 'text')
if self._keyboard.widget:
# If it exists, this widget is a VKeyboard object which you can use
# to change the keyboard layout.
pass
self._keyboard.bind(on_key_down=self._on_keyboard_down)
self._keyboard.bind(on_key_up=self._on_keyboard_up)
if platform == 'android':
Window.release_all_keyboards()
self.add_widget(ScreenButtonsLayout())
self.mapping = {
"up": {
"down": self.spaceship.thrust_on,
"up": self.spaceship.thrust_off
},
"right": {
"down": self.spaceship.turn_right,
"up": self.spaceship.stop_rotation
},
"left": {
"down": self.spaceship.turn_left,
"up": self.spaceship.stop_rotation
},
"spacebar": {
"down": self.spaceship.shot
}
}
def _keyboard_closed(self):
print('My keyboard have been closed!')
self._keyboard.unbind(on_key_down=self._on_keyboard_down)
self._keyboard.unbind(on_key_up=self._on_keyboard_up)
self._keyboard = None
def _on_keyboard_down(self, keyboard, keycode, text, modifiers):
if 'down' in self.mapping.get(keycode[1], {}):
self.mapping[keycode[1]]['down']()
return True
def _on_keyboard_up(self, keyboard, keycode):
if 'up' in self.mapping.get(keycode[1], {}):
self.mapping[keycode[1]]['up']()
class AnimatedBackground(Widget):
background = ObjectProperty()
uv_pos_x_px = NumericProperty()
def __init__(self, **kwargs):
super(AnimatedBackground, self).__init__(**kwargs)
# ToDo: move link to image to kv file
texture = Image('images/debris.png').texture
texture.wrap = 'repeat'
setattr(self, 'background', texture)
self.animate()
self.bind(uv_pos_x_px=self.change_uv_pos)
def change_uv_pos(self, called_by, value):
self.background.uvpos = (
value / self.background.size[0],
self.background.uvpos[1]
)
self.property('background').dispatch(self)
def animate(self, *args):
self.uv_pos_x_px = 0
self.animation = Animation(
uv_pos_x_px=self.background.size[0], t='linear', duration=15
)
self.animation.bind(on_complete=self.animate)
self.animation.start(self)
class SpacePilgrimGame(Widget):
shots = ListProperty()
spaceships = ListProperty()
asteroids = ListProperty()
explosions = ListProperty()
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.frame_schedule = Clock.schedule_interval(self.update, 1.0/60.0)
self.asteroid_schedule = Clock.schedule_interval(self.generate_asteroid, 2)
def game_start(self):
# reset game status
[self.remove_shot(shot) for shot in self.shots]
[self.remove_asteroid(asteroid) for asteroid in self.asteroids]
# start schedule
self.frame_schedule.cancel()
self.frame_schedule = Clock.schedule_interval(self.update, 1.0/60.0)
self.asteroid_schedule.cancel()
self.asteroid_schedule = Clock.schedule_interval(self.generate_asteroid, 2)
self.remove_widget(self.splash)
spaceship = self.add_spaceship()
self.spaceship_status = SpaceshipStatus(spaceship=spaceship)
self.add_widget(self.spaceship_status)
self.spaceship_controls = ControlsManager(spaceship=spaceship)
self.add_widget(self.spaceship_controls)
def game_stop(self):
# create splash screen and centering it
self.splash = Splash()
self.add_widget(self.splash)
if hasattr(self, 'spaceship_status'):
self.remove_widget(self.spaceship_status)
if hasattr(self, 'spaceship_controls'):
self.remove_widget(self.spaceship_controls)
Window.release_all_keyboards()
def update(self, dt):
[spaceship.update(dt) for spaceship in self.spaceships]
[shot.update(dt) for shot in self.shots]
[asteroid.update(dt) for asteroid in self.asteroids]
for asteroid in self.asteroids:
for spaceship in self.spaceships:
if asteroid.collide_widget(spaceship):
self.remove_asteroid(asteroid)
spaceship.lives -= 1
self.add_explosion(asteroid.pos)
if spaceship.lives == 0:
self.game_stop()
self.remove_spaceship(spaceship)
self.add_explosion(spaceship.pos)
for shot in self.shots:
if asteroid.collide_widget(shot):
self.remove_asteroid(asteroid)
shot.spaceship.points += 1
self.remove_shot(shot)
self.add_explosion(asteroid.pos)
def add_spaceship(self):
spaceship = Spaceship()
spaceship.pos = Vector(*self.center) - Vector(*spaceship.size) / 2
self.spaceships.append(spaceship)
self.add_widget(spaceship)
return spaceship
def remove_spaceship(self, spaceship):
spaceship.thrust_off()
self.spaceships.remove(spaceship)
self.remove_widget(spaceship)
def add_explosion(self, pos):
explosion = Explosion()
explosion.pos = pos
self.add_widget(explosion)
self.explosions.append(explosion)
def remove_explosion(self, explosion):
self.remove_widget(explosion)
if explosion in self.explosions:
self.explosions.remove(explosion)
def remove_asteroid(self, asteroid):
self.remove_widget(asteroid)
if asteroid in self.asteroids:
self.asteroids.remove(asteroid)
def remove_shot(self, shot, *largs):
self.remove_widget(shot)
if shot in self.shots:
self.shots.remove(shot)
def generate_asteroid(self, dt):
# use left, bottom positions because asteroids can fly around screen
positions = {
'left': Vector(0, randint(0, Window.size[1])),
'bottom': Vector(randint(0, Window.size[0]), 0),
}
position = choice(list(positions.values()))
if (
len(self.asteroids) < 3 and self.spaceships and
min(map(lambda s: position.distance(s.pos), self.spaceships)) > 200
):
asteroid = Asteroid()
asteroid.pos = position
asteroid.angle = randint(0, 360)
self.add_widget(asteroid)
self.asteroids.append(asteroid)
class SpacePilgrimApp(App):
use_kivy_settings = False
def build(self):
game = SpacePilgrimGame()
game.game_stop()
return game
def on_pause(self):
return True
if __name__ == '__main__':
SpacePilgrimApp().run()