# This file contains the Controls class, which is responsible for handling user input. # The key_pressed method is called when a key is pressed, and it contains the logic for handling different key presses. import random import os import json bindings = {} if os.path.exists("conf/keybindings.json"): with open("conf/keybindings.json", "r") as f: bindings = json.load(f) else: import yaml # read yaml config file with open("conf/keybindings.yaml", "r") as f: bindings = yaml.safe_load(f) class KeyBindings: def trigger(self, action): import os debug = os.environ.get('DEBUG_KEYS', '').lower() == 'true' if debug: print(f"[KEY] Triggering action: {action} (status: {self.game_status})") # Check if the action is in the bindings current_bindings = bindings.get(f"keybinding_{self.game_status}", {}) if action in current_bindings: value = current_bindings[action] if debug: print(f"[KEY] Found binding: {action} -> {value}") # Call the corresponding method if value: if "|" in value: method_name, *args = value.split("|") method = getattr(self, method_name) if debug: print(f"[KEY] Calling method: {method_name}({args})") method(*args) else: if debug: print(f"[KEY] Calling method: {value}()") getattr(self, value)() return if debug: print(f"[KEY] Action '{action}' not found in {self.game_status} bindings") print(f"[KEY] Available actions: {list(current_bindings.keys())}") return None def spawn_new_bomb(self): self.spawn_bomb(self.pointer) def spawn_new_mine(self): self.spawn_mine(self.pointer) def spawn_new_nuclear_bomb(self): self.spawn_nuclear_bomb(self.pointer) def spawn_new_gas(self): self.spawn_gas(self.pointer) def toggle_audio(self): self.render_engine.audio = not self.render_engine.audio def toggle_pause(self): self.game_status = "paused" if self.game_status == "game" else "game" def toggle_full_screen(self): self.full_screen = not self.full_screen self.render_engine.full_screen(self.full_screen) def quit_game(self): self.render_engine.close() def start_scrolling(self, direction): self.scrolling_direction = direction if not self.scrolling: self.scrolling = 1 def stop_scrolling(self): self.scrolling = 0 def scroll(self): if self.scrolling: if not self.scrolling % 5: if self.scrolling_direction == "Up": self.scroll_cursor(y=-1) elif self.scrolling_direction == "Down": self.scroll_cursor(y=1) elif self.scrolling_direction == "Left": self.scroll_cursor(x=-1) elif self.scrolling_direction == "Right": self.scroll_cursor(x=1) self.scrolling += 1 def axis_scroll(self, x, y): self.scroll_cursor(1 if x > 0 else -1, 1 if y > 0 else -1)