supercasiobros/levelconverter/levelconv.py

163 lines
3.4 KiB
Python
Raw Normal View History

2020-01-12 14:34:13 +01:00
from PIL import Image
# Palette de couleurs
pierre = (0, 0, 0, 255)
brique = (255, 0, 0, 255)
empty = (255, 255, 255, 255)
piece = (203, 255, 0, 255)
boite_piece = (255, 153, 0, 255)
boite_champi = (204, 0, 255, 255)
brique_piece = (101, 127, 0, 255)
beton = (84, 84, 84, 255)
tuyau_milieu = (0, 255, 102, 255)
tuyau_bout = (50, 255, 0, 255)
2020-01-12 14:34:13 +01:00
# Load image
filename = input("File name ?\n> ")
img = Image.open(filename)
print("Loaded", filename, "(size =", img.size, ")")
code = "w_current_x = " + str(img.size[0]) + ";\nw_current_y = " + str(img.size[1]) + ";\nworld_t lvl[]=\n{"
largeur=img.size[0]
hauteur=img.size[1]
# Balayage des pixels : passe pour dessiner les murs
pixels = img.load()
for x in range(0,img.size[0]):
code += "\n "
for i in range(0, img.size[1]):
y=img.size[1]-i-1
if pixels[x,y] == pierre:
2020-01-12 14:34:13 +01:00
hexa="0x"
left=1
right=1
if x != 0:
left = (pixels[x-1,y] == pierre)
if x != img.size[0]-1:
right = (pixels[x+1,y] == pierre)
sx=0
if left and right:
sx=1
elif left and right==0:
sx=2
elif left==0 and right:
sx=0
hexa+= str(sx)
up=0 # vide par defaut
down=1
if y != 0:
up = (pixels[x,y-1] == pierre)
if y != img.size[1]-1:
down = (pixels[x,y+1] == pierre)
sy = int(bool(up))
hexa += str(sy)
2020-01-12 14:34:13 +01:00
code += "{EARTH," + hexa + "}, "
elif pixels[x,y]==piece:
code += "{COIN,0}, "
elif pixels[x,y] == brique:
code += "{BRICK,0}, "
elif pixels[x,y]==brique_piece:
code += "{BRICK,0x15}, "
elif pixels[x,y]==boite_piece:
code += "{GIFT,0x11}, "
elif pixels[x,y]==boite_champi:
code += "{GIFT,0x21}, "
2020-01-12 14:34:13 +01:00
elif pixels[x,y]==empty:
code += "{0,0}, "
elif pixels[x,y]==beton:
code += "{BLOC,0}, "
elif pixels[x,y]==tuyau_bout:
left=0
right=0
up=0 # vide par defaut
down=0
tx=0
ty=0
if x:
left = (pixels[x-1,y] == tuyau_bout) or (pixels[x-1,y] == tuyau_milieu)
if x != img.size[0]-1:
right = (pixels[x+1,y] == tuyau_bout) or (pixels[x+1,y] == tuyau_milieu)
if y:
up = (pixels[x,y-1] == tuyau_bout) or (pixels[x,y-1] == tuyau_milieu)
if y != img.size[1]-1:
down = (pixels[x,y+1] == tuyau_bout) or (pixels[x,y+1] == tuyau_milieu)
if right and down:
tx=0
if (pixels[x,y+1]==tuyau_milieu):
ty=2
elif left and down:
if (pixels[x,y+1]==tuyau_milieu):
tx=1
ty=2
else:
tx=2
ty=0
elif up and right:
tx=0
if (pixels[x,y-1]==tuyau_milieu):
ty=4
else:
ty=1
elif up and left:
if (pixels[x,y-1]==tuyau_milieu):
tx=1
ty=4
else:
tx=2
ty=1
hexa="0x"+str(tx)+str(ty)
code += "{TUYAU,"+hexa+"}, "
elif pixels[x,y]==tuyau_milieu:
left=0
right=0
up=0 # vide par defaut
down=0
tx=0
ty=0
if x:
left = (pixels[x-1,y] == tuyau_bout) or (pixels[x-1,y] == tuyau_milieu)
if x != img.size[0]-1:
right = (pixels[x+1,y] == tuyau_bout) or (pixels[x+1,y] == tuyau_milieu)
if y:
up = (pixels[x,y-1] == tuyau_bout) or (pixels[x,y-1] == tuyau_milieu)
if y != img.size[1]-1:
down = (pixels[x,y+1] == tuyau_bout) or (pixels[x,y+1] == tuyau_milieu)
if right and left==0:
tx=0
ty=3
elif left and right==0:
tx=1
ty=3
elif up==0 and down:
tx=1
ty=0
elif up and down==0:
tx=1
ty=1
hexa="0x"+str(tx)+str(ty)
code += "{TUYAU,"+hexa+"}, "
2020-01-12 14:34:13 +01:00
else:
code += "{0,'?'}, "
#code += str(pixels[x,y])
code += "\n};"
2020-01-12 14:34:13 +01:00
f = open(filename+".c", 'w')
f.write(code)
f.close()