100 lines
2.4 KiB
Python
100 lines
2.4 KiB
Python
from casioplot import *
|
|
|
|
class SpriteIndexed:
|
|
def __init__(self, w: int, h: int, data):
|
|
self.w = w
|
|
self.h = h
|
|
self.data = data
|
|
def _get_data(self, index: int, pallet: dict):
|
|
value = self.data[index]
|
|
return pallet[value]
|
|
def draw(self, x: int, y: int, pallet: dict) -> None:
|
|
i = 0
|
|
for yoff in range(self.h):
|
|
for xoff in range(self.w):
|
|
value = self._get_data(i, pallet)
|
|
if value:
|
|
set_pixel(x + xoff, y + yoff, value)
|
|
i += 1
|
|
def to_raw(self, pallet: dict) -> SpriteRaw:
|
|
raw_data = []
|
|
for i in range(len(self.data)):
|
|
data_cell = self.data[i]
|
|
if data_cell:
|
|
raw_data.append(pallet[data_cell])
|
|
else:
|
|
raw_data.append(None)
|
|
return SpriteRaw(self.w, self.h, raw_data)
|
|
|
|
class SpriteRaw(SpriteIndexed):
|
|
def _get_data(self, index: int, placeholder: None) -> tuple:
|
|
return self.data[index]
|
|
def draw(self, x: int, y: int) -> None:
|
|
SpriteIndexed.draw(self, x, y, None)
|
|
def to_raw(self) -> None:
|
|
return None
|
|
|
|
def store_rect(x: int, y: int, w:int, h: int) -> SpriteRaw:
|
|
result = []
|
|
for i in range(y, y + h):
|
|
for j in range(x, x + w):
|
|
result.append(get_pixel(j, i))
|
|
return SpriteRaw(w, h, result)
|
|
|
|
def rect(x: int, y: int, w: int, h: int, color: tuple) -> None:
|
|
for i in range(x, x + w):
|
|
for j in range(y, y + h):
|
|
set_pixel(i, j, color)
|
|
|
|
def fill_screen(color: tuple) -> None:
|
|
rect(0, 0, 384, 192, color)
|
|
|
|
def invert_pixel(x: int, y: int) -> None:
|
|
ocolor = get_pixel(x, y)
|
|
set_pixel(x, y, (255 - ocolor[0], 255 - ocolor[1], 255 - ocolor[2]))
|
|
|
|
def invert_rect(x: int, y: int, w: int, h: int) -> None:
|
|
for i in range(x, x + w):
|
|
for j in range(y, y + h):
|
|
invert_pixel(i, j)
|
|
|
|
def wait_until_ac() -> int:
|
|
c = 0
|
|
try:
|
|
while 1:
|
|
c += 1
|
|
except:
|
|
return c
|
|
|
|
#color constants
|
|
C_BLACK = (0,0,0)
|
|
C_WHITE = (255,255,255)
|
|
C_RED = (255,0,0)
|
|
C_GREEN = (0,255,0)
|
|
C_BLUE = (0,0,255)
|
|
C_YELLOW = (255,255,0)
|
|
C_PURPLE = (255,0,255)
|
|
C_CYAN = (0,255,255)
|
|
#standard pallets
|
|
PALLET_STR = {
|
|
' ': None,
|
|
'd': C_BLACK,
|
|
'w': C_WHITE,
|
|
'r': C_RED,
|
|
'g': C_GREEN,
|
|
'b': C_BLUE,
|
|
'y': C_YELLOW,
|
|
'p': C_PURPLE,
|
|
'c': C_CYAN
|
|
}
|
|
PALLET_BYTE = {
|
|
0x01: C_BLACK,
|
|
0x02: C_WHITE,
|
|
0x03: C_RED,
|
|
0x04: C_GREEN,
|
|
0x05: C_BLUE,
|
|
0x06: C_YELLOW,
|
|
0x07: C_PURPLE,
|
|
0x08: C_CYAN
|
|
}
|