# -*- coding: utf-8 -*- import os from PIL import Image, ImageDraw, ImageFont # Configuration BASE_DIR = os.path.dirname(os.path.abspath(__file__)) ASSETS_DIR = os.path.join(BASE_DIR, "assets") CARDS_DIR = os.path.join(ASSETS_DIR, "cards") UI_DIR = os.path.join(ASSETS_DIR, "ui") # Fonction pour créer un dossier s'il n'existe pas def makedirs_safe(path): if not os.path.exists(path): os.makedirs(path) # Assurer que les dossiers existent makedirs_safe(CARDS_DIR) makedirs_safe(UI_DIR) # Données des cartes CARDS = [ {"id": "knight", "name": "Chevalier", "color": "#3498db"}, {"id": "archers", "name": "Archers", "color": "#e67e22"}, {"id": "giant", "name": "Géant", "color": "#f1c40f"}, {"id": "goblins", "name": "Gobelins", "color": "#2ecc71"}, {"id": "wizard", "name": "Sorcier", "color": "#9b59b6"}, {"id": "skeletons", "name": "Squelettes", "color": "#bdc3c7"}, {"id": "fireball", "name": "Boule de Feu", "color": "#e74c3c"}, {"id": "arrows", "name": "Flèches", "color": "#1abc9c"}, ] # Données des coffres CHESTS = [ {"id": "chest_free", "name": "Gratuit", "color": "#95a5a6"}, {"id": "chest_silver", "name": "Argent", "color": "#bdc3c7"}, {"id": "chest_gold", "name": "Or", "color": "#f1c40f"}, {"id": "chest_magic", "name": "Magique", "color": "#9b59b6"}, ] # Données des éléments UI supplémentaires UI_ELEMENTS = [ {"id": "background", "name": "Arène", "color": "#27ae60", "size": (400, 600)}, {"id": "tower", "name": "Tour", "color": "#7f8c8d", "size": (60, 60)}, {"id": "drapeau_joueur", "name": "Drapeau J", "color": "#3498db", "size": (40, 30)}, {"id": "drapeau_enemy", "name": "Drapeau E", "color": "#e74c3c", "size": (40, 30)}, ] def create_placeholder_image(filename, text, bg_color, size=(140, 180), is_chest=False): img = Image.new('RGB', size, color=bg_color) d = ImageDraw.Draw(img) # Charger une police par défaut try: font = ImageFont.truetype("arial.ttf", 20) font_big = ImageFont.truetype("arial.ttf", 40) except IOError: try: font = ImageFont.truetype("DejaVuSans.ttf", 40) font_big = ImageFont.truetype("DejaVuSans.ttf", 40) except IOError: font = ImageFont.load_default() font_big = ImageFont.load_default() width, height = size # Dessiner une bordure d.rectangle([5, 5, width-5, height-5], outline="white", width=3) if is_chest: # Dessiner une forme de coffre simple d.rectangle([30, 60, width-30, height-40], fill="brown", outline="white") d.arc([30, 40, width-30, 80], 180, 0, fill="brown", width=20) else: # Dessiner une icône générique (cercle pour tête) d.ellipse([width//2 - 30, 40, width//2 + 30, 100], fill="white", outline="black") # Centrer le texte try: # Pillow >= 8.0.0 bbox = d.textbbox((0, 0), text, font=font) text_w = bbox[2] - bbox[0] text_h = bbox[3] - bbox[1] try: d.text(((width - text_w) // 2, 120), text, fill="white", font=font, stroke_width=1, stroke_fill="black") except (ValueError, TypeError): d.text(((width - text_w) // 2, 120), text, fill="white", font=font) # Initiale au centre initial = text[0] bbox_i = d.textbbox((0, 0), initial, font=font_big) text_iw = bbox_i[2] - bbox_i[0] text_ih = bbox_i[3] - bbox_i[1] d.text(((width - text_iw) // 2, 55), initial, fill="black", font=font_big) except AttributeError: # Pillow < 8.0.0 (fallback) text_w, text_h = d.textsize(text, font=font) d.text(((width - text_w) // 2, 120), text, fill="white", font=font) # Initiale au centre (fallback) initial = text[0] text_iw, text_ih = d.textsize(initial, font=font_big) d.text(((width - text_iw) // 2, 55), initial, fill="black", font=font_big) # Sauvegarder l'image try: img.save(filename) print("Généré : {}".format(filename)) except Exception as e: print("Erreur lors de la sauvegarde de {} : {}".format(filename, e)) def main(): print("Génération des assets...") # Générer les cartes for card in CARDS: path = os.path.join(CARDS_DIR, "{}.png".format(card['id'])) create_placeholder_image(path, card['name'], card['color']) # Générer les coffres (taille carrée pour l'UI) for chest in CHESTS: path = os.path.join(UI_DIR, "{}.png".format(chest['id'])) create_placeholder_image(path, chest['name'], chest['color'], size=(100, 100), is_chest=True) # Générer les éléments UI (Background, Tower, Flags) for elem in UI_ELEMENTS: path = os.path.join(UI_DIR, "{}.png".format(elem['id'])) create_placeholder_image(path, elem['name'], elem['color'], size=elem['size']) print("Terminé ! Vous pouvez lancer le jeu.") if __name__ == "__main__": main()