You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
85 lines
3.2 KiB
85 lines
3.2 KiB
import json |
|
import tkinter as tk |
|
import random |
|
from units import rat |
|
import uuid |
|
import subprocess |
|
|
|
class MiceMaze: |
|
def __init__(self, maze_file): |
|
with open(maze_file, 'r') as file: |
|
self.maze = json.load(file) |
|
self.cell_size = 60 |
|
self.height = len(self.maze) |
|
self.width = len(self.maze[0]) |
|
self.window = tk.Tk() |
|
self.tunnel = tk.PhotoImage(file=f"Rat/BMP_TUNNEL.png").zoom(3) |
|
self.grasses = [] |
|
for i in range(4): |
|
self.grasses.append(tk.PhotoImage(file=f"Rat/BMP_1_GRASS_{i+1}.png").zoom(3)) |
|
self.menu = tk.Menu(self.window) |
|
self.menu.add_command(label="Quit", command=self.window.destroy) |
|
self.status_bar = tk.Label(self.window, text="Welcome to Mice!", bd=1, relief=tk.SUNKEN, anchor=tk.W) |
|
self.status_bar.pack(side=tk.BOTTOM, fill=tk.X) |
|
self.window.title("Mice!") |
|
self.window.bind("<KeyPress>", self.key_pressed) |
|
self.canvas = tk.Canvas(self.window, width=self.width*self.cell_size, height=self.height*self.cell_size) |
|
self.canvas.pack() |
|
self.units = {} |
|
self.new_rat() |
|
self.window.config(menu=self.menu) |
|
|
|
def new_rat(self, position=None): |
|
if position is None: |
|
position = self.choose_start() |
|
id = uuid.uuid4() |
|
if random.random() < 0.5: |
|
self.units.update({id : rat.Male(self, position, id)}) |
|
else: |
|
self.units.update({id : rat.Female(self, position, id)}) |
|
|
|
# Cacheare le posizioni valide per migliorare le performance |
|
def choose_start(self): |
|
if not hasattr(self, '_valid_positions'): |
|
self._valid_positions = [ |
|
(x, y) for y in range(1, self.height-1) |
|
for x in range(1, self.width-1) |
|
if self.maze[y][x] |
|
] |
|
return random.choice(self._valid_positions) |
|
|
|
def draw_maze(self): |
|
for y in range(self.height): |
|
for x in range(self.width): |
|
color = "black" if self.maze[y][x] else "white" |
|
self.canvas.create_rectangle(x*self.cell_size, y*self.cell_size, (x+1)*self.cell_size, (y+1)*self.cell_size, fill=color) |
|
variant = random.randint(0, 3) |
|
tile = self.grasses[variant] if self.maze[y][x] else self.tunnel |
|
self.canvas.create_image(x*self.cell_size, y*self.cell_size, image=tile, anchor="nw", tag="maze") |
|
|
|
def update_maze(self): |
|
self.canvas.delete("unit") |
|
for unit in self.units.copy().values(): |
|
unit.move() |
|
unit.collisions() |
|
unit.draw() |
|
self.status_bar.config(text=f"Mice: {len(self.units)}") |
|
self.window.after(40, self.update_maze) |
|
|
|
def run(self): |
|
self.draw_maze() |
|
self.update_maze() |
|
self.window.mainloop() |
|
|
|
def key_pressed(self, event): |
|
if event.keysym == "q": |
|
self.window.destroy() |
|
if event.keysym == "r": |
|
self.new_rat() |
|
if event.keysym == "d": |
|
if self.units: |
|
self.units[random.choice(list(self.units.keys()))].die() |
|
def play_sound(self, sound_file): |
|
subprocess.Popen(["aplay", f"sound/{sound_file}"]) |
|
solver = MiceMaze('maze.json') |
|
solver.run()
|
|
|