player erasure without goddamn unusable get_pixel

This commit is contained in:
Lephenixnoir 2023-10-29 19:52:41 +01:00
parent a48abbc221
commit 6d38e57d26
Signed by: Lephenixnoir
GPG Key ID: 1BBA026E13FC0495
4 changed files with 83 additions and 26 deletions

Binary file not shown.

View File

@ -12,13 +12,14 @@ def discover_palette(img, px):
return sorted(palette)
def encode_rle(img, px, palette):
pairs = []
lines = []
for y in range(img.height):
x = 0
pairs = []
strip = (None, 0)
while x < img.width:
next_color = palette.index(px[x,y])
if next_color == strip[0] and strip[1] < 64:
if next_color == strip[0] and strip[1] < 32:
strip = (strip[0], strip[1] + 1)
else:
if strip[1] > 0:
@ -27,7 +28,8 @@ def encode_rle(img, px, palette):
x += 1
if strip[1] > 0:
pairs.append(strip)
return pairs
lines.append(pairs)
return lines
def unalpha_palette(palette):
newp = []
@ -49,7 +51,8 @@ def main():
print("Palette:", palette)
assert len(palette) == 4
bgdata = bytes(64*x + (y-1) for (x, y) in encode_rle(img, px, palette))
bgdata = [bytes(64*x + (y-1) for (x, y) in pairs)
for pairs in encode_rle(img, px, palette)]
print("")
print("Encoding tiles...")
@ -59,13 +62,11 @@ def main():
palette = discover_palette(img, px)
print("Palette:", palette)
assert palette[0][3] == 0
print("Encoded palette:", unalpha_palette(palette))
tiles = []
for i in range(img.width // 16):
subimg = img.crop((16*i, 0, 16*i+16, 16))
assert subimg.width == 16 and subimg.height == 16
subpx = subimg.load()
tiledata = bytes()
@ -74,6 +75,27 @@ def main():
tiledata += bytes([48 + palette.index(subpx[x,y])])
tiles.append(tiledata)
print("")
print("Encoding sprites...")
img = Image.open("sprites.png")
px = img.load()
palette = discover_palette(img, px)
print("Palette:", palette)
print("Encoded palette:", unalpha_palette(palette))
sprites = []
for i in range(img.width // 16):
subimg = img.crop((16*i, 0, 16*i+16, img.height))
subpx = subimg.load()
spritedata = bytes()
for y in range(img.height):
for x in range(16):
spritedata += bytes([48 + palette.index(subpx[x,y])])
sprites.append(spritedata)
print("")
print("Encoding maps...")
@ -107,6 +129,11 @@ def main():
fp.write(f" {tiledata},\n")
fp.write("]\n")
fp.write("sprites = [\n")
for spritedata in sprites:
fp.write(f" {spritedata},\n")
fp.write("]\n")
fp.write("levels = [\n")
for encoded in encoded_layers:
text = str(encoded).replace(" ", "")

70
gg.py
View File

@ -3,28 +3,45 @@ from casioplot import *
BG_COLORS = [(79,95,106), (146,163,175), (170,201,223), (255,255,255)]
TILE_COLORS = [None, (63, 131, 150), (79, 95, 106), (90, 108, 130), (104, 85, 100), (144, 199, 240), (145, 135, 143), (146, 163, 175), (197, 205, 216), (233, 223, 170), (236, 236, 238)]
SPRITE_COLORS = [None, (35, 27, 22), (38, 33, 39), (67, 46, 42), (104, 60, 54), (135, 83, 87), (152, 115, 109), (153, 140, 137), (154, 38, 50), (160, 127, 126), (190, 81, 80), (196, 200, 105), (228, 200, 147), (229, 84, 84), (238, 240, 236)]
def renderBG():
def renderBGLine(y, x, xmin, xmax):
for b in bg[y-53]:
c = BG_COLORS[b // 64]
x2 = min(x + (b & 63) + 1, xmax)
if x2 <= xmin:
x = x2
continue
while x < x2:
set_pixel(x, y, c)
x += 1
if x >= xmax:
return
def renderBG(xmin, xmax, y, ymax):
c2 = BG_COLORS[2]
c0 = BG_COLORS[0]
for y in range(0, 53):
for x in range(0, 384):
while y < min(ymax, 53):
for x in range(xmin, xmax):
set_pixel(x, y, c2)
y = 53
x = 0
for b in bg:
c = BG_COLORS[b // 64]
d = (b & 63) + 1
for dx in range(d):
set_pixel(x+dx, y, c)
set_pixel(x+dx+192, y, c)
x += d
if x >= 192:
x = 0
y += 1
if xmax < 192:
while y < min(ymax, 162):
renderBGLine(y, 0, xmin, xmax)
y += 1
for y in range(162, 192):
for x in range(0, 384):
elif xmin >= 192:
while y < min(ymax, 162):
renderBGLine(y, 192, xmin, xmax)
y += 1
else:
while y < min(ymax, 162):
renderBGLine(y, 0, xmin, xmax)
renderBGLine(y, 192, xmin, xmax)
y += 1
while y < ymax:
for x in range(xmin, xmax):
set_pixel(x, y, c0)
y += 1
def renderTile(x, y, num):
for dy in range(16):
@ -34,7 +51,7 @@ def renderTile(x, y, num):
set_pixel(x+dx, y+dy, TILE_COLORS[c])
def renderLevel(num):
renderBG()
renderBG(0, 384, 0, 192)
for value in levels[num]:
if value < 0:
pos = -value
@ -42,9 +59,22 @@ def renderLevel(num):
renderTile(16 * (pos % 24), 16 * (pos // 24), value)
pos += 1
def renderPlayer(x, y):
pass
# data = [ get_pixel(y, x) ... ]
def renderPlayer(x, y, num):
for dy in range(21):
for dx in range(16):
c = sprites[num][16*dy+dx]-48
if c:
set_pixel(x+dx, y+dy, SPRITE_COLORS[c])
def erasePlayer(x, y):
renderBG(x, x+16, y, y+21)
clear_screen()
draw_string(140, 99, "Chargement...", (0,0,0), "small")
show_screen()
renderLevel(0)
show_screen()
for x in range(300):
renderPlayer(x, 80, x % 2)
show_screen()
erasePlayer(x, 80)

BIN
sprites.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB