2020-01-12 14:34:13 +01:00
from PIL import Image
# Palette de couleurs
2020-01-28 14:32:39 +01:00
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 )
2020-01-28 21:32:58 +01:00
koopa_rouge = ( 127 , 0 , 0 )
2020-01-28 14:32:39 +01:00
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
2020-01-12 18:04:09 +01:00
2020-01-12 14:34:13 +01:00
# Load image
filename = input ( " File name ? \n > " )
img = Image . open ( filename )
2020-01-13 12:12:46 +01:00
print ( " Converting " , filename , " to " , " \" " + filename + " .c \" , " , " size = " , img . size )
2020-01-12 14:34:13 +01:00
2020-01-30 21:09:18 +01:00
code = " // !b Here the generated section begins (see the end at \" !e \" ) \n map_current->w = " + str ( img . size [ 0 ] ) + " ; \n map_current->h = " + str ( img . size [ 1 ] ) + " ; \n cell_t lvl[]= \n { "
2020-01-13 12:12:46 +01:00
nombre_ennemis = 0
ennemis = " \n ennemi_t ennemies0[]= { "
2020-01-12 14:34:13 +01:00
# 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
2020-01-12 18:04:09 +01:00
2020-01-28 14:32:39 +01:00
if color_compare ( pixels [ x , y ] , pierre ) :
2020-01-12 14:34:13 +01:00
hexa = " 0x "
left = 1
right = 1
if x != 0 :
2020-01-28 14:32:39 +01:00
left = ( color_compare ( pixels [ x - 1 , y ] , pierre ) )
2020-01-12 14:34:13 +01:00
if x != img . size [ 0 ] - 1 :
2020-01-28 14:32:39 +01:00
right = ( color_compare ( pixels [ x + 1 , y ] , pierre ) )
2020-01-12 14:34:13 +01:00
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 :
2020-01-28 14:32:39 +01:00
up = ( color_compare ( pixels [ x , y - 1 ] , pierre ) )
2020-01-12 14:34:13 +01:00
if y != img . size [ 1 ] - 1 :
2020-01-28 14:32:39 +01:00
down = ( color_compare ( pixels [ x , y + 1 ] , pierre ) )
2020-01-12 14:34:13 +01:00
2020-01-12 18:04:09 +01:00
sy = int ( bool ( up ) )
hexa + = str ( sy )
2020-01-12 14:34:13 +01:00
code + = " { EARTH, " + hexa + " }, "
2020-01-12 18:04:09 +01:00
2020-01-28 14:32:39 +01:00
elif color_compare ( pixels [ x , y ] , piece ) :
2020-01-12 18:04:09 +01:00
code + = " { COIN,0}, "
2020-01-28 14:32:39 +01:00
elif color_compare ( pixels [ x , y ] , brique ) :
2020-01-12 18:04:09 +01:00
code + = " { BRICK,0}, "
2020-01-28 14:32:39 +01:00
elif color_compare ( pixels [ x , y ] , brique_piece ) :
2020-01-12 18:04:09 +01:00
code + = " { BRICK,0x15}, "
2020-01-28 14:32:39 +01:00
elif color_compare ( pixels [ x , y ] , boite_piece ) :
2020-01-12 18:04:09 +01:00
code + = " { GIFT,0x11}, "
2020-01-28 14:32:39 +01:00
elif color_compare ( pixels [ x , y ] , boite_champi ) :
2020-01-12 18:04:09 +01:00
code + = " { GIFT,0x21}, "
2020-01-28 14:32:39 +01:00
elif color_compare ( pixels [ x , y ] , empty ) :
2020-01-12 14:34:13 +01:00
code + = " { 0,0}, "
2020-01-28 14:32:39 +01:00
elif color_compare ( pixels [ x , y ] , beton ) :
2020-01-12 18:04:09 +01:00
code + = " { BLOC,0}, "
2020-01-28 14:32:39 +01:00
elif color_compare ( pixels [ x , y ] , tuyau_bout ) :
2020-01-12 18:04:09 +01:00
left = 0
right = 0
up = 0 # vide par defaut
down = 0
tx = 0
ty = 0
if x :
2020-01-28 14:32:39 +01:00
left = ( color_compare ( pixels [ x - 1 , y ] , tuyau_bout ) ) or ( color_compare ( pixels [ x - 1 , y ] , tuyau_milieu ) )
2020-01-12 18:04:09 +01:00
if x != img . size [ 0 ] - 1 :
2020-01-28 14:32:39 +01:00
right = ( color_compare ( pixels [ x + 1 , y ] , tuyau_bout ) ) or ( color_compare ( pixels [ x + 1 , y ] , tuyau_milieu ) )
2020-01-12 18:04:09 +01:00
if y :
2020-01-28 14:32:39 +01:00
up = ( color_compare ( pixels [ x , y - 1 ] , tuyau_bout ) ) or ( color_compare ( pixels [ x , y - 1 ] , tuyau_milieu ) )
2020-01-12 18:04:09 +01:00
if y != img . size [ 1 ] - 1 :
2020-01-28 14:32:39 +01:00
down = ( color_compare ( pixels [ x , y + 1 ] , tuyau_bout ) ) or ( color_compare ( pixels [ x , y + 1 ] , tuyau_milieu ) )
2020-01-12 18:04:09 +01:00
if right and down :
tx = 0
2020-01-28 14:32:39 +01:00
if ( color_compare ( pixels [ x , y + 1 ] , tuyau_milieu ) ) :
2020-01-12 18:04:09 +01:00
ty = 2
elif left and down :
2020-01-28 14:32:39 +01:00
if ( color_compare ( pixels [ x , y + 1 ] , tuyau_milieu ) ) :
2020-01-12 18:04:09 +01:00
tx = 1
ty = 2
else :
tx = 2
ty = 0
elif up and right :
tx = 0
2020-01-28 14:32:39 +01:00
if ( color_compare ( pixels [ x , y - 1 ] , tuyau_milieu ) ) :
2020-01-12 18:04:09 +01:00
ty = 4
else :
ty = 1
elif up and left :
2020-01-28 14:32:39 +01:00
if ( color_compare ( pixels [ x , y - 1 ] , tuyau_milieu ) ) :
2020-01-12 18:04:09 +01:00
tx = 1
ty = 4
else :
tx = 2
ty = 1
hexa = " 0x " + str ( tx ) + str ( ty )
code + = " { TUYAU, " + hexa + " }, "
2020-01-28 14:32:39 +01:00
elif color_compare ( pixels [ x , y ] , tuyau_milieu ) :
2020-01-12 18:04:09 +01:00
left = 0
right = 0
up = 0 # vide par defaut
down = 0
tx = 0
ty = 0
if x :
2020-01-28 14:32:39 +01:00
left = ( color_compare ( pixels [ x - 1 , y ] , tuyau_bout ) ) or ( color_compare ( pixels [ x - 1 , y ] , tuyau_milieu ) )
2020-01-12 18:04:09 +01:00
if x != img . size [ 0 ] - 1 :
2020-01-28 14:32:39 +01:00
right = ( color_compare ( pixels [ x + 1 , y ] , tuyau_bout ) ) or ( color_compare ( pixels [ x + 1 , y ] , tuyau_milieu ) )
2020-01-12 18:04:09 +01:00
if y :
2020-01-28 14:32:39 +01:00
up = ( color_compare ( pixels [ x , y - 1 ] , tuyau_bout ) ) or ( color_compare ( pixels [ x , y - 1 ] , tuyau_milieu ) )
2020-01-12 18:04:09 +01:00
if y != img . size [ 1 ] - 1 :
2020-01-28 14:32:39 +01:00
down = ( color_compare ( pixels [ x , y + 1 ] , tuyau_bout ) ) or ( color_compare ( pixels [ x , y + 1 ] , tuyau_milieu ) )
2020-01-12 18:04:09 +01:00
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-28 14:32:39 +01:00
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}, "
2020-01-12 14:34:13 +01:00
else :
code + = " { 0, ' ? ' }, "
#code += str(pixels[x,y])
2020-01-13 12:12:46 +01:00
2020-01-28 14:32:39 +01:00
if color_compare ( pixels [ x , y ] , goomba ) :
2020-01-13 12:12:46 +01:00
nombre_ennemis + = 1
2020-01-13 20:26:37 +01:00
ennemis + = " \n GOOMBA( " + str ( 8 * x ) + " , " + str ( 8 * i ) + " , -1), "
2020-01-28 14:32:39 +01:00
elif color_compare ( pixels [ x , y ] , koopa_vert ) :
2020-01-13 20:26:37 +01:00
ennemis + = " \n KOOPA_V( " + str ( 8 * x ) + " , " + str ( 8 * i ) + " , -1), "
2020-01-28 21:32:58 +01:00
elif color_compare ( pixels [ x , y ] , koopa_rouge ) :
ennemis + = " \n KOOPA_R( " + str ( 8 * x ) + " , " + str ( 8 * i ) + " , -1), "
2020-01-13 12:12:46 +01:00
code + = " \n }; \n init_level(lvl); \n "
ennemis + = " \n }; \n ennemis_global_size= " + str ( nombre_ennemis ) + " ; \n init_ennemies(ennemies0); \n "
2020-01-12 14:34:13 +01:00
2020-01-12 18:04:09 +01:00
f = open ( filename + " .c " , ' w ' )
2020-01-28 14:32:39 +01:00
f . write ( code + ennemis + " \n // !e End of generated section " )
2020-01-13 12:12:46 +01:00
f . close ( )
print ( " Converted succesfully ! " )