import tkinter as tk import os class GameWindow: """Classe che gestisce la finestra di gioco e il rendering grafico.""" def __init__(self, width, height, cell_size, title, key_callback=None): self.cell_size = cell_size self.window = tk.Tk() self.window.title(title) self.canvas = tk.Canvas(self.window, width=width*cell_size, height=height*cell_size) self.canvas.pack() self.menu = tk.Menu(self.window) self.menu.add_command(label="Quit", command=self.window.destroy) self.status_bar = tk.Label(self.window, text=title, bd=1, relief=tk.SUNKEN, anchor=tk.W) self.status_bar.pack(side=tk.BOTTOM, fill=tk.X) self.window.config(menu=self.menu) if key_callback: self.window.bind("", key_callback) def load_image(self, path, transparent_color=None): image = tk.PhotoImage(file=os.path.join(os.path.dirname(__file__), "..", "assets", path)) if transparent_color: gray_pixels = [] for y in range(image.height()): for x in range(image.width()): r, g, b = image.get(x, y) if r == transparent_color[0] and g == transparent_color[1] and b == transparent_color[2]: gray_pixels.append((x, y)) for x, y in gray_pixels: image.transparency_set(x, y, 1) return image.zoom(self.cell_size // 20) def bind(self, event, callback): self.window.bind(event, callback) def draw_image(self, x, y, image, tag, anchor="nw"): self.canvas.create_image(x, y, image=image, anchor=anchor, tag=tag) def draw_rectangle(self, x, y, width, height, tag, outline="red"): self.canvas.create_rectangle(x, y, x+width, y+height, outline=outline, tag=tag) def delete_tag(self, tag): self.canvas.delete(tag) def update_status(self, text): self.status_bar.config(text=text) def new_cycle(self, delay, callback): self.window.after(delay, callback) def mainloop(self, **kwargs): kwargs["update"]() self.window.mainloop() def get_image_size(self, image): return image.width(), image.height()