]> git.ayabusa.dev Git - a-la-carte.git/commitdiff
finished converter and implemented sprite rendering
authorayabusa <lebgpub@gmail.com>
Sun, 15 Dec 2024 07:07:10 +0000 (08:07 +0100)
committerayabusa <lebgpub@gmail.com>
Sun, 15 Dec 2024 07:07:10 +0000 (08:07 +0100)
src/game.py
src/sprites/_converter.py
src/sprites/sprites_converted.txt [new file with mode: 0644]

index 59bcc73aeb292fe76343c2dbb9ff228155a8bdac..220ab181fabc230e7859beab72cd8225b9c6359a 100644 (file)
@@ -2,7 +2,28 @@ from kandinsky import *
 
 print("Game started")
 colors = [color(i) for i in [(26,28,44),(93,39,93),(177,62,83),(239,125,87),(255,205,117),(167,240,112),(56,183,100),(37,113,121),(41,54,111),(59,93,201),(65,166,246),(115,239,247),(244,244,244),(148,176,194),(86,108,134),(51,60,87)]]
-sprites = {}
+sprites = {'assiette': (0, 2, 20, 17,
+              'ggggggg000000gggggggggggg00cccccc00gggggggg00cccccccccc00ggggg0cccccccccccccc0ggg0cccccddddddccccc0gg0ccccdccccccdcccc0g0ccccdccccccccdcccc00cccdccccccccccdccd00cccdcccccccccccccd00dccdcccccccccccccd00dcccdccccccccccccd0g0dcccccddcccccccd0gg0dccccccccccccccd0ggg0ddccccccccccdd0ggggg00ddccccccdd00gggggggg00dddddd00gggggggggggg000000ggggggg'),
+ 'oignon': (3, 3, 16, 13,
+            'ggggggggggg00ggggggg00000g0650ggggg021122016650ggg0111c111210660g0211c111210g0500211cc11111200500211c11211110g0g012c112111110ggg012c111112120gggg01121112110gggg01221112110gggggg010122110gggggggg0g00000ggggggg'),
+ 'oignon_coupe': (3, 5, 13, 14,
+                  'gggggg00000ggggg000ccccc0ggg0cccc000c0gg0c00c100g0c00c0g0c1cc00c00c00g0c11cc1001cc00ccc110gg011cc1000c0ggg0011c0g0c0ggggg00c00g0c0ggggg01cc00c0gggggg011cc10ggggggg00110gggggggggg00gg'),
+ 'planche': (0, 5, 20, 14,
+             'ggggg00000000000000ggggg0333333333333330g00g0333003333333330033003320c0333323330033333330cd022233330032333330ddc023333300332233330ddd033333002200333330ddd033330g00g03333330ddf03330gggg022222220fdf0220ggggg00000000ffff00gggggggggggggg00fdf0gggggggggggggggg0ff0ggggggggggggggggg00gg'),
+ 'player_down': (3, 0, 15, 20,
+                 'gg00000000000ggg0ccc0ccc0ccc0g0ccc0ccccc0ccc00ccc0ccccc0ccc00ccc0ccccc0ccc0g0ccc0ccc0ccc0ggg000000cc000gggg0ccccccccc0gggg00000000000ggggg04c848c40gggggg044444440ggggggg0000000ggggggg0cccdccc0ggggg0cccccdccc0gggg0c0cfcdc0c0gggg0c0cccdc0c0gggg000cfcdc000gggggg0cccdc0gggggggg0000000ggggggggg00g00ggggg'),
+ 'player_left': (3, 0, 15, 20,
+                 'gg00000000000ggg0ccc0ccc0ccc0g0ccc0ccccc0ccc00ccc0ccccc0ccc00ccc0ccccc0ccc0g0ccc0ccc0ccc0ggg000000cc000gggg0ccccccccc0gggg00000000000ggggg08c48c440gggggg044444440ggggggg0000000ggggggg0ccdcccc0ggggg0ccccdcccc0gggg0c0cfcd0cc0gggg0c0cccdc0c0gggg000cfcdc000gggggg0cccdc0gggggggg0000000ggggggg000gg000gggg'),
+ 'player_right': (2, 0, 15, 20,
+                  'gg00000000000ggg0ccc0ccc0ccc0g0ccc0ccccc0ccc00ccc0ccccc0ccc00ccc0ccccc0ccc0g0ccc0ccc0ccc0ggg000cc000000gggg0ccccccccc0gggg00000000000ggggg044c84c80gggggg044444440ggggggg0000000ggggggg0ccccdcc0ggggg0ccccdcccc0gggg0cc0dcfc0c0gggg0c0cdccc0c0gggg000cdcfc000gggggg0cdccc0gggggggg0000000gggggggg000gg000ggg'),
+ 'player_up': (3, 0, 15, 20,
+               'gg00000000000ggg0ccc0ccc0ccc0g0ccc0ccccc0ccc00ccc0ccccc0ccc00ccc0ccccc0ccc0g0ccc0ccc0ccc0ggg000000cc000gggg0ccccccccc0gggg00000000000ggggg044444440gggggg044444440ggggggg0000000ggggggg0ccccccc0ggggg0ccccccccc0gggg0c0ccccc0c0gggg0c0ccccc0c0gggg000ccccc000gggggg0ccccc0gggggggg0000000ggggggggg00g00ggggg'),
+ 'poele': (0, 0, 19, 19,
+           'gggggg00000000gggggggg000eeeeeeee000gggg0eeeffffffffeee0gg0eeffeeeeeeeeffee0g0efeeeeeeeeeeeefe0g0efeeeeeeeeeeeefe0g0eeeeeeeeeeeeeeee0g00eeeeeeeeeeeeee00g0b0000eeeeee0000f0g000fff000000ffff00g00fffffffffff000f0g0ff0000000000baf0g0ff0fabfffbafffba0g0f0ffbff2f3b3ffff0gg0fff0ffffffff0fff0g0fff0000000000fff0g0fff0gggggggg0fff0g0ff0gggggggggg0ff0gg00gggggggggggg00g'),
+ 'salade': (1, 4, 18, 13,
+            'gggggggggggg0gggggggggg0000g0070gggggggg07777077570ggggg00755567567570ggg077567777777570gg07657765655767770g077776556556755770000076556556767070ggg075556555670g00ggg075556555670gggggg07665655670gggggggg077777770gggggggggg0000000gggggg'),
+ 'salade_cuite': (2, 7, 15, 9,
+                  'gggg0000000ggggg0055765567000g077755555556770g0655655555560g075566566565550g5666656655760g050705576750700g0g0g070606g0gggggggg0g0g0gggg')}
 
 class Ingredient:
     def __init__(self, i_nom):
