-
Notifications
You must be signed in to change notification settings - Fork 6
/
stage.py
556 lines (498 loc) · 21.9 KB
/
stage.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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
import utime
import array
import struct
import _stage
FONT = (b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
b'P\x01\xd4\x05\xf5\x17\xed\x1e\xd5\x15\xd0\x01P\x01\x00\x00'
b'P\x01\xd0\x01\xd5\x15\xed\x1e\xf5\x17\xd4\x05P\x01\x00\x00'
b'P\x01\xd0\x05\x95\x17\xfd\x1f\x95\x17\xd0\x05P\x01\x00\x00'
b'P\x01\xd4\x01\xb5\x15\xfd\x1f\xb5\x15\xd4\x01P\x01\x00\x00'
b'T\x05\xf9\x1b\xdd\x1d}\x1f\xd9\x19\xa9\x1aT\x05\x00\x00'
b'T\x05\xf9\x1b]\x1d\xdd\x1dY\x19\xa9\x1aT\x05\x00\x00P\x01\xd0\x01'
b'\xe5\x16\xfd\x1f\xe4\x06t\x07\x14\x05\x00\x00P\x01\xd5\x15'
b']\x1d\x95\x15\xf4\x07\xe4\x06T\x05\x00\x00\x14\x05y\x1b'
b'\xfd\x1f\xf9\x1b\xe4\x06\xd0\x01@\x00\x00\x00P\x01\xf4\x06'
b'\xad\x1b\xed\x1b\xf9\x1a\xa4\x06P\x01\x00\x00@\x00\xd0\x01'
b'\xf4\x06\xfd\x1a\xa4\x06\x90\x01@\x00\x00\x00@\x15\xd0\x1a'
b'\xb4\x1b\xed\x1b\xfd\x06\xad\x01U\x00\x00\x00T\x05\xf5\x17'
b'\xbd\x1a]\x19m\x1bm\x1aU\x15\x00\x00\x00\x15D\x1f\xd9\x1f\xe4\x07'
b'\x94\x01Y\x06\x05\x01\x00\x00T\x05\xbd\x1a\xfd\x1a\xfd\x1a'
b'\xf4\x06\x90\x01@\x00\x00\x00\x15\x15m\x1e\xfd\x1f\xf5\x16'
b'\xb4\x06\xf4\x06T\x05\x00\x00P\x01\x04\x04\x04\x04P\x01'
b'\xf4\x06\xb4\x06P\x01\x00\x00P\x05t\x1b]\x1a\x1d\x15]\x1d\xf4\x07'
b'P\x01\x00\x00T\x00\x10\x01\x10\x05T\x1bm\x1ai\x05\x14\x00\x00\x00'
b'T\x05\xf4\x06\x90\x01\xf4\x06\xb9\x1a\xa9\x1aT\x05\x00\x00'
b'T\x05\xf5\x17\xdd\x1d\xdd\x1d\xf5\x17\xe4\x06T\x05\x00\x00'
b'U\x15\xad\x1e\xfd\x1f\xad\x1e\xd5\x15\xa9\x1aU\x15\x00\x00'
b'P\x01\xe4\x06t\x07\xe4\x06\xd0\x01\xd0\x05\xd0\x06P\x05'
b'P\x05\xd4\x17\xa5\x1d\xf9\x16\xb9\x06\xa5\x05T\x01\x00\x00'
b'U\x15\xfd\x1f\xbd\x1f\xad\x1e\xbd\x1f\xfd\x1fU\x15\x00\x00'
b'U\x15\xf9\x1a\xb4\x06\xf9\x1a\xf9\x1a\xa5\x16T\x05\x00\x00'
b'\x14\x05e\x16y\x1b\xd4\x05y\x1be\x16\x14\x05\x00\x00T\x15\xf5\x1f'
b'\x9d\x19\xf5\x1d\xd4\x1d\xd0\x1dP\x15\x00\x00\x00\x00P\x01'
b'\xe4\x06\xf4\x07\xe4\x06P\x01\x00\x00\x00\x00U\x15\xdd\x1d'
b'\xdd\x1d\x99\x19U\x15\xdd\x1dU\x15\x00\x00\x00\x00\x00\x00'
b'\x00\x00\x00\x00U\x15\xdd\x1dU\x15\x00\x00\x00\x00\x00\x00'
b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00P\x01\xd0\x01'
b'\xd0\x01\x90\x01P\x01\xd0\x01P\x01\x00\x00T\x05t\x07d\x06T\x05'
b'\x00\x00\x00\x00\x00\x00\x00\x00\x14\x05u\x17\xed\x1et\x07'
b'\xed\x1eu\x17\x14\x05\x00\x00T\x15\xf5\x1b\x99\x05\xf5\x17'
b'\x94\x19\xf9\x17U\x05\x00\x00\x15\x14\x1d\x1dU\x07\xd0\x01'
b't\x15\x1d\x1d\x05\x15\x00\x00T\x01\xe4\x05u\x07\xdd\x01'
b']\x17\xe5\x1dT\x14\x00\x00P\x01\xd0\x01\x90\x01P\x01'
b'\x00\x00\x00\x00\x00\x00\x00\x00@\x05P\x06\x90\x01\xd0\x01'
b'\x90\x01P\x06@\x05\x00\x00T\x00d\x01\x90\x01\xd0\x01\x90\x01d\x01'
b'T\x00\x00\x00\x00\x00\x14\x05t\x07\xd0\x01t\x07\x14\x05'
b'\x00\x00\x00\x00P\x01\x90\x01\xd5\x15\xf9\x1b\xd5\x15\x90\x01'
b'P\x01\x00\x00\x00\x00\x00\x00\x00\x00P\x01\xd0\x01\x90\x01'
b'P\x01\x00\x00\x00\x00\x00\x00U\x15\xf9\x1bU\x15\x00\x00'
b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00P\x01\xd0\x01'
b'P\x01\x00\x00\x00\x04\x00\x1d@\x07\xd0\x01t\x00\x1d\x00'
b'\x04\x00\x00\x00T\x05\xe5\x16Y\x1a\xdd\x1di\x19\xe5\x16'
b'T\x05\x00\x00@\x01\xd0\x01\xe4\x01\xd0\x01\xd0\x01\xe4\x06'
b'T\x05\x00\x00T\x05\xf9\x17U\x1d\xf4\x17Y\x05\xfd\x1fU\x15\x00\x00'
b'T\x05\xf5\x17]\x1d\x94\x07]\x1d\xf5\x17T\x05\x00\x00P\x00t\x00'
b']\x05]\x17\xfd\x1fU\x17@\x05\x00\x00U\x15\xfd\x1b]\x05\xfd\x1b'
b'U\x1d\xf9\x1bU\x05\x00\x00T\x15\xf5\x1b]\x05\xfd\x1b]\x1d\xf9\x1b'
b'T\x05\x00\x00U\x15\xfd\x1fU\x19\xd0\x06d\x01t\x00T\x00\x00\x00'
b'T\x05\xf5\x17]\x1d\xf5\x17]\x1d\xf5\x17T\x05\x00\x00T\x05\xf9\x1b'
b']\x1d\xf9\x1fT\x1d\xf9\x17U\x05\x00\x00\x00\x00P\x01\xd0\x01P\x01'
b'\xd0\x01P\x01\x00\x00\x00\x00\x00\x00P\x01\xd0\x01P\x01'
b'\xd0\x01\x90\x01P\x01\x00\x00\x00\x05@\x07\xd0\x01t\x00'
b'\xd0\x01@\x07\x00\x05\x00\x00\x00\x00U\x15\xf9\x1bT\x05'
b'\xf9\x1bU\x15\x00\x00\x00\x00\x14\x00t\x00\xd0\x01@\x07'
b'\xd0\x01t\x00\x14\x00\x00\x00T\x05\xe5\x17]\x1d\xd5\x16'
b'P\x05\xd0\x01P\x01\x00\x00T\x05\xb5\x17\xdd\x1d\x9d\x1b'
b'Y\x15\xf5\x06T\x05\x00\x00P\x00\xe4\x01Y\x07]\x1d\xed\x1e]\x1d'
b'\x15\x15\x00\x00U\x01\xfd\x05]\x07\xed\x16]\x1d\xfd\x17'
b'U\x05\x00\x00T\x05\xf5\x06]\x01\x1d\x14]\x1d\xf5\x17T\x05\x00\x00'
b'U\x01\xbd\x05]\x17\x1d\x1d]\x1d\xfd\x16U\x05\x00\x00U\x05\xfd\x06'
b']\x01\xfd\x01]\x15\xfd\x1bU\x15\x00\x00U\x15\xfd\x1b]\x15]\x00'
b'\xbd\x01]\x01\x15\x00\x00\x00T\x15\xf5\x1b]\x05\xdd\x1f'
b'Y\x1d\xf5\x1bT\x15\x00\x00\x15\x15\x1d\x1d]\x1d\xfd\x1f'
b']\x1d\x1d\x1d\x15\x15\x00\x00T\x05\xe4\x06\xd0\x01\xd0\x01'
b'\xd0\x01\xe4\x06T\x05\x00\x00\x00\x15\x00\x1d\x00\x1d\x05\x1d'
b']\x19\xf5\x17T\x05\x00\x00\x15\x14\x1d\x1d]\x07\xfd\x01'
b']\x07\x1d\x1d\x15\x14\x00\x00\x15\x00\x1d\x00\x1d\x00\x1d\x00'
b']\x15\xfd\x1fU\x15\x00\x00\x05\x14\x1d\x1dm\x1e\xdd\x1d'
b']\x1d\x1d\x1d\x15\x15\x00\x00\x05\x15\x1d\x1dm\x1d\xdd\x1d'
b']\x1e\x1d\x1d\x15\x14\x00\x00T\x01\xb5\x05]\x17\x1d\x1d'
b']\x1d\xe5\x17T\x05\x00\x00U\x05\xfd\x16]\x19]\x1d\xfd\x17]\x05'
b'\x15\x00\x00\x00T\x01\xb5\x05]\x17\x1d\x1d]\x1e\xe5\x07'
b'T\x1d\x00\x15U\x05\xfd\x16]\x19]\x1d\xfd\x07]\x1d\x15\x15\x00\x00'
b'T\x05\xf5\x07]\x01\xe5\x06T\x1d\xf9\x17U\x05\x00\x00U\x15\xf9\x1b'
b'\xd5\x15\xd0\x01\xd0\x01\xd0\x01P\x01\x00\x00\x15\x15\x1d\x1d'
b'\x1d\x1d\x19\x1du\x19\xd4\x17P\x05\x00\x00\x05\x14\x1d\x1d'
b'\x19\x19u\x17d\x06\xd0\x01@\x00\x00\x00\x15\x15\x1d\x1d'
b'\x1d\x1d]\x1d\xd9\x19u\x17\x14\x05\x00\x00\x05\x14\x1d\x1d'
b't\x07\xd0\x01t\x07\x1d\x1d\x05\x14\x00\x00\x15\x15\x1d\x1d'
b'\x19\x19u\x17\x94\x05\xd0\x01P\x01\x00\x00U\x15\xf9\x1b'
b'U\x07\xd0\x01t\x15\xf9\x1bU\x15\x00\x00T\x05\xf4\x06t\x01t\x00'
b't\x01\xf4\x06T\x05\x00\x00\x05\x00\x1d\x00t\x00\xd0\x01'
b'@\x07\x00\x1d\x00\x14\x00\x00T\x05\xe4\x07P\x07@\x07P\x07\xe4\x07'
b'T\x05\x00\x00@\x00\xd0\x01t\x07\x19\x19\x04\x04\x00\x00'
b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00U\x15\xf9\x1b'
b'U\x15\x00\x00P\x00\xb4\x01\xd4\x06P\x07@\x01\x00\x00'
b'\x00\x00\x00\x00\x00\x00T\x15\xe5\x1f]\x1d]\x1d\xf5\x1f'
b'T\x15\x00\x00\x15\x00]\x05\xfd\x16]\x1d]\x1d\xfd\x17U\x05\x00\x00'
b'\x00\x00T\x05\xe5\x07]\x05]\x1d\xf5\x16T\x05\x00\x00\x00\x15T\x1d'
b'\xe5\x1f]\x1d]\x1d\xf5\x1fT\x15\x00\x00\x00\x00T\x05'
b'\xf5\x17\xad\x1e]\x15\xf5\x07T\x05\x00\x00@\x15P\x1e'
b'\xd4\x15\xf4\x07\xd4\x05\xd0\x01\xd0\x01P\x01\x00\x00T\x15'
b'\xe5\x1f]\x1d\xf5\x1fT\x1d\xf9\x16U\x05\x15\x00]\x05\xfd\x16]\x1d'
b'\x1d\x1d\x1d\x1d\x15\x15\x00\x00P\x01\xd0\x01P\x01\xd0\x01'
b'\xd0\x01\xd0\x01P\x01\x00\x00@\x05@\x07@\x05@\x07E\x07]\x07'
b'\xe5\x05T\x01\x15\x00\x1d\x14]\x1d\xfd\x06]\x19\x1d\x1d'
b'\x15\x14\x00\x00T\x00t\x00t\x00t\x00d\x05\xd4\x07P\x05\x00\x00'
b'\x00\x00U\x05\xfd\x17\xdd\x19\xdd\x1d]\x1d\x15\x15\x00\x00'
b'\x00\x00U\x05\xfd\x17]\x19\x1d\x1d\x1d\x1d\x15\x15\x00\x00'
b'\x00\x00T\x05\xe5\x17]\x1d]\x1d\xf5\x17T\x05\x00\x00\x00\x00U\x05'
b'\xfd\x17]\x1d]\x1d\xfd\x17]\x05\x15\x00\x00\x00T\x15\xf5\x1f]\x1d'
b']\x1d\xf5\x1fT\x1d\x00\x15\x00\x00U\x05\xdd\x16}\x1d]\x04\x1d\x00'
b'\x15\x00\x00\x00\x00\x00T\x15\xe5\x1f\xad\x05\x94\x1e\xfd\x16'
b'U\x05\x00\x00T\x00u\x05\xfd\x07t\x01t\x01\xd4\x07P\x05\x00\x00'
b'\x00\x00\x15\x15\x1d\x1d\x1d\x1d]\x1d\xe5\x1fT\x15\x00\x00'
b'\x00\x00\x05\x14\x1d\x1d\x19\x19u\x17\xd4\x05P\x01\x00\x00'
b'\x00\x00\x15\x15]\x1d\xdd\x1d\xd9\x19u\x17T\x05\x00\x00'
b'\x00\x00\x15\x15m\x1e\xd4\x05\xd4\x05m\x1e\x15\x15\x00\x00'
b'\x00\x00\x15\x15\x1d\x1d]\x1d\xe5\x1fT\x1d\xfd\x17U\x05'
b'\x00\x00U\x15\xfd\x1f\xa4\x15\x95\x06\xfd\x1fU\x15\x00\x00'
b'@\x05\x90\x07\xd0\x01t\x01\xd0\x01\x90\x07@\x05\x00\x00'
b'P\x01\x90\x01\xd0\x01\xd0\x01\xd0\x01\x90\x01P\x01\x00\x00'
b'T\x00\xb4\x01\xd0\x01P\x07\xd0\x01\xb4\x01T\x00\x00\x00'
b'\x00\x00T\x00u\x15\xd9\x19U\x17@\x05\x00\x00\x00\x00U\x15\xfd\x1f'
b'\xed\x1e\xbd\x1f\xed\x1e\xfd\x1fU\x15\x00\x00')
PALETTE = (b'\xf8\x1f\x00\x00\xcey\xff\xff\xf8\x1f\x00\x19\xfc\xe0\xfd\xe0'
b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')
def color565(r, g, b):
return (r & 0xf8) << 8 | (g & 0xfc) << 3 | b >> 3
def collide(ax0, ay0, ax1, ay1, bx0, by0, bx1=None, by1=None):
if bx1 is None:
bx1 = bx0
if by1 is None:
by1 = by0
return not (ax1 < bx0 or ay1 < by0 or ax0 > bx1 or ay0 > by1)
class BMP16:
def __init__(self, filename):
self.filename = filename
self.colors = 0
def read_header(self):
if self.colors:
return
with open(self.filename, 'rb') as f:
f.seek(10)
self.data = int.from_bytes(f.read(4), 'little')
f.seek(18)
self.width = int.from_bytes(f.read(4), 'little')
self.height = int.from_bytes(f.read(4), 'little')
f.seek(46)
self.colors = int.from_bytes(f.read(4), 'little')
def read_palette(self):
palette = array.array('H', (0 for i in range(16)))
with open(self.filename, 'rb') as f:
f.seek(self.data - self.colors * 4)
for color in range(self.colors):
buffer = f.read(4)
c = color565(buffer[0], buffer[1], buffer[2])
palette[color] = (c << 8) | (c >> 8)
return palette
def read_data(self, buffer=None):
line_size = self.width >> 1
if buffer is None:
buffer = bytearray(line_size * self.height)
with open(self.filename, 'rb') as f:
f.seek(self.data)
index = (self.height - 1) * line_size
for line in range(self.height):
chunk = f.read(line_size)
buffer[index:index + line_size] = chunk
index -= line_size
return buffer
def read_blockstream(f):
while True:
size = f.read(1)[0]
if size == 0:
break
for i in range(size):
yield f.read(1)[0]
class EndOfData(Exception):
pass
class LZWDict:
def __init__(self, code_size):
self.code_size = code_size
self.clear_code = 1 << code_size
self.end_code = self.clear_code + 1
self.codes = []
self.clear()
def clear(self):
self.last = b''
self.code_len = self.code_size + 1
self.codes[:] = []
def decode(self, code):
if code == self.clear_code:
self.clear()
return b''
elif code == self.end_code:
raise EndOfData()
elif code < self.clear_code:
value = bytes([code])
elif code <= len(self.codes) + self.end_code:
value = self.codes[code - self.end_code - 1]
else:
value = self.last + self.last[0:1]
if self.last:
self.codes.append(self.last + value[0:1])
if (len(self.codes) + self.end_code + 1 >= 1 << self.code_len and
self.code_len < 12):
self.code_len += 1
self.last = value
return value
def lzw_decode(data, code_size):
dictionary = LZWDict(code_size)
bit = 0
try:
byte = next(data)
try:
while True:
code = 0
for i in range(dictionary.code_len):
code |= ((byte >> bit) & 0x01) << i
bit += 1
if bit >= 8:
bit = 0
byte = next(data)
yield dictionary.decode(code)
except EndOfData:
while True:
next(data)
except StopIteration:
return
class GIF16:
def __init__(self, filename):
self.filename = filename
def read_header(self):
with open(self.filename, 'rb') as f:
header = f.read(6)
if header not in {b'GIF87a', b'GIF89a'}:
raise ValueError("Not GIF file")
self.width, self.height, flags, self.background, self.aspect = (
struct.unpack('<HHBBB', f.read(7)))
self.palette_size = 1 << ((flags & 0x07) + 1)
if not flags & 0x80 or self.palette_size > 16:
raise ValueError("16-color GIF expected")
def read_palette(self):
palette = array.array('H', (0 for i in range(16)))
with open(self.filename, 'rb') as f:
f.seek(13)
for color in range(self.palette_size):
buffer = f.read(3)
c = color565(buffer[2], buffer[1], buffer[0])
palette[color] = (c << 8) | (c >> 8)
return palette
def read_data(self, buffer=None):
line_size = self.width >> 1
if buffer is None:
buffer = bytearray(line_size * self.height)
with open(self.filename, 'rb') as f:
f.seek(13 + self.palette_size * 3)
while True: # skip to first frame
block_type = f.read(1)[0]
if block_type == 0x2c:
break
elif block_type == 0x21: # skip extension
extension_type = f.read(1)[0]
while True:
size = f.read(1)[0]
if size == 0:
break
f.seek(1, size)
elif block_type == 0x3b:
raise ValueError("no frames")
x, y, w, h, flags = struct.unpack('<HHHHB', f.read(9))
if flags & 0x80:
raise ValueError("local palette")
if flags & 0x40:
raise ValueError("interlaced")
if w != self.width or h != self.height or x != 0 or y != 0:
raise ValueError("partial frame")
min_code_size = f.read(1)[0]
x = 0
y = 0
for decoded in lzw_decode(read_blockstream(f), min_code_size):
for pixel in decoded:
if x & 0x01:
buffer[(x >> 1) + y * line_size] |= pixel
else:
buffer[(x >> 1) + y * line_size] = pixel << 4
x += 1
if (x >= self.width):
x = 0
y += 1
return buffer
class Bank:
def __init__(self, buffer=None, palette=None):
self.buffer = buffer
self.palette = palette
@classmethod
def from_bmp16(cls, filename):
bmp = BMP16(filename)
bmp.read_header()
if bmp.width != 16 or bmp.height != 256:
raise ValueError("image size not 16x256")
palette = bmp.read_palette()
buffer = bmp.read_data()
return cls(buffer, palette)
@classmethod
def from_gif16(cls, filename):
gif = GIF16(filename)
gif.read_header()
if gif.width != 16 or gif.height != 256:
raise ValueError("image size not 16x256")
palette = gif.read_palette()
buffer = gif.read_data()
return cls(buffer, palette)
class Grid:
def __init__(self, bank, width=8, height=8, palette=None, buffer=None):
self.x = 0
self.y = 0
self.z = 0
self.stride = (width + 1) & 0xfe
self.width = width
self.height = height
self.bank = bank
self.palette = palette or bank.palette
self.buffer = buffer or bytearray(self.stride * height)
self.layer = _stage.Layer(self.stride, self.height, self.bank.buffer,
self.palette, self.buffer)
def tile(self, x, y, tile=None):
if not 0 <= x < self.width or not 0 <= y < self.height:
return 0
index = (y * self.stride + x) >> 1
b = self.buffer[index]
if tile is None:
return b & 0x0f if x & 0x01 else b >> 4
if x & 0x01:
b = b & 0xf0 | tile
else:
b = b & 0x0f | (tile << 4)
self.buffer[index] = b
def move(self, x, y, z=None):
self.x = x
self.y = y
if z is not None:
self.z = z
self.layer.move(int(x), int(y))
class WallGrid(Grid):
def __init__(self, grid, walls, bank, palette=None):
super().__init__(bank, grid.width + 1, grid.height + 1, palette)
self.grid = grid
self.walls = walls
self.update()
self.move(self.x - 8, self.y - 8)
def update(self):
for y in range(self.height):
for x in range(self.width):
t = 0
bit = 1
for dy in (-1, 0):
for dx in (-1, 0):
if self.grid.tile(x + dx, y + dy) in self.walls:
t |= bit
bit <<= 1
self.tile(x, y, t)
class Sprite:
def __init__(self, bank, frame, x, y, z=0, rotation=0, palette=None):
self.bank = bank
self.palette = palette or bank.palette
self.frame = frame
self.rotation = rotation
self.x = x
self.y = y
self.z = z
self.layer = _stage.Layer(1, 1, self.bank.buffer, self.palette)
self.layer.move(x, y)
self.layer.frame(frame, rotation)
self.px = x
self.py = y
def move(self, x, y, z=None):
self.x = x
self.y = y
if z is not None:
self.z = z
self.layer.move(int(x), int(y))
def set_frame(self, frame=None, rotation=None):
if frame is not None:
self.frame = frame
if rotation is not None:
self.rotation = rotation
self.layer.frame(self.frame, self.rotation)
def update(self):
pass
def _updated(self):
self.px = int(self.x)
self.py = int(self.y)
class Text:
def __init__(self, width, height, font=None, palette=None, buffer=None):
self.width = width
self.height = height
self.font = font or FONT
self.palette = palette or PALETTE
self.buffer = buffer or bytearray(width * height)
self.layer = _stage.Text(width, height, self.font,
self.palette, self.buffer)
self.column = 0
self.row = 0
self.x = 0
self.y = 0
self.z = 0
def char(self, x, y, c=None, hightlight=False):
if not 0 <= x < self.width or not 0 <= y < self.height:
return
if c is None:
return chr(self.buffer[y * self.width + x])
c = ord(c)
if hightlight:
c |= 0x80
self.buffer[y * self.width + x] = c
def move(self, x, y, z=None):
self.x = x
self.y = y
if z is not None:
self.z = z
self.layer.move(int(x), int(y))
def cursor(self, x=None, y=None):
if y is not None:
self.row = min(max(0, y), self.width - 1)
if x is not None:
self.column = min(max(0, x), self.height - 1)
def text(self, text, hightlight=False):
for c in text:
if ord(c) >= 32:
self.char(self.column, self.row, c, hightlight)
self.column += 1
if self.column >= self.width or c == '\n':
self.column = 0
self.row += 1
if self.row >= self.height:
self.row = 0
def clear(self):
for i in range(self.width * self.height):
self.buffer[i] = 0
class Stage:
buffer = bytearray(512)
def __init__(self, display, fps=6, scale=None):
if scale is None:
self.scale = max(1, display.width // 128)
else:
self.scale = scale
self.layers = []
self.display = display
self.width = display.width // self.scale
self.height = display.height // self.scale
self.last_tick = utime.ticks_ms()
self.tick_delay = 1000 // fps
def tick(self):
self.last_tick += self.tick_delay
wait = max(0, self.last_tick - utime.ticks_ms())
if wait:
utime.sleep_ms(wait)
else:
self.last_tick = utime.ticks_ms()
def render_block(self, x0=0, y0=0, x1=None, y1=None):
if x1 is None:
x1 = self.width
else:
x1 = min(max(1, x1), self.width)
if y1 is None:
y1 = self.height
else:
y1 = min(max(1, y1), self.height)
x0 = min(max(0, x0), self.width - 1)
y0 = min(max(0, y0), self.height - 1)
if x0 >= x1 or y0 >= y1:
return
layers = [l.layer for l in self.layers]
with self.display as display:
display.block(x0 * self.scale, y0 * self.scale,
x1 * self.scale - 1, y1 * self.scale - 1)
_stage.render(x0, y0, x1, y1, layers, self.buffer,
display.spi, self.scale)
def render_sprites(self, sprites):
layers = [l.layer for l in self.layers]
with self.display as display:
for sprite in sprites:
x0 = max(0, min(self.width - 1,
min(sprite.px, int(sprite.x))))
y0 = max(0, min(self.height - 1,
min(sprite.py, int(sprite.y))))
x1 = max(1, min(self.width,
max(sprite.px, int(sprite.x)) + 16))
y1 = max(1, min(self.height,
max(sprite.py, int(sprite.y)) + 16))
if x0 >= x1 or y0 >= y1:
continue
display.block(x0 * self.scale, y0 * self.scale,
x1 * self.scale - 1, y1 * self.scale - 1)
_stage.render(x0, y0, x1, y1, layers, self.buffer,
display.spi, self.scale)
sprite._updated()