Skip to content

Commit a7b3295

Browse files
committed
Clean up the library for CP 4.0
1 parent 347b020 commit a7b3295

File tree

3 files changed

+32
-30
lines changed

3 files changed

+32
-30
lines changed

stage.py

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import time
22
import array
33
import _stage
4-
import struct
54

65

76
FONT = (b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
@@ -131,7 +130,7 @@
131130

132131
def color565(r, g, b):
133132
"""Convert 24-bit RGB color to 16-bit."""
134-
return (b & 0xf8) << 8 | (g & 0xfc) << 3 | r >> 3
133+
return (r & 0xf8) << 8 | (g & 0xfc) << 3 | b >> 3
135134

136135

137136
def collide(ax0, ay0, ax1, ay1, bx0, by0, bx1=None, by1=None):
@@ -399,11 +398,11 @@ class Stage:
399398
"""
400399
buffer = bytearray(512)
401400

402-
def __init__(self, display_bus, fps=6):
401+
def __init__(self, display, fps=6):
403402
self.layers = []
404-
self.display_bus = display_bus
405-
self.width = 128
406-
self.height = 128
403+
self.display = display
404+
self.width = display.width
405+
self.height = display.height
407406
self.last_tick = time.monotonic()
408407
self.tick_delay = 1 / fps
409408

@@ -416,26 +415,15 @@ def tick(self):
416415
else:
417416
self.last_tick = time.monotonic()
418417

419-
def _block(self, x0, y0, x1, y1):
420-
x0 += 3
421-
x1 += 3
422-
y0 += 2
423-
y1 += 2
424-
xpos = struct.pack('>HH', x0, x1)
425-
self.display_bus.send(0x2a, xpos)
426-
ypos = struct.pack('>HH', y0, y1)
427-
self.display_bus.send(0x2b, ypos)
428-
self.display_bus.send(0x2c, b'')
429-
430418
def render_block(self, x0=0, y0=0, x1=None, y1=None):
431419
"""Update a rectangle of the screen."""
432420
if x1 is None:
433421
x1 = self.width
434422
if y1 is None:
435423
y1 = self.height
436424
layers = [l.layer for l in self.layers]
437-
self._block(x0, y0, x1 - 1, y1 - 1)
438-
_stage.render(x0, y0, x1, y1, layers, self.buffer, self.display_bus)
425+
self.display.block(x0, y0, x1 - 1, y1 - 1)
426+
_stage.render(x0, y0, x1, y1, layers, self.buffer, self.display.bus)
439427

440428
def render_sprites(self, sprites):
441429
"""Update the spots taken by all the sprites in the list."""
@@ -447,7 +435,7 @@ def render_sprites(self, sprites):
447435
y1 = max(1, min(self.height, max(sprite.py, int(sprite.y)) + 16))
448436
if x0 == x1 or y0 == y1:
449437
continue
450-
self._block(x0, y0, x1 - 1, y1 - 1)
438+
self.display.block(x0, y0, x1 - 1, y1 - 1)
451439
_stage.render(x0, y0, x1, y1, layers, self.buffer,
452-
self.display_bus)
440+
self.display.bus)
453441
sprite._updated()

ugame10/stage.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../stage.py

ugame.py renamed to ugame10/ugame.py

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,10 @@
88
import busio
99
import audioio
1010
import analogio
11-
11+
import struct
1212
import gamepad
1313

1414

15-
try:
16-
import displayio
17-
HAS_DISPLAYIO = True
18-
except ImportErrror:
19-
HAS_DISPLAYIO = False
20-
21-
2215
K_X = 0x01
2316
K_DOWN = 0x02
2417
K_LEFT = 0x04
@@ -47,7 +40,27 @@ def mute(self, value=True):
4740
self.mute_pin.value = not value
4841

4942

50-
display_bus = board.DISPLAY.bus
43+
class Display:
44+
buffer = bytearray(4)
45+
46+
def __init__(self):
47+
self.bus = board.DISPLAY.bus
48+
self.width = board.DISPLAY.width
49+
self.height = board.DISPLAY.height
50+
51+
def block(self, x0, y0, x1, y1):
52+
x0 += 3
53+
x1 += 3
54+
y0 += 2
55+
y1 += 2
56+
struct.pack_into('>HH', self.buffer, 0, x0, x1)
57+
self.bus.send(0x2a, self.buffer)
58+
struct.pack_into('>HH', self.buffer, 0, y0, y1)
59+
self.bus.send(0x2b, self.buffer)
60+
self.bus.send(0x2c, b'')
61+
62+
63+
display = Display()
5164
buttons = gamepad.GamePad(
5265
digitalio.DigitalInOut(board.X),
5366
digitalio.DigitalInOut(board.DOWN),

0 commit comments

Comments
 (0)