@@ -14,6 +35,20 @@ class Plat:
         self.p_type = p_type
         self.p_ingredients = p_ingredients
 
+def draw_sprite(sprite_name: str, x: int, y: int, multiplier=1)->None:
+    if sprite_name not in sprites: raise ValueError('Sprite name not in sprites')
+    offx, offy, w, h, data = sprites[sprite_name]
+    curx, cury = x+offx, y+offy
+    for i in range(h):
+        for k in range(w):
+            px = int(data[w*i+k], 18)
+            if px != 16 and px != 17:
+                fill_rect(curx*multiplier,cury*multiplier,multiplier,multiplier,colors[px])
+            curx += 1
+        curx=x+offx
+        cury+=1
+
+
 def draw_map(mapid: int):
     if mapid==0:
         fill_rect(0,40,320,182,colors[4]) # table
@@ -28,4 +63,5 @@ def draw_map(mapid: int):
         fill_rect(122,206,76,8,colors[2])
         fill_rect(122,214,76,8,colors[15])
 
-draw_map(0)
\ No newline at end of file
+draw_map(0)
+draw_sprite("player_up", 0, 0, 2)
\ No newline at end of file
index 7827391d8577ba46e5138a68cd020a48373b30b3..86a6a25b0982179bad901e6c68aaadc7222a86b6 100644 (file)
@@ -2,12 +2,14 @@ from os import listdir
 from os.path import isfile, join
 from PIL import Image
 import numpy as np
