Aggiungi supporto per la risoluzione personalizzata e gestione della pausa nel gioco

This commit is contained in:
2024-12-23 00:59:48 +01:00
parent 42e2f4aa16
commit 97038023d5
10 changed files with 93 additions and 24 deletions
+50 -9
View File
@@ -1,12 +1,12 @@
#!/usr/bin/python3
import cProfile
import pstats
import random
from units import rat, bomb
import uuid
import subprocess
from engine import maze, sdl2 as engine, controls
import os
import datetime
import subprocess
class MiceMaze(controls.KeyBindings):
def __init__(self, maze_file):
@@ -23,7 +23,10 @@ class MiceMaze(controls.KeyBindings):
self.unit_positions = {}
self.unit_positions_before = {}
self.scrolling_direction = None
self.pause = False
self.game_end = (False, None)
self.scrolling = False
self.sounds = {}
for _ in range(5):
self.new_rat()
@@ -55,8 +58,41 @@ class MiceMaze(controls.KeyBindings):
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 game_over(self):
if self.game_end[0]:
if not self.game_end[1]:
self.engine.win_screen("Game Over: Mice are too many!", image=self.assets["BMP_WEWIN"])
else:
self.engine.win_screen(f"You Win! Points: {self.points}", image=self.assets["BMP_WEWIN"])
return True
if len(self.units) >= 150:
self.stop_sound()
self.play_sound("WEWIN.WAV")
self.game_end = (True, False)
return True
if not len(self.units):
self.stop_sound()
self.play_sound("VICTORY.WAV")
self.game_end = (True, True)
self.save_score()
return True
def save_score(self):
with open("scores.txt", "a") as f:
f.write(f"{datetime.datetime.now()} - {self.points}\n")
def read_score(self):
with open("scores.txt") as f:
return f.read().splitlines()
def update_maze(self):
if self.pause:
self.engine.pause_screen("Paused")
return
if self.game_over():
return
self.engine.delete_tag("unit")
self.engine.delete_tag("effect")
self.engine.draw_pointer(self.pointer[0] * self.cell_size, self.pointer[1] * self.cell_size)
@@ -87,10 +123,17 @@ class MiceMaze(controls.KeyBindings):
)
self.engine.scroll_view(self.pointer)
def play_sound(self, sound_file):
def play_sound(self, sound_file,tag="main"):
if self.audio:
subprocess.Popen(["aplay", f"sound/{sound_file}"])
#self.engine.play_sound(f"sound/{sound_file}")
if len(self.sounds) > 5:
self.sounds.pop(next(iter(self.sounds))).kill()
self.sounds[f"{tag}_{random.random()}"] = subprocess.Popen(["aplay", f"sound/{sound_file}"])
def stop_sound(self, tag=None):
for key, value in self.sounds.copy().items():
if tag is None or tag in key:
value.kill()
del self.sounds[key]
def graphics_load(self):
self.tunnel = self.engine.load_image("Rat/BMP_TUNNEL.png")
@@ -131,8 +174,6 @@ class MiceMaze(controls.KeyBindings):
self.engine.new_cycle(100, self.scroll)
if __name__ == "__main__":
profiler = cProfile.Profile()
profiler.enable()
solver = MiceMaze('maze.json')
solver.run()