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.

79 lines
2.8 KiB

import random
from units import rat
import uuid
import subprocess
from engine import maze, sdl2 as engine
class MiceMaze:
def __init__(self, maze_file):
self.map = maze.Map(maze_file)
self.audio = True
self.cell_size = 60
self.engine = engine.GameWindow(self.map.width, self.map.height, self.cell_size, "Mice!", key_callback=self.key_pressed)
self.graphics_load()
self.units = {}
for _ in range(5):
self.new_rat()
def new_rat(self, position=None):
if position is None:
position = self.choose_start()
id = uuid.uuid4()
rat_class = rat.Male if random.random() < 0.5 else rat.Female
self.units[id] = rat_class(self, position, id)
def choose_start(self):
if not hasattr(self, '_valid_positions'):
self._valid_positions = [
(x, y) for y in range(1, self.map.height-1)
for x in range(1, self.map.width-1)
if self.map.matrix[y][x]
]
return random.choice(self._valid_positions)
def draw_maze(self):
for y, row in enumerate(self.map.matrix):
for x, cell in enumerate(row):
variant = x*y % 4
tile = self.grasses[variant] if cell else self.tunnel
self.engine.draw_image(x * self.cell_size, y * self.cell_size, tile, tag="maze")
def update_maze(self):
self.engine.delete_tag("unit")
for unit in self.units.copy().values():
unit.move()
unit.collisions()
unit.draw()
self.engine.update_status(f"Mice: {len(self.units)}")
self.engine.new_cycle(50, self.update_maze)
def run(self):
self.draw_maze()
self.engine.mainloop(update=self.update_maze, bg_update=self.draw_maze)
def key_pressed(self, event):
if event.keysym == "q":
self.engine.window.destroy()
elif event.keysym == "r":
self.new_rat()
elif event.keysym == "d":
if self.units:
self.units[random.choice(list(self.units.keys()))].die()
elif event.keysym == "m":
self.audio = not self.audio
def play_sound(self, sound_file):
if self.audio:
subprocess.Popen(["aplay", f"sound/{sound_file}"])
def graphics_load(self):
self.tunnel = self.engine.load_image("Rat/BMP_TUNNEL.png")
self.grasses = [self.engine.load_image(f"Rat/BMP_1_GRASS_{i+1}.png") for i in range(4)]
self.rat_assets = {}
for sex in ["MALE", "FEMALE", "BABY"]:
self.rat_assets[sex] = {}
for direction in ["UP", "DOWN", "LEFT", "RIGHT"]:
self.rat_assets[sex][direction] = self.engine.load_image(f"Rat/BMP_{sex}_{direction}.png", transparent_color=(128, 128, 128))
solver = MiceMaze('maze.json')
solver.run()