+import pprint
 
-path = "./src/sprites/"
-files_list = [f for f in listdir(path) if isfile(join(path, f))]
 colors = [(26,28,44),(93,39,93),(177,62,83),(239,125,87),(255,205,117),(167,240,112),(56,183,100),(37,113,121),(41,54,111),(59,93,201),(65,166,246),(115,239,247),(244,244,244),(148,176,194),(86,108,134),(51,60,87)]
 
-def get_color_id(r, g, b, a)->str:
+def get_color_id(r: int, g: int, b: int, a: int)->str:
+    """
+    Get the pixel color id, it is a str that goes from 0 to h
+    """
     if a ==0: # if transparent
         return "g"
     try:
@@ -31,7 +33,10 @@ def get_color_id(r, g, b, a)->str:
             case 15:
                 return "f"
 
-def get_offset_x(img, w, h)->int:
+def get_offset_x(img: Image.Image, w: int, h: int)->int:
+    """
+    Returns the x offset of an image
+    """
     offsetx = 0
     for i in range(h):
         for i in range(w):
@@ -42,7 +47,10 @@ def get_offset_x(img, w, h)->int:
         offsetx+=1
     return offsetx
 
-def get_offset_y(img, w, h)->int:
+def get_offset_y(img: Image.Image, w: int, h: int)->int:
+    """
+    Returns the y offset of an image
+    """
     offsety = 0
     for i in range(w):
         for i in range(h):
@@ -53,33 +61,85 @@ def get_offset_y(img, w, h)->int:
         offsety+=1
     return offsety
 
-def get_croped_image(img)-> tuple[Image.Image, int, int]:
+def get_croped_image(img: Image.Image)->tuple[Image.Image, int, int]:
+    """
+    Removes the borders (transparent) from the image and return the x and y offsets to get it to the right position
+    """
     w, h = img.size
-    offsetx, offsety = get_offset_x(image, w, h), get_offset_y(img, w, h)
-    img_reversed = image.rotate(180)
+    offsetx, offsety = get_offset_x(img, w, h), get_offset_y(img, w, h)
+    img_reversed = img.rotate(180)
     neg_offsetx, neg_offsety = get_offset_y(img_reversed, w, h), get_offset_x(img_reversed, w, h)
     img_croped = img.crop((offsetx, offsety, h-neg_offsety, w-neg_offsetx))
-    print("w;", w, "h",h,"x:",offsetx, "y:", offsety, "neg x:", neg_offsetx, "neg y:", neg_offsety)
-    print(h-offsetx-neg_offsetx, w-offsety-neg_offsety)
     return (img_croped, offsetx, offsety)
 
-image = Image.open(join(path, "salade.png"))
-image_croped, off_x, off_y = get_croped_image(image)
-w, h = image_croped.size
-result = ""
-for v in range(h):
-    for k in range(w):
-        result += get_color_id(*image_croped.getpixel((k, v)))
-print('result is,', result)
-image_croped.show()
-exit()
+def compress_string_image(img: str)->str:
+    pass
 
+def convert_all_files(save_to_file: bool, compress_sprites: bool, path: str)->None:
+    files_list = [f for f in listdir(path) if isfile(join(path, f))]
+    result_file = None
+    final_result = {}
+    if save_to_file: 
+        result_file = open(join(path, "sprites_converted.txt"), "w")
 
