annotate pew.py @ 14:8239a9ade11d draft default tip

Add a license
author Radomir Dopieralski <sheep-devel@sheep.art.pl>
date Fri, 23 Feb 2018 12:07:23 +0100
parents 46a5abc02fd1
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
7
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
1 from micropython import const
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
2 import time
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
3 import framebuf
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
4 import lameboy
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
5
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
6
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
7 _FONT = (
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
8 b'{{{{{{wws{w{HY{{{{YDYDY{sUtGUsH[wyH{uHgHE{ws{{{{vyxyv{g[K[g{{]f]{{{wDw{{'
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
9 b'{{{wy{{{D{{{{{{{w{K_w}x{VHLHe{wuwww{`KfyD{UKgKU{w}XDK{DxTKT{VxUHU{D[wyx{'
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
10 b'UHfHU{UHEKe{{w{w{{{w{wy{KwxwK{{D{D{{xwKwx{eKg{w{VIHyB{fYH@H{dHdHd{FyxyF{'
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
11 b'`XHX`{DxtxD{Dxtxx{FyxIF{HHDHH{wwwww{KKKHU{HXpXH{xxxxD{Y@DLH{IL@LX{fYHYf{'
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
12 b'`HH`x{fYHIF{`HH`H{UxUKU{Dwwww{HHHIR{HHH]w{HHLD@{HYsYH{HYbww{D[wyD{txxxt{'
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
13 b'x}w_K{GKKKG{wLY{{{{{{{{Dxs{{{{{BIIB{x`XX`{{ByyB{KBIIB{{WIpF{OwUwww{`YB[`'
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
14 b'x`XHH{w{vwc{K{OKHUxHpXH{vwws_{{dD@H{{`XHH{{fYYf{{`XX`x{bYIBK{Ipxx{{F}_d{'
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
15 b'wUws_{{HHIV{{HH]s{{HLD@{{HbbH{{HHV[a{D_}D{Cw|wC{wwwwwwpwOwp{WKfxu{@YYY@{'
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
16 )
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
17 _SALT = const(132)
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
18
10
08c0c8e701be Nicer patterns
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 9
diff changeset
19 _BUFFER = memoryview(bytearray(b'\x00\x00\x00\x00\x00\x00'
08c0c8e701be Nicer patterns
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 9
diff changeset
20 b'\x44\x11\x44\x11\x44\x11'
08c0c8e701be Nicer patterns
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 9
diff changeset
21 b'\xee\xbb\xee\xbb\xee\xbb'
08c0c8e701be Nicer patterns
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 9
diff changeset
22 b'\xff\xff\xff\xff\xff\xff'))
7
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
23 _TILES = tuple(framebuf.FrameBuffer(_BUFFER[6 * c:6 * c + 6], 8, 6,
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
24 framebuf.MONO_HLSB) for c in range(4))
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
25
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
26 K_X = lameboy.buttons.A
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
27 K_DOWN = lameboy.buttons.DOWN
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
28 K_LEFT = lameboy.buttons.LEFT
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
29 K_RIGHT = lameboy.buttons.RIGHT
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
30 K_UP = lameboy.buttons.UP
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
31 K_O = lameboy.buttons.B
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
32
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
33 _tick = None
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
34
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
35
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
36 def brightness(level):
13
46a5abc02fd1 Add readme
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 12
diff changeset
37 try:
46a5abc02fd1 Add readme
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 12
diff changeset
38 lameboy.backlight.color(level * 16, level * 16, level * 16)
46a5abc02fd1 Add readme
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 12
diff changeset
39 except OSError:
46a5abc02fd1 Add readme
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 12
diff changeset
40 pass
7
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
41
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
42
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
43 def show(pix):
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
44 for y in range(8):
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
45 for x in range(8):
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
46 color = pix.pixel(7 - x, 7 - y)
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
47 lameboy.display.fb.blit(_TILES[3 - color], 10 + x * 8, y * 6)
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
48 lameboy.display.update()
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
49
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
50
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
51 def keys():
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
52 return lameboy.buttons.get_pressed()
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
53
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
54
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
55 def tick(delay):
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
56 global _tick
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
57
9
e273cabfa0c1 Port to CircuitPython
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 8
diff changeset
58 _tick += delay
e273cabfa0c1 Port to CircuitPython
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 8
diff changeset
59 time.sleep(max(0, _tick - time.monotonic()))
7
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
60
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
61
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
62 class GameOver(Exception):
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
63 pass
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
64
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
65
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
66 class Pix:
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
67 __slots__ = ('buffer', 'width', 'height')
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
68
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
69 def __init__(self, width=8, height=8, buffer=None):
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
70 if buffer is None:
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
71 buffer = bytearray(width * height)
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
72 self.buffer = buffer
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
73 self.width = width
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
74 self.height = height
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
75
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
76 @classmethod
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
77 def from_text(cls, string, color=None, bgcolor=0, colors=None):
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
78 pix = cls(4 * len(string), 6)
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
79 font = memoryview(_FONT)
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
80 if colors is None:
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
81 if color is None:
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
82 colors = (3, 2, bgcolor, bgcolor)
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
83 else:
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
84 colors = (color, color, bgcolor, bgcolor)
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
85 x = 0
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
86 for c in string:
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
87 index = ord(c) - 0x20
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
88 if not 0 <= index <= 95:
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
89 continue
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
90 row = 0
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
91 for byte in font[index * 6:index * 6 + 6]:
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
92 unsalted = byte ^ _SALT
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
93 for col in range(4):
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
94 pix.pixel(x + col, row, colors[unsalted & 0x03])
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
95 unsalted >>= 2
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
96 row += 1
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
97 x += 4
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
98 return pix
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
99
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
100 @classmethod
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
101 def from_iter(cls, lines):
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
102 pix = cls(len(lines[0]), len(lines))
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
103 y = 0
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
104 for line in lines:
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
105 x = 0
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
106 for pixel in line:
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
107 pix.pixel(x, y, pixel)
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
108 x += 1
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
109 y += 1
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
110 return pix
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
111
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
112 def pixel(self, x, y, color=None):
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
113 if not 0 <= x < self.width or not 0 <= y < self.height:
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
114 return 0
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
115 if color is None:
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
116 return self.buffer[x + y * self.width]
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
117 self.buffer[x + y * self.width] = color
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
118
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
119 def box(self, color, x=0, y=0, width=None, height=None):
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
120 x = min(max(x, 0), self.width - 1)
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
121 y = min(max(y, 0), self.height - 1)
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
122 width = max(0, min(width or self.width, self.width - x))
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
123 height = max(0, min(height or self.height, self.height - y))
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
124 for y in range(y, y + height):
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
125 xx = y * self.width + x
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
126 for i in range(width):
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
127 self.buffer[xx] = color
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
128 xx += 1
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
129
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
130 def blit(self, source, dx=0, dy=0, x=0, y=0,
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
131 width=None, height=None, key=None):
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
132 if dx < 0:
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
133 x -= dx
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
134 dx = 0
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
135 if x < 0:
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
136 dx -= x
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
137 x = 0
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
138 if dy < 0:
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
139 y -= dy
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
140 dy = 0
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
141 if y < 0:
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
142 dy -= y
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
143 y = 0
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
144 width = min(min(width or source.width, source.width - x),
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
145 self.width - dx)
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
146 height = min(min(height or source.height, source.height - y),
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
147 self.height - dy)
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
148 source_buffer = memoryview(source.buffer)
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
149 self_buffer = self.buffer
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
150 if key is None:
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
151 for row in range(height):
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
152 xx = y * source.width + x
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
153 dxx = dy * self.width + dx
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
154 self_buffer[dxx:dxx + width] = source_buffer[xx:xx + width]
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
155 y += 1
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
156 dy += 1
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
157 else:
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
158 for row in range(height):
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
159 xx = y * source.width + x
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
160 dxx = dy * self.width + dx
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
161 for col in range(width):
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
162 color = source_buffer[xx]
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
163 if color != key:
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
164 self_buffer[dxx] = color
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
165 dxx += 1
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
166 xx += 1
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
167 y += 1
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
168 dy += 1
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
169
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
170 def __str__(self):
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
171 return "\n".join(
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
172 "".join(
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
173 ('.', '+', '*', '@')[self.pixel(x, y)]
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
174 for x in range(self.width)
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
175 )
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
176 for y in range(self.height)
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
177 )
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
178
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
179
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
180 def init():
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
181 global _tick
aa3586d05684 Add the pew.py library
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
182
9
e273cabfa0c1 Port to CircuitPython
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents: 8
diff changeset
183 _tick = time.monotonic()