199 lines
5.2 KiB
Python
199 lines
5.2 KiB
Python
from PIL import Image
|
|
|
|
# Palette de couleurs
|
|
pierre = (0, 0, 0)
|
|
brique = (255, 0, 0)
|
|
empty = (255, 255, 255)
|
|
piece = (203, 255, 0)
|
|
boite_piece = (255, 153, 0)
|
|
boite_champi = (204, 0, 255)
|
|
brique_piece = (101, 127, 0)
|
|
beton = (84, 84, 84)
|
|
tuyau_milieu = (0, 255, 102)
|
|
tuyau_bout = (50, 255, 0)
|
|
drapeau = (0, 255, 216)
|
|
|
|
goomba = (127, 76, 0)
|
|
koopa_vert = (25, 127, 0)
|
|
|
|
def color_compare(color1, color2):
|
|
if color1[0] == color2[0] and color1[1] == color2[1] and color1[2] == color2[2]:
|
|
return 1
|
|
else:
|
|
return 0
|
|
|
|
|
|
|
|
# Load image
|
|
filename = input("File name ?\n> ")
|
|
img = Image.open(filename)
|
|
print("Converting", filename, "to", "\"" + filename + ".c\",", "size =", img.size)
|
|
|
|
code = "// !b Here the generated section begins (see the end at \"!e\")\nw_current_x = " + str(img.size[0]) + ";\nw_current_y = " + str(img.size[1]) + ";\nworld_t lvl[]=\n{"
|
|
|
|
nombre_ennemis=0
|
|
ennemis = "\nennemi_t ennemies0[]={"
|
|
|
|
|
|
# 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 color_compare(pixels[x,y], pierre):
|
|
hexa="0x"
|
|
|
|
left=1
|
|
right=1
|
|
|
|
if x != 0:
|
|
left = (color_compare(pixels[x-1,y], pierre))
|
|
if x != img.size[0]-1:
|
|
right = (color_compare(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 = (color_compare(pixels[x,y-1], pierre))
|
|
if y != img.size[1]-1:
|
|
down = (color_compare(pixels[x,y+1], pierre))
|
|
|
|
sy = int(bool(up))
|
|
hexa += str(sy)
|
|
|
|
code += "{EARTH," + hexa + "}, "
|
|
|
|
elif color_compare(pixels[x,y], piece):
|
|
code += "{COIN,0}, "
|
|
elif color_compare(pixels[x,y], brique):
|
|
code += "{BRICK,0}, "
|
|
elif color_compare(pixels[x,y],brique_piece):
|
|
code += "{BRICK,0x15}, "
|
|
elif color_compare(pixels[x,y],boite_piece):
|
|
code += "{GIFT,0x11}, "
|
|
elif color_compare(pixels[x,y],boite_champi):
|
|
code += "{GIFT,0x21}, "
|
|
elif color_compare(pixels[x,y],empty):
|
|
code += "{0,0}, "
|
|
elif color_compare(pixels[x,y],beton):
|
|
code += "{BLOC,0}, "
|
|
|
|
elif color_compare(pixels[x,y],tuyau_bout):
|
|
left=0
|
|
right=0
|
|
up=0 # vide par defaut
|
|
down=0
|
|
tx=0
|
|
ty=0
|
|
if x:
|
|
left = (color_compare(pixels[x-1,y], tuyau_bout)) or (color_compare(pixels[x-1,y], tuyau_milieu))
|
|
if x != img.size[0]-1:
|
|
right = (color_compare(pixels[x+1,y], tuyau_bout)) or (color_compare(pixels[x+1,y], tuyau_milieu))
|
|
|
|
if y:
|
|
up = (color_compare(pixels[x,y-1], tuyau_bout)) or (color_compare(pixels[x,y-1], tuyau_milieu))
|
|
if y != img.size[1]-1:
|
|
down = (color_compare(pixels[x,y+1], tuyau_bout)) or (color_compare(pixels[x,y+1], tuyau_milieu))
|
|
|
|
if right and down:
|
|
tx=0
|
|
if (color_compare(pixels[x,y+1], tuyau_milieu)):
|
|
ty=2
|
|
elif left and down:
|
|
if (color_compare(pixels[x,y+1], tuyau_milieu)):
|
|
tx=1
|
|
ty=2
|
|
else:
|
|
tx=2
|
|
ty=0
|
|
elif up and right:
|
|
tx=0
|
|
if (color_compare(pixels[x,y-1], tuyau_milieu)):
|
|
ty=4
|
|
else:
|
|
ty=1
|
|
elif up and left:
|
|
if (color_compare(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 color_compare(pixels[x,y], tuyau_milieu):
|
|
left=0
|
|
right=0
|
|
up=0 # vide par defaut
|
|
down=0
|
|
tx=0
|
|
ty=0
|
|
if x:
|
|
left = (color_compare(pixels[x-1,y], tuyau_bout)) or (color_compare(pixels[x-1,y], tuyau_milieu))
|
|
if x != img.size[0]-1:
|
|
right = (color_compare(pixels[x+1,y], tuyau_bout)) or (color_compare(pixels[x+1,y], tuyau_milieu))
|
|
|
|
if y:
|
|
up = (color_compare(pixels[x,y-1], tuyau_bout)) or (color_compare(pixels[x,y-1], tuyau_milieu))
|
|
if y != img.size[1]-1:
|
|
down = (color_compare(pixels[x,y+1], tuyau_bout)) or (color_compare(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+"}, "
|
|
elif color_compare(pixels[x,y], drapeau):
|
|
if not color_compare(pixels[x,y-1], drapeau):
|
|
code += "{END_LEVEL,0x1400}, "
|
|
elif not color_compare(pixels[x,y-2], drapeau):
|
|
code+= "{END_LEVEL,0x1401}, "
|
|
elif not color_compare(pixels[x,y-3], drapeau):
|
|
code+= "{END_LEVEL,0x1302}, "
|
|
elif not color_compare(pixels[x,y-4], drapeau):
|
|
code+= "{END_LEVEL,0x1202}, "
|
|
elif not color_compare(pixels[x,y-5], drapeau):
|
|
code+= "{END_LEVEL,0x1202}, "
|
|
elif not color_compare(pixels[x,y-6], drapeau):
|
|
code+= "{END_LEVEL,0x1202}, "
|
|
else:
|
|
code+= "{END_LEVEL,0x1102},"
|
|
else:
|
|
code += "{0,'?'}, "
|
|
#code += str(pixels[x,y])
|
|
|
|
if color_compare(pixels[x,y], goomba):
|
|
nombre_ennemis += 1
|
|
ennemis += "\n GOOMBA(" + str(8*x) + ", " + str(8*i) + ", -1),"
|
|
elif color_compare(pixels[x,y], koopa_vert):
|
|
ennemis += "\n KOOPA_V(" + str(8*x) + ", " + str(8*i) + ", -1),"
|
|
code += "\n};\ninit_level(lvl);\n"
|
|
ennemis += "\n};\nennemis_global_size=" + str(nombre_ennemis) + ";\ninit_ennemies(ennemies0);\n"
|
|
|
|
f = open(filename+".c", 'w')
|
|
f.write(code + ennemis + "\n// !e End of generated section")
|
|
f.close()
|
|
|
|
print("Converted succesfully !") |