|
| 1 | +from micropython import const |
| 2 | +import board |
| 3 | +import time |
| 4 | +import digitalio |
| 5 | +import _pew |
| 6 | + |
| 7 | + |
| 8 | +_FONT = ( |
| 9 | + b'\xff\xff\xff\xff\xff\xff\xf3\xf3\xf7\xfb\xf3\xff\xcc\xdd\xee\xff\xff' |
| 10 | + b'\xff\xdd\x80\xdd\x80\xdd\xff\xf7\x81\xe4\xc6\xd0\xf7\xcc\xdb\xf3\xf9' |
| 11 | + b'\xcc\xff\xf6\xcdc\xdcf\xff\xf3\xf7\xfe\xff\xff\xff\xf6\xfd\xfc\xfd' |
| 12 | + b'\xf6\xff\xe7\xdf\xcf\xdf\xe7\xff\xff\xd9\xe2\xd9\xff\xff\xff\xf3\xc0' |
| 13 | + b'\xf3\xff\xff\xff\xff\xff\xf3\xf7\xfe\xff\xff\x80\xff\xff\xff\xff\xff' |
| 14 | + b'\xff\xff\xf3\xff\xcf\xdb\xf3\xf9\xfc\xff\xd2\xcd\xc8\xdc\xe1\xff\xf7' |
| 15 | + b'\xf1\xf3\xf3\xe2\xff\xe1\xce\xe3\xfd\xc0\xff\xe1\xce\xe3\xce\xe1\xff' |
| 16 | + b'\xf3\xf9\xdc\xc0\xcf\xff\xc0\xfc\xe4\xcf\xe1\xff\xd2\xfc\xe1\xcc\xe2' |
| 17 | + b'\xff\xc0\xdb\xf3\xf9\xfc\xff\xe2\xcc\xe2\xcc\xe2\xff\xe2\xcc\xd2\xcf' |
| 18 | + b'\xe1\xff\xff\xf3\xff\xf3\xff\xff\xff\xf3\xff\xf3\xf7\xfe\xcf\xf3\xfc' |
| 19 | + b'\xf3\xcf\xff\xff\xc0\xff\xc0\xff\xff\xfc\xf3\xcf\xf3\xfc\xff\xe1\xcf' |
| 20 | + b'\xe3\xfb\xf3\xff\xe2\xcd\xc4\xd4\xbd\xd2\xe2\xdd\xcc\xc4\xcc\xff\xe4' |
| 21 | + b'\xcc\xe4\xcc\xe4\xff\xe2\xcd\xfc\xcd\xe2\xff\xe4\xdc\xcc\xdc\xe4\xff' |
| 22 | + b'\xd0\xfc\xf4\xfc\xd0\xff\xd0\xfc\xfc\xf4\xfc\xff\xd2\xfd\xfc\x8d\xd2' |
| 23 | + b'\xff\xcc\xcc\xc4\xcc\xcc\xff\xd1\xf3\xf3\xf3\xd1\xff\xcb\xcf\xcf\xdc' |
| 24 | + b'\xe2\xff\xdc\xcc\xd8\xf4\xc8\xff\xfc\xfc\xfc\xec\xc0\xff\xdd\xc4\xc0' |
| 25 | + b'\xc8\xcc\xff\xcd\xd4\xd1\xc5\xdc\xff\xe2\xdd\xcc\xdd\xe2\xff\xe4\xcc' |
| 26 | + b'\xcc\xe4\xfc\xff\xe2\xcc\xcc\xc8\xd2\xcf\xe4\xcc\xcc\xe0\xcc\xff\xd2' |
| 27 | + b'\xec\xe2\xce\xe1\xff\xc0\xe2\xf3\xf3\xf3\xff\xcc\xcc\xcc\xdd\xe2\xff' |
| 28 | + b'\xcc\xcc\xdd\xe6\xf3\xff\xcc\xc8\xc4\xc0\xd9\xff\xcc\xd9\xe2\xd9\xcc' |
| 29 | + b'\xff\xcc\xdd\xe6\xf3\xf3\xff\xc0\xde\xf7\xed\xc0\xff\xd0\xfc\xfc\xfc' |
| 30 | + b'\xd0\xff\xfc\xf9\xf3\xdb\xcf\xff\xc1\xcf\xcf\xcf\xc1\xff\xf3\xd9\xee' |
| 31 | + b'\xff\xff\xff\xff\xff\xff\xff\x80\xff\xfc\xf7\xef\xff\xff\xff\xff\xd2' |
| 32 | + b'\xcd\xcc\x86\xff\xfc\xe4\xdc\xcc\xe4\xff\xff\xd2\xfd\xbc\xc6\xff\xcf' |
| 33 | + b'\xc6\xcd\xcc\x86\xff\xff\xd6\xcd\xb1\xd2\xff\xcb\xb7\xc1\xf3\xf3\xf6' |
| 34 | + b'\xff\xe2\xcc\xd2\xdf\xe1\xfc\xe4\xdc\xcc\xcc\xff\xf3\xfb\xf1\xb3\xdb' |
| 35 | + b'\xff\xcf\xef\xc7\xcf\xdd\xe2\xfd\xec\xd8\xf4\xcc\xff\xf6\xf3\xf3\xf3' |
| 36 | + b'\xdb\xff\xff\xd9\xc4\xc8\xcc\xff\xff\xe4\xdd\xcc\xcc\xff\xff\xe2\xcc' |
| 37 | + b'\xcc\xe2\xff\xff\xe4\xdc\xcc\xe4\xfc\xff\xc6\xcd\xcc\xc6\xcf\xff\xc9' |
| 38 | + b'\xf4\xfc\xfc\xff\xff\xd2\xf8\xcb\xe1\xff\xf3\xd1\xf3\xb3\xdb\xff\xff' |
| 39 | + b'\xcc\xcc\xcd\x82\xff\xff\xcc\xdd\xe6\xf3\xff\xff\xcc\xc8\xd1\xd9\xff' |
| 40 | + b'\xff\xcc\xe6\xe6\xcc\xff\xff\xdc\xcd\xd2\xcf\xe1\xff\xc0\xdb\xf9\xc0' |
| 41 | + b'\xff\xd3\xf3\xf9\xf3\xd3\xff\xf3\xf3\xf7\xf3\xf3\xff\xf1\xf3\xdb\xf3' |
| 42 | + b'\xf1\xff\xbfr\x8d\xfe\xff\xfff\x99f\x99f\x99') |
| 43 | + |
| 44 | + |
| 45 | +K_RIGHT = const(0x01) |
| 46 | +K_DOWN = const(0x02) |
| 47 | +K_LEFT = const(0x04) |
| 48 | +K_UP = const(0x08) |
| 49 | +K_O = const(0x40) |
| 50 | +K_X = const(0x80) |
| 51 | + |
| 52 | +_screen = None |
| 53 | + |
| 54 | + |
| 55 | +def brightness(level): |
| 56 | + pass |
| 57 | + |
| 58 | + |
| 59 | +def show(pix): |
| 60 | + _screen.blit(pix) |
| 61 | + |
| 62 | + |
| 63 | +def tick(delay): |
| 64 | + global _tick |
| 65 | + |
| 66 | + _tick += delay |
| 67 | + time.sleep(max(0, _tick - time.monotonic())) |
| 68 | + |
| 69 | + |
| 70 | +class GameOver(Exception): |
| 71 | + pass |
| 72 | + |
| 73 | + |
| 74 | +class Pix: |
| 75 | + def __init__(self, width=8, height=8, buffer=None): |
| 76 | + if buffer is None: |
| 77 | + buffer = bytearray(width * height) |
| 78 | + self.buffer = buffer |
| 79 | + self.width = width |
| 80 | + self.height = height |
| 81 | + |
| 82 | + @classmethod |
| 83 | + def from_text(cls, string, color=None, bgcolor=0, colors=None): |
| 84 | + pix = cls(4 * len(string), 6) |
| 85 | + font = memoryview(_FONT) |
| 86 | + if colors is None: |
| 87 | + if color is None: |
| 88 | + colors = (3, 2, 1, bgcolor) |
| 89 | + else: |
| 90 | + colors = (color, color, bgcolor, bgcolor) |
| 91 | + x = 0 |
| 92 | + for c in string: |
| 93 | + index = ord(c) - 0x20 |
| 94 | + if not 0 <= index <= 95: |
| 95 | + continue |
| 96 | + row = 0 |
| 97 | + for byte in font[index * 6:index * 6 + 6]: |
| 98 | + for col in range(4): |
| 99 | + pix.pixel(x + col, row, colors[byte & 0x03]) |
| 100 | + byte >>= 2 |
| 101 | + row += 1 |
| 102 | + x += 4 |
| 103 | + return pix |
| 104 | + |
| 105 | + @classmethod |
| 106 | + def from_iter(cls, lines): |
| 107 | + pix = cls(len(lines[0]), len(lines)) |
| 108 | + y = 0 |
| 109 | + for line in lines: |
| 110 | + x = 0 |
| 111 | + for pixel in line: |
| 112 | + pix.pixel(x, y, pixel) |
| 113 | + x += 1 |
| 114 | + y += 1 |
| 115 | + return pix |
| 116 | + |
| 117 | + def pixel(self, x, y, color=None): |
| 118 | + if not 0 <= x < self.width or not 0 <= y < self.height: |
| 119 | + return 0 |
| 120 | + if color is None: |
| 121 | + return self.buffer[x + y * self.width] |
| 122 | + self.buffer[x + y * self.width] = color |
| 123 | + |
| 124 | + def box(self, color, x=0, y=0, width=None, height=None): |
| 125 | + x = min(max(x, 0), self.width - 1) |
| 126 | + y = min(max(y, 0), self.height - 1) |
| 127 | + width = max(0, min(width or self.width, self.width - x)) |
| 128 | + height = max(0, min(height or self.height, self.height - y)) |
| 129 | + for y in range(y, y + height): |
| 130 | + xx = y * self.width + x |
| 131 | + for i in range(width): |
| 132 | + self.buffer[xx] = color |
| 133 | + xx += 1 |
| 134 | + |
| 135 | + def blit(self, source, dx=0, dy=0, x=0, y=0, |
| 136 | + width=None, height=None, key=None): |
| 137 | + if dx < 0: |
| 138 | + x -= dx |
| 139 | + dx = 0 |
| 140 | + if x < 0: |
| 141 | + dx -= x |
| 142 | + x = 0 |
| 143 | + if dy < 0: |
| 144 | + y -= dy |
| 145 | + dy = 0 |
| 146 | + if y < 0: |
| 147 | + dy -= y |
| 148 | + y = 0 |
| 149 | + width = min(min(width or source.width, source.width - x), |
| 150 | + self.width - dx) |
| 151 | + height = min(min(height or source.height, source.height - y), |
| 152 | + self.height - dy) |
| 153 | + source_buffer = memoryview(source.buffer) |
| 154 | + self_buffer = self.buffer |
| 155 | + if key is None: |
| 156 | + for row in range(height): |
| 157 | + xx = y * source.width + x |
| 158 | + dxx = dy * self.width + dx |
| 159 | + self_buffer[dxx:dxx + width] = source_buffer[xx:xx + width] |
| 160 | + y += 1 |
| 161 | + dy += 1 |
| 162 | + else: |
| 163 | + for row in range(height): |
| 164 | + xx = y * source.width + x |
| 165 | + dxx = dy * self.width + dx |
| 166 | + for col in range(width): |
| 167 | + color = source_buffer[xx] |
| 168 | + if color != key: |
| 169 | + self_buffer[dxx] = color |
| 170 | + dxx += 1 |
| 171 | + xx += 1 |
| 172 | + y += 1 |
| 173 | + dy += 1 |
| 174 | + |
| 175 | + def __str__(self): |
| 176 | + return "\n".join( |
| 177 | + "".join( |
| 178 | + ('.', '+', '*', '@')[self.pixel(x, y)] |
| 179 | + for x in range(self.width) |
| 180 | + ) |
| 181 | + for y in range(self.height) |
| 182 | + ) |
| 183 | + |
| 184 | + |
| 185 | +def init(): |
| 186 | + global _screen, _tick, keys, _rows, _cols |
| 187 | + |
| 188 | + if _screen is not None: |
| 189 | + return |
| 190 | + |
| 191 | + _screen = Pix(8, 8) |
| 192 | + _tick = time.monotonic() |
| 193 | + |
| 194 | + _rows = ( |
| 195 | + digitalio.DigitalInOut(board.R1), |
| 196 | + digitalio.DigitalInOut(board.R2), |
| 197 | + digitalio.DigitalInOut(board.R3), |
| 198 | + digitalio.DigitalInOut(board.R4), |
| 199 | + digitalio.DigitalInOut(board.R5), |
| 200 | + digitalio.DigitalInOut(board.R6), |
| 201 | + digitalio.DigitalInOut(board.R7), |
| 202 | + digitalio.DigitalInOut(board.R8), |
| 203 | + ) |
| 204 | + |
| 205 | + _cols = ( |
| 206 | + digitalio.DigitalInOut(board.C1), |
| 207 | + digitalio.DigitalInOut(board.C2), |
| 208 | + digitalio.DigitalInOut(board.C3), |
| 209 | + digitalio.DigitalInOut(board.C4), |
| 210 | + digitalio.DigitalInOut(board.C5), |
| 211 | + digitalio.DigitalInOut(board.C6), |
| 212 | + digitalio.DigitalInOut(board.C7), |
| 213 | + digitalio.DigitalInOut(board.C8), |
| 214 | + ) |
| 215 | + _buttons = digitalio.DigitalInOut(board.BUTTONS) |
| 216 | + _pew.PewPew(_screen.buffer, _rows, _cols, _buttons) |
| 217 | + keys = _pew.get_pressed |
0 commit comments