sprito/sprito.py

100 lines
2.4 KiB
Python
Raw Permalink Normal View History

2020-04-12 17:46:30 +02:00
from casioplot import *
2020-04-09 18:08:00 +02:00
class SpriteIndexed:
def __init__(self, w: int, h: int, data):
self.w = w
self.h = h
self.data = data
2020-04-10 14:27:22 +02:00
def _get_data(self, index: int, pallet: dict):
value = self.data[index]
return pallet[value]
2020-04-09 18:08:00 +02:00
def draw(self, x: int, y: int, pallet: dict) -> None:
2020-04-10 14:27:22 +02:00
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:
2020-04-09 18:08:00 +02:00
raw_data = []
for i in range(len(self.data)):
data_cell = self.data[i]
if data_cell:
2020-04-09 18:08:00 +02:00
raw_data.append(pallet[data_cell])
else:
raw_data.append(None)
return SpriteRaw(self.w, self.h, raw_data)
class SpriteRaw(SpriteIndexed):
2020-04-10 14:27:22 +02:00
def _get_data(self, index: int, placeholder: None) -> tuple:
return self.data[index]
2020-04-09 18:08:00 +02:00
def draw(self, x: int, y: int) -> None:
2020-04-10 14:27:22 +02:00
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)
2020-04-12 17:46:30 +02:00
2020-04-13 19:11:33 +02:00
def wait_until_ac() -> int:
c = 0
try:
while 1:
c += 1
except:
return c
2020-04-12 17:46:30 +02:00
#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)
2020-04-12 18:23:21 +02:00
#standard pallets
PALLET_STR = {
' ': None,
2020-04-12 17:46:30 +02:00
'd': C_BLACK,
'w': C_WHITE,
'r': C_RED,
'g': C_GREEN,
'b': C_BLUE,
'y': C_YELLOW,
'p': C_PURPLE,
'c': C_CYAN
}
2020-04-12 18:23:21 +02:00
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
}