-
Notifications
You must be signed in to change notification settings - Fork 18
/
pew.py
203 lines (174 loc) · 5.74 KB
/
pew.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
from micropython import const
import board
import busio
import digitalio
import time
import ugame
import stage
import array
_FONT = (
b'{{{{{{wws{w{HY{{{{YDYDY{sUtGUsH[wyH{uHgHE{ws{{{{vyxyv{g[K[g{{]f]{{{wDw{{'
b'{{{wy{{{D{{{{{{{w{K_w}x{VHLHe{wuwww{`KfyD{UKgKU{w}XDK{DxTKT{VxUHU{D[wyx{'
b'UHfHU{UHEKe{{w{w{{{w{wy{KwxwK{{D{D{{xwKwx{eKg{w{VIHyB{fYH@H{dHdHd{FyxyF{'
b'`XHX`{DxtxD{Dxtxx{FyxIF{HHDHH{wwwww{KKKHU{HXpXH{xxxxD{Y@DLH{IL@LX{fYHYf{'
b'`HH`x{fYHIF{`HH`H{UxUKU{Dwwww{HHHIR{HHH]w{HHLD@{HYsYH{HYbww{D[wyD{txxxt{'
b'x}w_K{GKKKG{wLY{{{{{{{{Dxs{{{{{BIIB{x`XX`{{ByyB{KBIIB{{WIpF{OwUwww{`YB[`'
b'x`XHH{w{vwc{K{OKHUxHpXH{vwws_{{dD@H{{`XHH{{fYYf{{`XX`x{bYIBK{Ipxx{{F}_d{'
b'wUws_{{HHIV{{HH]s{{HLD@{{HbbH{{HHV[a{D_}D{Cw|wC{wwwwwwpwOwp{WKfxu{@YYY@{'
)
_SALT = const(132)
_PALETTE = array.array('H', (0x0, 0x4a29, 0x6004, 0xf8, 0xfd, 0xf42, 0x825b,
0xf8, 0xfe, 0x125b, 0xcffb, 0xe0cf, 0xffff,
0x1ff8, 0xdbff, 0xffff))
K_X = ugame.K_X
K_DOWN = ugame.K_DOWN
K_LEFT = ugame.K_LEFT
K_RIGHT = ugame.K_RIGHT
K_UP = ugame.K_UP
K_O = ugame.K_O
_tick = None
_display = None
def brightness(level):
pass
def show(pix):
for y in range(8):
for x in range(8):
_grid.tile(x + 1, y, 1 + (pix.pixel(x, y) & 0x03))
_game.render_block(16, 0, 144, 128)
keys = ugame.buttons.get_pressed
def tick(delay):
global _tick
now = time.monotonic()
_tick += delay
if _tick < now:
_tick = now
else:
time.sleep(_tick - now)
class GameOver(SystemExit):
pass
class Pix:
__slots__ = ('buffer', 'width', 'height')
def __init__(self, width=8, height=8, buffer=None):
if buffer is None:
buffer = bytearray(width * height)
self.buffer = buffer
self.width = width
self.height = height
@classmethod
def from_text(cls, string, color=None, bgcolor=0, colors=None):
pix = cls(4 * len(string), 6)
font = memoryview(_FONT)
if colors is None:
if color is None:
colors = (3, 2, bgcolor, bgcolor)
else:
colors = (color, color, bgcolor, bgcolor)
x = 0
for c in string:
index = ord(c) - 0x20
if not 0 <= index <= 95:
continue
row = 0
for byte in font[index * 6:index * 6 + 6]:
unsalted = byte ^ _SALT
for col in range(4):
pix.pixel(x + col, row, colors[unsalted & 0x03])
unsalted >>= 2
row += 1
x += 4
return pix
@classmethod
def from_iter(cls, lines):
pix = cls(len(lines[0]), len(lines))
y = 0
for line in lines:
x = 0
for pixel in line:
pix.pixel(x, y, pixel)
x += 1
y += 1
return pix
def pixel(self, x, y, color=None):
if not 0 <= x < self.width or not 0 <= y < self.height:
return 0
if color is None:
return self.buffer[x + y * self.width]
self.buffer[x + y * self.width] = color
def box(self, color, x=0, y=0, width=None, height=None):
x = min(max(x, 0), self.width - 1)
y = min(max(y, 0), self.height - 1)
width = max(0, min(width or self.width, self.width - x))
height = max(0, min(height or self.height, self.height - y))
for y in range(y, y + height):
xx = y * self.width + x
for i in range(width):
self.buffer[xx] = color
xx += 1
def blit(self, source, dx=0, dy=0, x=0, y=0,
width=None, height=None, key=None):
if dx < 0:
x -= dx
dx = 0
if x < 0:
dx -= x
x = 0
if dy < 0:
y -= dy
dy = 0
if y < 0:
dy -= y
y = 0
width = min(min(width or source.width, source.width - x),
self.width - dx)
height = min(min(height or source.height, source.height - y),
self.height - dy)
source_buffer = memoryview(source.buffer)
self_buffer = self.buffer
if key is None:
for row in range(height):
xx = y * source.width + x
dxx = dy * self.width + dx
self_buffer[dxx:dxx + width] = source_buffer[xx:xx + width]
y += 1
dy += 1
else:
for row in range(height):
xx = y * source.width + x
dxx = dy * self.width + dx
for col in range(width):
color = source_buffer[xx]
if color != key:
self_buffer[dxx] = color
dxx += 1
xx += 1
y += 1
dy += 1
def __str__(self):
return "\n".join(
"".join(
('.', '+', '*', '@')[self.pixel(x, y)]
for x in range(self.width)
)
for y in range(self.height)
)
def init():
global _tick, _display, _bitmap, _grid, _game
if _tick is not None:
return
_tick = time.monotonic()
_game = stage.Stage(ugame.display, 12)
_bank = bytearray(2048)
for c in range(16):
for y in range(0, 15):
for x in range(0, 7):
_bank[c * 128 + y * 8 + x] = c | c << 4
_bank[c * 128 + y * 8 + 7] = c << 4
_bank[c * 128] = c
_bank[c * 128 + 7] = 0
_bank[c * 128 + 14 * 8] = c
_bank[c * 128 + 14 * 8 + 7] = 0
tiles = stage.Bank(_bank, _PALETTE)
_grid = stage.Grid(tiles, 10, 8)
_grid.move(0, 0)
_game.layers = [_grid]
_game.render_block()