-
-
Notifications
You must be signed in to change notification settings - Fork 95
/
Copy pathtargets.py
87 lines (64 loc) · 2.4 KB
/
targets.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
import logging
import ppb
from ppb import keycodes
from ppb import Vector
class MoverMixin(ppb.BaseSprite):
velocity = Vector(0, 0)
def on_update(self, update, signal):
self.position += self.velocity * update.time_delta
class Player(MoverMixin, ppb.BaseSprite):
# We handle movement by mapping each key to a velocity vector
# and adding it on press and subtracting it on release.
left_vector = Vector(-1, 0)
right_vector = Vector(1, 0)
_rotation = 180
def on_key_pressed(self, event, signal):
if event.key in (keycodes.A, keycodes.Left):
self.velocity += self.left_vector
elif event.key in (keycodes.D, keycodes.Right):
self.velocity += self.right_vector
elif event.key is keycodes.Space:
self._fire_bullet(event.scene)
def on_key_released(self, event, signal):
if event.key in (keycodes.A, keycodes.Left):
self.velocity -= self.left_vector
elif event.key in (keycodes.D, keycodes.Right):
self.velocity -= self.right_vector
def on_button_pressed(self, event, signal):
if event.button is ppb.buttons.Primary:
self._fire_bullet(event.scene)
def _fire_bullet(self, scene):
scene.add(
Bullet(pos=self.position),
tags=['bullet'],
)
class Bullet(MoverMixin, ppb.BaseSprite):
velocity = Vector(0, 2)
_rotation = 180
def on_update(self, update, signal):
super().on_update(update, signal) # Execute movement
scene = update.scene
if self.position.y > scene.main_camera.frame_bottom:
scene.remove(self)
else:
for target in scene.get(tag='target'):
d = (target.position - self.position).length
if d <= target.radius:
scene.remove(self)
scene.remove(target)
break
class Target(ppb.BaseSprite):
radius = 0.5
class GameScene(ppb.BaseScene):
def __init__(self, *p, **kw):
super().__init__(*p, **kw)
# Set up sprites
self.add(Player(pos=Vector(0, 0)), tags=['player'])
# 5 targets in x = -3.75 -> 3.75, with margin
for x in (-3, -1.5, 0, 1.5, 3):
self.add(Target(pos=Vector(x, 1.875)), tags=['target'])
if __name__ == "__main__":
ppb.run(
starting_scene=GameScene,
log_level=logging.DEBUG,
)