-for file in files_list:
-    if not file.endswith(".png"):
-        print("Skipping", file, ", because it's not a png image.")
-        break
-    image = Image.open(join(path, file))
-    w, h = image.size
-    offsetx, offsety = 0,0 
+    for file in files_list:
+        if not file.endswith(".png"):
+            print("Skipping", file, ", because it's not a png image.")
+            break
+        image = Image.open(join(path, file))
+        image_croped, off_x, off_y = get_croped_image(image)
+        w, h = image_croped.size
+        result = ""
+        for v in range(h):
+            for k in range(w):
+                result += get_color_id(*image_croped.getpixel((k, v)))
+        print('Finished,', file, '\n', result, '\n')
+        final_result[file.removesuffix(".png")] = (off_x, off_y, w, h, result)
+    if save_to_file: 
+        result_file.write(file + "\n" + pprint.pformat(final_result, compact=True) + '\n')
+        result_file.close()
 
+def gui()->None:
+    print("""
+  ___          _ _             ___                     _           
+ / __|_ __ _ _(_) |_ ___ ___  / __|___ _ ___ _____ _ _| |_ ___ _ _ 
+ \__ \ '_ \ '_| |  _/ -_|_-< | (__/ _ \ ' \ V / -_) '_|  _/ -_) '_|
+ |___/ .__/_| |_|\__\___/__/  \___\___/_||_\_/\___|_|  \__\___|_|  
+     |_|                                                           
+  - An Ayabusa software
+      
+What would you like to do?
+    [1] Convert all sprites and print them
+    [2] Convert all sprites and save them to file (sprites_converted.txt)
+    [other] Quit/Cancel""")
+    save_to_file = False
+    compress_sprites = True
+    path = "./src/sprites/"
+
+    choice = input("Your choice [1]: ")
+    if choice == "1" or choice == "":
+        save_to_file = False
+    elif choice == "2":
+        save_to_file = True
+    else:
+        print("Invalid input exiting the program.")
+        exit()
+
+    choice = input("Where are your sprites located ? [./src/sprites/]: ")
+    if choice != "":
+        path = choice
+
+    choice = input("Would you like to compress the spites ? (Y/N) [Y]: ")
+    if choice == "Y" or choice == "":
+        compress_sprites = True
+    elif choice == "N":
+        compress_sprites = False
+    else:
+        print("Invalid input exiting the program.")
+        exit()
+    print("Convertion started ...\n")
+    convert_all_files(save_to_file, compress_sprites, path)
+    print("Convertion finished ...")
+
+# Launch the text UI
+gui()
\ No newline at end of file
diff --git a/src/sprites/sprites_converted.txt b/src/sprites/sprites_converted.txt
new file mode 100644 (file)
index 0000000..4726cde
--- /dev/null
@@ -0,0 +1,23 @@
+sprites_converted.txt
+{'assiette': (0, 2, 20, 17,
+              'ggggggg000000gggggggggggg00cccccc00gggggggg00cccccccccc00ggggg0cccccccccccccc0ggg0cccccddddddccccc0gg0ccccdccccccdcccc0g0ccccdccccccccdcccc00cccdccccccccccdccd00cccdcccccccccccccd00dccdcccccccccccccd00dcccdccccccccccccd0g0dcccccddcccccccd0gg0dccccccccccccccd0ggg0ddccccccccccdd0ggggg00ddccccccdd00gggggggg00dddddd00gggggggggggg000000ggggggg'),
+ 'oignon': (3, 3, 16, 13,
+            'ggggggggggg00ggggggg00000g0650ggggg021122016650ggg0111c111210660g0211c111210g0500211cc11111200500211c11211110g0g012c112111110ggg012c111112120gggg01121112110gggg01221112110gggggg010122110gggggggg0g00000ggggggg'),
+ 'oignon_coupe': (3, 5, 13, 14,
+                  'gggggg00000ggggg000ccccc0ggg0cccc000c0gg0c00c100g0c00c0g0c1cc00c00c00g0c11cc1001cc00ccc110gg011cc1000c0ggg0011c0g0c0ggggg00c00g0c0ggggg01cc00c0gggggg011cc10ggggggg00110gggggggggg00gg'),
+ 'planche': (0, 5, 20, 14,
+             'ggggg00000000000000ggggg0333333333333330g00g0333003333333330033003320c0333323330033333330cd022233330032333330ddc023333300332233330ddd033333002200333330ddd033330g00g03333330ddf03330gggg022222220fdf0220ggggg00000000ffff00gggggggggggggg00fdf0gggggggggggggggg0ff0ggggggggggggggggg00gg'),
+ 'player_down': (3, 0, 15, 20,
+                 'gg00000000000ggg0ccc0ccc0ccc0g0ccc0ccccc0ccc00ccc0ccccc0ccc00ccc0ccccc0ccc0g0ccc0ccc0ccc0ggg000000cc000gggg0ccccccccc0gggg00000000000ggggg04c848c40gggggg044444440ggggggg0000000ggggggg0cccdccc0ggggg0cccccdccc0gggg0c0cfcdc0c0gggg0c0cccdc0c0gggg000cfcdc000gggggg0cccdc0gggggggg0000000ggggggggg00g00ggggg'),
+ 'player_left': (3, 0, 15, 20,
+                 'gg00000000000ggg0ccc0ccc0ccc0g0ccc0ccccc0ccc00ccc0ccccc0ccc00ccc0ccccc0ccc0g0ccc0ccc0ccc0ggg000000cc000gggg0ccccccccc0gggg00000000000ggggg08c48c440gggggg044444440ggggggg0000000ggggggg0ccdcccc0ggggg0ccccdcccc0gggg0c0cfcd0cc0gggg0c0cccdc0c0gggg000cfcdc000gggggg0cccdc0gggggggg0000000ggggggg000gg000gggg'),
+ 'player_right': (2, 0, 15, 20,
+                  'gg00000000000ggg0ccc0ccc0ccc0g0ccc0ccccc0ccc00ccc0ccccc0ccc00ccc0ccccc0ccc0g0ccc0ccc0ccc0ggg000cc000000gggg0ccccccccc0gggg00000000000ggggg044c84c80gggggg044444440ggggggg0000000ggggggg0ccccdcc0ggggg0ccccdcccc0gggg0cc0dcfc0c0gggg0c0cdccc0c0gggg000cdcfc000gggggg0cdccc0gggggggg0000000gggggggg000gg000ggg'),
+ 'player_up': (3, 0, 15, 20,
+               'gg00000000000ggg0ccc0ccc0ccc0g0ccc0ccccc0ccc00ccc0ccccc0ccc00ccc0ccccc0ccc0g0ccc0ccc0ccc0ggg000000cc000gggg0ccccccccc0gggg00000000000ggggg044444440gggggg044444440ggggggg0000000ggggggg0ccccccc0ggggg0ccccccccc0gggg0c0ccccc0c0gggg0c0ccccc0c0gggg000ccccc000gggggg0ccccc0gggggggg0000000ggggggggg00g00ggggg'),
+ 'poele': (0, 0, 19, 19,
+           'gggggg00000000gggggggg000eeeeeeee000gggg0eeeffffffffeee0gg0eeffeeeeeeeeffee0g0efeeeeeeeeeeeefe0g0efeeeeeeeeeeeefe0g0eeeeeeeeeeeeeeee0g00eeeeeeeeeeeeee00g0b0000eeeeee0000f0g000fff000000ffff00g00fffffffffff000f0g0ff0000000000baf0g0ff0fabfffbafffba0g0f0ffbff2f3b3ffff0gg0fff0ffffffff0fff0g0fff0000000000fff0g0fff0gggggggg0fff0g0ff0gggggggggg0ff0gg00gggggggggggg00g'),
+ 'salade': (1, 4, 18, 13,
+            'gggggggggggg0gggggggggg0000g0070gggggggg07777077570ggggg00755567567570ggg077567777777570gg07657765655767770g077776556556755770000076556556767070ggg075556555670g00ggg075556555670gggggg07665655670gggggggg077777770gggggggggg0000000gggggg'),
+ 'salade_cuite': (2, 7, 15, 9,
+                  'gggg0000000ggggg0055765567000g077755555556770g0655655555560g075566566565550g5666656655760g050705576750700g0g0g070606g0gggggggg0g0g0gggg')}