Aggiorna la gestione dei tasti e aggiungi nuovi file di configurazione per le schermate di pausa e menu di avvio

This commit is contained in:
2024-12-28 00:34:48 +01:00
parent 69940890ec
commit 6dc871db54
7 changed files with 71 additions and 41 deletions
+29 -24
View File
@@ -6,7 +6,6 @@ import uuid
from engine import maze, sdl2 as engine, controls
import os
import datetime
import subprocess
import json
class MiceMaze(controls.KeyBindings):
@@ -26,17 +25,22 @@ class MiceMaze(controls.KeyBindings):
self.unit_positions = {}
self.unit_positions_before = {}
self.scrolling_direction = None
self.pause = False
self.game_status = "start_menu"
self.game_end = (False, None)
self.dialog = "start_menu"
self.scrolling = False
self.sounds = {}
self.start_game()
self.background_texture = None
keybindings_file, = "keybinding_game.json",
with open(os.path.join("conf", keybindings_file)) as f:
self.keybindings = json.load(f)
self.configs = self.get_config()
def get_config(self):
configs = {}
for file in os.listdir("conf"):
if file.endswith(".json"):
with open(os.path.join("conf", file)) as f:
configs[file[:-5]] = json.load(f)
return configs
def start_game(self):
for _ in range(5):
self.new_rat()
@@ -91,15 +95,19 @@ class MiceMaze(controls.KeyBindings):
return True
if self.count_rats() > 200:
self.stop_sound()
self.play_sound("WEWIN.WAV")
self.game_end = (True, False)
self.game_status = "paused"
return True
if not len(self.units):
self.stop_sound()
self.play_sound("VICTORY.WAV")
self.play_sound("WELLDONE.WAV", tag="effects")
self.game_end = (True, True)
self.game_status = "paused"
self.save_score()
return True
@@ -108,17 +116,23 @@ class MiceMaze(controls.KeyBindings):
f.write(f"{datetime.datetime.now()} - {self.points}\n")
def read_score(self):
table = []
with open("scores.txt") as f:
return f.read().splitlines()
rows = f.read().splitlines()
for row in rows:
table.append(row.split(" - "))
table.sort(key=lambda x: int(x[1]), reverse=True)
return table
def update_maze(self):
if self.pause:
if self.dialog != "start_menu":
self.engine.dialog("Pause")
self.engine.start_dialog(image=self.assets["BMP_WEWIN"])
return
if self.game_over():
return
if self.game_status == "paused":
self.engine.dialog("Pause")
return
if self.game_status == "start_menu":
self.engine.dialog("Welcome to the Mice!", subtitle="A game by Matteo because he was bored",image=self.assets["BMP_WEWIN"])
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)
@@ -149,21 +163,11 @@ class MiceMaze(controls.KeyBindings):
)
self.engine.scroll_view(self.pointer)
def play_sound(self, sound_file,tag="main"):
self.engine.play_sound(sound_file)
return
if self.audio:
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 play_sound(self, sound_file,tag="base"):
self.engine.play_sound(sound_file, tag=tag)
def stop_sound(self, tag=None):
self.engine.stop_sound()
return
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", surface=True)
@@ -180,6 +184,7 @@ class MiceMaze(controls.KeyBindings):
for file in os.listdir("assets/Rat"):
if file.endswith(".png"):
self.assets[file[:-4]] = self.engine.load_image(f"Rat/{file}")
def add_point(self, value):
self.points += value