-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrenderer.py
More file actions
307 lines (254 loc) · 12.9 KB
/
renderer.py
File metadata and controls
307 lines (254 loc) · 12.9 KB
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
import threading
import time
import json
import pygame
from pygame.locals import *
pygame.display.init()
pygame.font.init()
SIZE = 4
window = pygame.display.set_mode((1024,768),pygame.DOUBLEBUF|pygame.RESIZABLE)
class Renderer(threading.Thread):
def __init__(self, messages_from_engine):
threading.Thread.__init__(self)
self._messages_from_engine = messages_from_engine
self.daemon = True
self._world = None
self._selected = "-1"
self._profile = [0 for x in range(100)]
with open("default.theme", "r") as f:
self._colours = json.loads(f.read())
def run(self):
self._dragging = False
cl = pygame.time.Clock()
sx = 0
sy = 0
WSIZE = 150
overlay = pygame.surface.Surface((WSIZE * SIZE * 4, WSIZE * SIZE * 4), pygame.SRCALPHA)
background = pygame.surface.Surface((WSIZE * SIZE * 4, WSIZE * SIZE * 4), pygame.SRCALPHA)
background.fill((255,255,255,0))
for x in range(WSIZE):
for y in range(WSIZE):
self._draw_hexagon(background, x, y, SIZE, 0,0)
while(True):
time = cl.tick(10)
for event in pygame.event.get():
if event.type == MOUSEBUTTONDOWN:
if event.button == 1:
self._dragging = True
self._mouse = event.pos
elif event.button == 4:
global SIZE
oldsize = SIZE
SIZE += 1
if SIZE > 10:
SIZE = 10
if oldsize != SIZE:
overlay = pygame.surface.Surface((WSIZE * SIZE * 4, WSIZE * SIZE * 4), pygame.SRCALPHA)
background = pygame.surface.Surface((WSIZE * SIZE * 4, WSIZE * SIZE * 4), pygame.SRCALPHA)
background.fill((255,255,255,0))
for x in range(WSIZE):
for y in range(WSIZE):
self._draw_hexagon(background, x, y, SIZE, 0,0)
overlay.fill((255,255,255,0))
for x in range(WSIZE):
for y in range(WSIZE):
if self._world:
if len(self._world[x][y]["markers"]):
self._draw_markers(overlay, self._world[x][y]["markers"], x, y, SIZE, 0,0)
if self._world[x][y]["rock"]:
self._draw_rock(overlay, x, y, SIZE, 0,0)
if self._world[x][y]["foods"]>0:
self._draw_food(overlay, self._world[x][y]["foods"], x, y, SIZE, 0,0)
if self._world[x][y]["ant"]:
self._draw_ant(overlay, self._world[x][y]["ant"], x, y, SIZE, 0,0)
elif event.button == 5:
global SIZE
oldsize = SIZE
SIZE -= 1
if SIZE < 1:
SIZE = 1
if oldsize != SIZE:
overlay = pygame.surface.Surface((WSIZE * SIZE * 4, WSIZE * SIZE * 4), pygame.SRCALPHA)
background = pygame.surface.Surface((WSIZE * SIZE * 4, WSIZE * SIZE * 4), pygame.SRCALPHA)
background.fill((255,255,255,0))
for x in range(WSIZE):
for y in range(WSIZE):
self._draw_hexagon(background, x, y, SIZE, 0,0)
overlay.fill((255,255,255,0))
for x in range(WSIZE):
for y in range(WSIZE):
if self._world:
if len(self._world[x][y]["markers"]):
self._draw_markers(overlay, self._world[x][y]["markers"], x, y, SIZE, 0,0)
if self._world[x][y]["rock"]:
self._draw_rock(overlay, x, y, SIZE, 0,0)
if self._world[x][y]["foods"]>0:
self._draw_food(overlay, self._world[x][y]["foods"], x, y, SIZE, 0,0)
if self._world[x][y]["ant"]:
self._draw_ant(overlay, self._world[x][y]["ant"], x, y, SIZE, 0,0)
elif event.type == MOUSEBUTTONUP:
if event.button == 1:
self._dragging = False
elif event.type == MOUSEMOTION:
if self._dragging:
sx -= self._mouse[0] - event.pos[0]
sy -= self._mouse[1] - event.pos[1]
self._mouse = event.pos
if event.type == VIDEORESIZE:
global window
window = pygame.display.set_mode((1024,768),pygame.DOUBLEBUF|pygame.RESIZABLE)
window.fill(self._colours["background"])
window.blit(overlay, (sx,sy))
window.blit(background, (sx,sy))
if len(self._messages_from_engine) > 0:
message = self._messages_from_engine.pop(-1)
for m in self._messages_from_engine:
if m[0] == "load theme":
try:
with open(m[1], "r") as f:
self._colours = json.loads(f.read())
except Exception as e:
pass
self._messages_from_engine[:] = []
if message[0] == "draw_world":
#print "RENDERINIG"
self._world = message[1]
WSIZE = len(self._world)
overlay.fill((255,255,255,0))
for x in range(WSIZE):
for y in range(WSIZE):
if self._world:
if len(self._world[x][y]["markers"]):
self._draw_markers(overlay, self._world[x][y]["markers"], x, y, SIZE, 0,0)
if self._world[x][y]["rock"]:
self._draw_rock(overlay, x, y, SIZE, 0,0)
if self._world[x][y]["foods"]>0:
self._draw_food(overlay, self._world[x][y]["foods"], x, y, SIZE, 0,0)
if self._world[x][y]["ant"]:
self._draw_ant(overlay, self._world[x][y]["ant"], x, y, SIZE, 0,0)
if message[0] == "select_world":
print message[1], self._selected
self._selected = message[1]
window.fill(self._colours["background"])
window.blit(overlay, (sx,sy))
window.blit(background, (sx,sy))
pygame.display.flip()
def _draw_hexagon(self, window, x, y, size, sx, sy):
if y % 2 == 0:
x*=4
y*=3
else:
x = x*4+2
y*=3
pygame.draw.line(window, self._colours["cell_border"], (x*size+sx,(y+1)*size+sy), ((x+2)*size+sx, y*size+sy))
pygame.draw.line(window, self._colours["cell_border"], ((x+2)*size+sx, y*size+sy), ((x+4)*size+sx, (y+1)*size+sy))
pygame.draw.line(window, self._colours["cell_border"], ((x+4)*size+sx, (y+1)*size+sy), ((x+4)*size+sx, (y+3)*size+sy))
pygame.draw.line(window, self._colours["cell_border"], ((x+4)*size+sx, (y+3)*size+sy), ((x+2)*size+sx, (y+4)*size+sy))
pygame.draw.line(window, self._colours["cell_border"], ((x+2)*size+sx, (y+4)*size+sy), ((x)*size+sx, (y+3)*size+sy))
pygame.draw.line(window, self._colours["cell_border"], ((x)*size+sx, (y+3)*size+sy), (x*size+sx,(y+1)*size+sy))
def _draw_rock(self, window, x, y, size, sx, sy):
if y % 2 == 0:
x*=4
y*=3
else:
x = x*4+2
y*=3
pygame.draw.line(window, self._colours["rocks"], (x*size+sx,(y+1)*size+sy), ((x+4)*size+sx, (y+3)*size+sy))
pygame.draw.line(window, self._colours["rocks"], ((x+2)*size+sx, y*size+sy), ((x+2)*size+sx, (y+4)*size+sy))
pygame.draw.line(window, self._colours["rocks"], ((x+4)*size+sx, (y+1)*size+sy), ((x)*size+sx, (y+3)*size+sy))
def _draw_ant(self, window, ant, x, y, size, sx, sy):
try:
if y % 2 == 0:
x*=4
y*=3
else:
x = x*4+2
y*=3
if ant.color == "red":
color = self._colours["red_ant"]
elif ant.color == "black":
color = self._colours["black_ant"]
ant_surf = pygame.surface.Surface((4*SIZE,4*SIZE), flags=pygame.SRCALPHA)
lines = ((4*size,2*size),
(0*size, 1*size),
(0*size, 3*size))
width = 0 if ant._has_food else 1
pygame.draw.polygon(ant_surf, color, lines, width)
ant_surf = pygame.transform.rotate(ant_surf, -60 * ant._direction)
padding = (ant_surf.get_rect()[2] - (size*4))/2
window.blit(ant_surf, (x*size+sx - padding, y*size + sy - padding))
except Exception as e:
pass
def _draw_markers(self, window, markers, x, y, size, sx, sy):
if y % 2 == 0:
x*=4
y*=3
else:
x = x*4+2
y*=3
color = (0,255,0)
for m in markers:
if m[1] == "red":
color = self._colours["red_marker"]
elif m[1] == "black":
color = self._colours["black_marker"]
if m[0] == 0:
lines = (((x+2)*size+sx,(y+2)*size+sy),
((x+2)*size+sx, y*size+sy),
((x+4)*size+sx, (y+1)*size+sy))
elif m[0] == 1:
lines = (((x+2)*size+sx,(y+2)*size+sy),
((x+4)*size+sx, (y+3)*size+sy),
((x+4)*size+sx, (y+1)*size+sy))
elif m[0] == 2:
lines = (((x+2)*size+sx,(y+2)*size+sy),
((x+4)*size+sx, (y+3)*size+sy),
((x+2)*size+sx, (y+4)*size+sy))
elif m[0] == 3:
lines = (((x+2)*size+sx,(y+2)*size+sy),
((x)*size+sx, (y+3)*size+sy),
((x+2)*size+sx, (y+4)*size+sy))
elif m[0] == 4:
lines = (((x+2)*size+sx,(y+2)*size+sy),
((x)*size+sx, (y+3)*size+sy),
((x)*size+sx, (y+1)*size+sy))
elif m[0] == 5:
lines = (((x+2)*size+sx,(y+2)*size+sy),
((x+2)*size+sx, y*size+sy),
((x)*size+sx, (y+1)*size+sy))
pygame.draw.polygon(window, color, lines, 0)
def _draw_food(self, window, foods, x, y, size, sx, sy):
if y % 2 == 0:
x*=4
y*=3
else:
x = x*4+2
y*=3
color = self._colours["food"]
lines = []
for i in range(foods):
if i == 0:
lines.extend((((x+2)*size+sx,(y+2)*size+sy),
((x+2)*size+sx, y*size+sy),
((x+4)*size+sx, (y+1)*size+sy)))
elif i == 1:
lines.extend((((x+2)*size+sx,(y+2)*size+sy),
((x+4)*size+sx, (y+3)*size+sy),
((x+4)*size+sx, (y+1)*size+sy)))
elif i == 2:
lines.extend((((x+2)*size+sx,(y+2)*size+sy),
((x+4)*size+sx, (y+3)*size+sy),
((x+2)*size+sx, (y+4)*size+sy)))
elif i == 3:
lines.extend((((x+2)*size+sx,(y+2)*size+sy),
((x)*size+sx, (y+3)*size+sy),
((x+2)*size+sx, (y+4)*size+sy)))
elif i == 4:
lines.extend((((x+2)*size+sx,(y+2)*size+sy),
((x)*size+sx, (y+3)*size+sy),
((x)*size+sx, (y+1)*size+sy)))
elif i == 5:
lines.extend((((x+2)*size+sx,(y+2)*size+sy),
((x+2)*size+sx, y*size+sy),
((x)*size+sx, (y+1)*size+sy)))
pygame.draw.polygon(window, color, lines, 0)