-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsample_actors.py
234 lines (190 loc) · 7.11 KB
/
sample_actors.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
from pdb import set_trace
from math import degrees, radians, cos, sin, pi
import time
import random
import pygame
import util
from actor import SpriteActor
from config import *
class VectorActor(SpriteActor):
def __init__(self, image_angle_deg=0, angle=0, velocity=0, groups=(), *args, **kwargs):
super().__init__(groups=groups, *args, **kwargs)
self.angle = angle
self.orig_image_angle = radians(image_angle_deg)
self.image_angle = self.orig_image_angle
self.velocity = velocity
self.render_props += ['angle', 'orig_image_angle']
self.rotate()
def rotate(self):
if self.image_angle != self.angle:
rot_deg = degrees(self.angle - self.orig_image_angle)
self.image = util.rotate(self.orig_image, rot_deg)
self.image_angle = self.angle
def update(self):
(a, m, (x, y)) = self.angle, self.velocity, self.pos
(dx, dy) = (m * cos(a), m * sin(a))
x1, y1 = x + dx, y + dy
self.pos = (x1, y1)
self.rect = self.rect.move(round(x1 - x), round(y - y1))
self.rotate()
super().update()
def bump(self):
pass
class Robot(VectorActor):
"""Can take damage of bump() until hitpoints is down to zero.
"""
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.hitpoints = 1
self.old_rect = self.rect # enable step-back on bumping an object
def take_damage(self, damage):
self.hitpoints -= damage
if self.hitpoints <= 0:
Explosion(center=self.rect.center, angle=self.angle, groups=self.groups())
self.die()
if damage:
self.append_sound('hit')
def bump(self, damage=0):
self.rect = self.old_rect
self.velocity = 0
if damage:
self.take_damage(damage)
def update(self):
self.old_rect = self.rect
super().update()
class BasicRobot(Robot):
"""A dumb robot that goes in circles
"""
def __init__(self, velocity=BASE_SPEED/2, image_angle_deg=ORIG_ANGLE, *args, **kwargs):
super().__init__(velocity=velocity, image_angle_deg=image_angle_deg, *args, **kwargs)
def bump(self, damage=0):
self.rect = self.old_rect
self.angle += radians(73)
if self.angle >= 4*pi:
self.angle -= 4*pi
if damage:
self.take_damage(damage)
def update(self):
self.angle += radians(1)
if self.angle >= 4*pi:
self.angle -= 4*pi
super().update()
class Explosion(VectorActor):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.time = time.time()
self.last_rotate = 0.0
self.physical = False
self.append_sound('boom')
def update(self):
super().update()
now = time.time()
if not self.last_rotate or now - self.last_rotate > 0.1:
self.last_rotate = now
self.angle = random.random() * 2 * pi
if now - self.time >= + 3.0:
self.die()
class Score(SpriteActor):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.physical = False
self.value = 0
self.showing = -1
self.font = pygame.font.SysFont('serif', 24)
def update(self):
if self.value != self.showing:
# render(text, antialias, color (RGBA), background=None): return Surface
t = "%5d" % self.value
text = self.font.render(t, 1, (0, 0, 0, 0), (200, 200, 200))
self.image.blit(text, (0, 0)) # in location (0,0)
self.showing = self.value
class Mine(SpriteActor):
def __init__(self, who=None, ttl=MINE_TTL, *args, **kwargs):
super().__init__(ttl=ttl, *args, **kwargs)
self.who = who
self.hitpoints = 1
self.ttl_callback = self.blast
def bump(self): # dropped over a wall
self.die()
def blast(self):
for i in range(36):
b = Bullet(self.who, self.rect.center, angle=i*10, ttl=MINE_BLAST_TTL, groups=self.groups())
i == 0 and b.append_sound('boom')
self.die()
class Bullet(VectorActor):
def __init__(self, shooter, center, angle, *args, **kwargs):
super().__init__(velocity=BASE_SPEED, center=center, angle=angle, *args, **kwargs)
self.shooter = shooter
self.damage = 1
self.hitpoints = 1
def bounce(self, wall):
if isinstance(wall, VWall):
self.angle = pi - self.angle
self.append_sound('bounce')
elif isinstance(wall, HWall):
self.angle = -self.angle
self.append_sound('bounce')
def bump(self, damage=0): # hit a mine or a wall
self.die()
class Wall(SpriteActor):
def __init__(self, topleft, groups, angle=0, *args, **kwargs):
super().__init__(topleft=topleft, groups=groups, *args, **kwargs)
angle = angle
class VWall(Wall):
pass
class HWall(Wall):
pass
class MinedropperRobot(Robot):
def __init__(self, velocity=BASE_SPEED/5, image_angle_deg=ORIG_ANGLE, *args, **kwargs):
super().__init__(velocity=velocity, image_angle_deg=image_angle_deg, *args, **kwargs)
self.hitpoints = 5
self.delta = 0.0
self.deltaDirection = "up"
self.nextMine = 0.0
def update(self):
super().update()
if self.deltaDirection == "up":
self.delta += radians(2)
if self.delta > radians(15):
self.delta = radians(15)
self.deltaDirection = "down"
else:
self.delta -= radians(2)
if self.delta < radians(-15):
self.delta = radians(-15)
self.deltaDirection = "up"
if self.nextMine <= time.time():
self.nextMine = time.time() + 5 * random.random()
center = self.rect.center
d = self.rect.h
v = [center[0] - d * cos(self.angle),
center[1] + d * sin(self.angle)]
Mine(who=self, center=v, groups=self.groups())
self.angle += self.delta
def killed_actor(self, _actor):
pass
def bump(self, damage=0):
self.angle += radians(73.0)
if self.angle >= 4*pi:
self.angle -= 4*pi
self.take_damage(damage)
class Spawner(SpriteActor):
def __init__(self, target_groups=None, *args, **kwargs):
super().__init__(*args, **kwargs)
self.time = 0.0
self.angle = 0
self.velocity = 0
self.hitpoints = 1
self.physical = False
self.robots = []
self.target_groups = target_groups
self.robots = (BasicRobot, MinedropperRobot)
def update(self):
if self.time == 0.0:
self.time = time.time() + 0.5 # wait 1/2 second on start
elif time.time() >= self.time: # every five seconds
self.time = time.time() + 5.0
angle = random.random() * 2 * pi;
velocity = random.random() * BASE_SPEED + 1
newRobot = random.choice(self.robots)
newRobot(groups=self.target_groups, center=self.rect.center, angle=angle, velocity=velocity)