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.
99 lines
3.5 KiB
99 lines
3.5 KiB
#!/usr/bin/python3 |
|
import cProfile |
|
import pstats |
|
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 = 40 |
|
self.engine = engine.GameWindow(self.map.width, self.map.height, self.cell_size, "Mice!", key_callback=self.key_pressed) |
|
self.graphics_load() |
|
self.units = {} |
|
self.unit_positions = {} |
|
self.unit_positions_before = {} |
|
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") |
|
self.unit_positions.clear() |
|
self.unit_positions_before.clear() |
|
for unit in self.units.values(): |
|
self.unit_positions.setdefault(unit.position, []).append(unit) |
|
self.unit_positions_before.setdefault(unit.position_before, []).append(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, key): |
|
print(key) |
|
if key == "Q": |
|
self.engine.window.close() |
|
elif key == "Return": |
|
self.new_rat() |
|
elif key == "D": |
|
if self.units: |
|
self.units[random.choice(list(self.units.keys()))].die() |
|
elif key == "M": |
|
self.audio = not self.audio |
|
elif key == "S": |
|
profiler.disable() |
|
stats = pstats.Stats(profiler).sort_stats('cumtime') |
|
stats.print_stats() |
|
|
|
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)) |
|
if __name__ == "__main__": |
|
profiler = cProfile.Profile() |
|
profiler.enable() |
|
|
|
solver = MiceMaze('maze.json') |
|
solver.run() |
|
|
|
|