-
Notifications
You must be signed in to change notification settings - Fork 0
/
render.py
308 lines (260 loc) · 8.82 KB
/
render.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
import cv2
import numpy as np
import pygame
import sync
import time
# global declares
NON_WHITE = (0, 0, 0)
WHITE = (255, 255, 255)
width, height = 1024, 768
def draw_calib_rects(
pygame,
screen,
calib_box_size,
width,
height,
calib_frame_id):
pygame.draw.rect(screen, WHITE,
(0, 0, calib_box_size, calib_box_size))
pygame.draw.rect(screen, WHITE,
(width - calib_box_size, 0,
calib_box_size, calib_box_size))
pygame.draw.rect(screen, WHITE,
(width - calib_box_size, height - calib_box_size,
calib_box_size, calib_box_size))
pygame.draw.rect(screen, WHITE,
(0, height - calib_box_size,
calib_box_size, calib_box_size))
if (1 & calib_frame_id):
pygame.draw.rect(screen, NON_WHITE,
(0, 0, calib_box_size, calib_box_size))
if (2 & calib_frame_id):
pygame.draw.rect(
screen,
NON_WHITE,
(width -
calib_box_size,
0,
calib_box_size,
calib_box_size))
if (4 & calib_frame_id):
pygame.draw.rect(screen, NON_WHITE,
(width - calib_box_size, height - calib_box_size,
calib_box_size, calib_box_size))
if (8 & calib_frame_id):
pygame.draw.rect(
screen,
NON_WHITE,
(0,
height -
calib_box_size,
calib_box_size,
calib_box_size))
class Monster:
def __init__(self, path, size, pos=None, direction=None):
self.path = path
self.size = size
self.pos = pos
self.direction = direction
self.n_frames = 8
self.frame_id = 0
self.frames = []
self.active = False
self.__init_frames()
def __init_frames(self):
# load all into memory
for i in range(self.n_frames):
img = pygame.image.load(
"{0}/frame-{1}.png".format(self.path, str(i + 1)))
img = pygame.transform.scale(img, self.size)
self.frames.append(img)
def __update_frame_id(self):
self.frame_id = (self.frame_id + 1) % self.n_frames
def update(self, update_f):
if not self.active:
return
self.direction, self.pos = update_f(
self.size, self.pos, self.direction)
def draw(self, screen):
if not self.active:
return
screen.blit(self.frames[self.frame_id], self.pos)
self.__update_frame_id()
def activate(self, pos, direction):
self.pos = pos
self.direction = direction
self.active = True
self.frame_id = 0
def kill(self):
self.active = False
def is_hit(self, pt):
if 0 <= pt[0] - self.pos[0] <= self.size[0] and \
0 <= pt[1] - self.pos[1] <= self.size[1]:
return True
return False
class Air:
def __init__(self, path, size):
self.path = path
self.size = size
self.pos = (0, 0)
self.n_frames = 6
self.frame_id = 0
self.frames = []
self.active = False
self.__init_frames()
def __init_frames(self):
# load all into memory
for i in range(self.n_frames):
img = pygame.image.load(
"{0}/frame-{1}.png".format(self.path, str(i + 1)))
img = pygame.transform.scale(img, self.size)
self.frames.append(img)
def __update_frame_id(self):
self.frame_id = self.frame_id + 1
# kill after one animation
if self.frame_id == self.n_frames:
self.active = False
def update(self):
return
def draw(self, screen):
if not self.active:
return
screen.blit(self.frames[self.frame_id], self.pos)
self.__update_frame_id()
def activate(self, pos):
self.pos = pos
self.frame_id = 0
self.active = True
def monster_random_motion(size, pos, direction):
# determine a random motion
speed = 8
dx = speed * np.cos(direction)
dy = speed * np.sin(direction)
updated = tuple(map(lambda x, y: x + y, pos, (dx, dy)))
b1 = size[0]
b3 = size[1]
b2 = width - size[0]
b4 = height - size[1]
if b1 < pos[0] < b2 and b3 < pos[1] < b4:
updated_direction = direction + (2 * np.random.random() - 1)
while updated_direction >= 2 * np.pi:
updated_direction = updated_direction - 2 * np.pi
else:
updated_direction = direction
# do nothing if trying to move out
if (pos[0] < b2 and updated[0] > b2) or \
(pos[0] > b1 and updated[0] < b1) or \
(pos[1] > b3 and updated[1] < b3) or \
(pos[1] < b4 and updated[1] > b4):
updated = pos
return updated_direction, updated
def pick_inactive(pool):
for el in pool:
if not el.active:
return el
return None
def render(queues={}):
msg_capture = queues['msg_render_capture']
rendered_images = queues['rendered_imgs']
locque = queues['msg_location_render']
pygame.init()
np.random.seed(int(time.time()))
screen = pygame.display.set_mode((width, height), pygame.FULLSCREEN)
background = pygame.image.load("res/images/bg.png")
background = pygame.transform.scale(background, (width, height))
running = True
screen.fill((0, 0, 0))
pygame.display.flip()
calib_frame_id = 0
calib_frame_N = 16
def advance_frame_id(x): return (x + 1) % calib_frame_N
calib_box_size = 20
n_monsters = 4
monster_size = (100, 100)
# declare monsters
bats = [Monster("res/images/08_bat_monster", monster_size)
for i in range(2)]
pumpkins = [
Monster(
"res/images/07_flying_pumpkin",
monster_size) for i in range(2)]
all_monsters = bats + pumpkins
spawn_locations = [((-100, 100), 0.35 * np.pi),
((300, height + 100), -0.35 * np.pi),
((width - 300, -100), 0.8 * np.pi),
((width + 100, height - 200), 1.3 * np.pi)]
# declare air explosion
airs = [Air("res/images/air_explode", monster_size)
for i in range(n_monsters)]
# calibration
draw_calib_rects(
pygame,
screen,
calib_box_size,
width,
height,
calib_frame_id)
pygame.display.flip()
msg_capture.put('anchor_start')
pygame.time.delay(100)
sync.wait_on(msg_capture, 'anchor_done')
while running:
screen.fill(0)
screen.blit(background, (0, 0))
draw_calib_rects(
pygame,
screen,
calib_box_size,
width,
height,
calib_frame_id)
# spawn monsters
dead_monster = pick_inactive(all_monsters)
if dead_monster:
spawn_location = spawn_locations[int(np.random.random() * 4)]
dead_monster.activate(spawn_location[0], spawn_location[1])
for monster in all_monsters:
monster.update(monster_random_motion)
monster.draw(screen)
for air in airs:
air.update()
air.draw(screen)
if not locque.empty():
loc = locque.get()
print("[render] ", loc)
pygame.draw.circle(screen, (255, 0, 0), loc, 20)
for monster in all_monsters[::-1]:
if monster.is_hit(loc):
monster.kill()
pick_inactive(airs).activate(monster.pos)
break
pygame.display.flip()
with rendered_images[calib_frame_id].get_lock():
rendered_img = np.frombuffer(
rendered_images[calib_frame_id].get_obj())
renarray = pygame.surfarray.array3d(screen)
print(renarray.shape)
renarray = np.swapaxes(renarray, 0, 1)
renarray = cv2.cvtColor(renarray, cv2.COLOR_BGR2RGB)
rendered_img[:] = renarray.flatten()
# post render procedures
calib_frame_id = advance_frame_id(calib_frame_id)
pygame.time.delay(100)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit(0)
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
pygame.quit()
exit(0)
elif event.type == pygame.MOUSEBUTTONDOWN:
pt = pygame.mouse.get_pos()
# evaluate this
for monster in all_monsters[::-1]:
if monster.is_hit(pt):
monster.kill()
pick_inactive(airs).activate(monster.pos)
break
if __name__ == '__main__':
render()