background + map setup with Tiled

This commit is contained in:
Lephenixnoir 2023-10-29 18:09:52 +01:00
commit a48abbc221
Signed by: Lephenixnoir
GPG Key ID: 1BBA026E13FC0495
9 changed files with 208 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/gg_data.py

1
README.md Normal file
View File

@ -0,0 +1 @@
Run `encode_data.py` then send `gg.py` and `gg_data.py` to the calculator.

BIN
bg.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 KiB

BIN
concept.xcf Normal file

Binary file not shown.

116
encode_data.py Executable file
View File

@ -0,0 +1,116 @@
#! /usr/bin/enb python3
from PIL import Image
import xml.etree.ElementTree
def discover_palette(img, px):
palette = []
for y in range(img.height):
for x in range(img.width):
if px[x,y] not in palette:
palette.append(px[x,y])
return sorted(palette)
def encode_rle(img, px, palette):
pairs = []
for y in range(img.height):
x = 0
strip = (None, 0)
while x < img.width:
next_color = palette.index(px[x,y])
if next_color == strip[0] and strip[1] < 64:
strip = (strip[0], strip[1] + 1)
else:
if strip[1] > 0:
pairs.append(strip)
strip = (next_color, 1)
x += 1
if strip[1] > 0:
pairs.append(strip)
return pairs
def unalpha_palette(palette):
newp = []
for i, (r, g, b, a) in enumerate(palette):
assert a == 255 or (a == 0 and i == 0)
if a == 0:
newp.append(None)
else:
newp.append((r, g, b))
return newp
def main():
print("Encoding BG...")
img = Image.open("bg.png")
px = img.load()
palette = discover_palette(img, px)
print("Palette:", palette)
assert len(palette) == 4
bgdata = bytes(64*x + (y-1) for (x, y) in encode_rle(img, px, palette))
print("")
print("Encoding tiles...")
img = Image.open("tileset.png")
px = img.load()
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()
for y in range(16):
for x in range(16):
tiledata += bytes([48 + palette.index(subpx[x,y])])
tiles.append(tiledata)
print("")
print("Encoding maps...")
tree = xml.etree.ElementTree.parse("maps.tmx")
layers = tree.getroot().findall("layer")
encoded_layers = []
for l in layers:
csv = l.find("data").text
csv = [int(x) for x in csv.split(",")]
encoded = []
streak = False
for (index, value) in enumerate(csv):
if value == 0:
streak = False
continue
if streak == False:
encoded.append(-index)
streak = True
encoded.append(value - 1)
print(encoded)
encoded_layers.append(encoded)
with open("gg_data.py", "w") as fp:
fp.write(f"bg = {bgdata}\n")
fp.write("tiles = [\n")
for tiledata in tiles:
fp.write(f" {tiledata},\n")
fp.write("]\n")
fp.write("levels = [\n")
for encoded in encoded_layers:
text = str(encoded).replace(" ", "")
fp.write(f" {text},\n")
fp.write("]\n")
main()

50
gg.py Normal file
View File

@ -0,0 +1,50 @@
from gg_data import *
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)]
def renderBG():
c2 = BG_COLORS[2]
c0 = BG_COLORS[0]
for y in range(0, 53):
for x in range(0, 384):
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
for y in range(162, 192):
for x in range(0, 384):
set_pixel(x, y, c0)
def renderTile(x, y, num):
for dy in range(16):
for dx in range(16):
c = tiles[num][16*dy+dx]-48
if c:
set_pixel(x+dx, y+dy, TILE_COLORS[c])
def renderLevel(num):
renderBG()
for value in levels[num]:
if value < 0:
pos = -value
else:
renderTile(16 * (pos % 24), 16 * (pos // 24), value)
pos += 1
def renderPlayer(x, y):
pass
# data = [ get_pixel(y, x) ... ]
renderLevel(0)
show_screen()

36
maps.tmx Normal file
View File

@ -0,0 +1,36 @@
<?xml version="1.0" encoding="UTF-8"?>
<map version="1.10" tiledversion="1.10.2" orientation="orthogonal" renderorder="right-down" width="24" height="12" tilewidth="16" tileheight="16" infinite="0" nextlayerid="3" nextobjectid="1">
<tileset firstgid="1" source="tileset.tsx"/>
<layer id="1" name="Tile Layer 1" width="24" height="12" visible="0">
<data encoding="csv">
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,2,1,1,1,1,1,2,0,0,0,0,
2,2,1,1,1,1,1,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,5,6,6,6,5,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
2,2,2,2,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,2,2,2,2,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4,3,3,4,3,3,4
</data>
</layer>
<layer id="2" name="Tile Layer 2" width="24" height="12">
<data encoding="csv">
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,2,1,1,1,1,1,1,1,2,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
</data>
</layer>
</map>

BIN
tileset.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

4
tileset.tsx Normal file
View File

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<tileset version="1.10" tiledversion="1.10.2" name="gg" tilewidth="16" tileheight="16" tilecount="6" columns="5">
<image source="tileset.png" width="80" height="16"/>
</tileset>