Refactor key handling and add keybindings configuration for game actions

This commit is contained in:
Matteo Benedetto
2025-08-19 18:59:06 +02:00
parent 4099b6f72e
commit 1269913275
6 changed files with 79 additions and 20 deletions
+32 -4
View File
@@ -2,9 +2,38 @@
# The key_pressed method is called when a key is pressed, and it contains the logic for handling different key presses.
import random
import yaml
# read yaml config file
bindings = {}
with open("conf/keybindings.yaml", "r") as f:
bindings = yaml.safe_load(f)
class KeyBindings:
def trigger(self, action):
print(f"Triggering action: {action}")
# Check if the action is in the bindings
if action in bindings[f"keybinding_{self.game_status}"]:
value = bindings[f"keybinding_{self.game_status}"][action]
# Call the corresponding method
if value:
print(f"Calling method: {value}")
if "|" in value:
method_name, *args = value.split("|")
method = getattr(self, method_name)
method(*args)
else:
getattr(self, value)()
else:
print(f"Action {action} not found in keybindings for {self.game_status}")
return
print(f"Action {action} not found in keybindings for {self.game_status}")
return None
def key_pressed(self, key, coords=None):
if key != "mouse":
key = [key, coords]
print(f"Key pressed: {key}")
keybindings = self.configs[f"keybinding_{self.game_status}"]
if key in keybindings.get("quit", []):
self.render_engine.close()
@@ -46,12 +75,11 @@ class KeyBindings:
self.points = 0
self.start_game()
def spawn_new_bomb(self):
self.spawn_bomb(self.pointer)
def quit_game(self):
self.render_engine.close()
def key_released(self, key):
if key in ["Up", "Down", "Left", "Right", 8, 9, 10, 11]:
self.stop_scrolling()
def start_scrolling(self, direction):
self.scrolling_direction = direction
if not self.scrolling:
+14 -12
View File
@@ -68,7 +68,7 @@ class GameWindow:
self.white_flash_opacity = 255
# Input handling
self.key_down, self.key_up, self.axis_scroll = key_callback
self.trigger = key_callback
self.button_cursor = [0, 0]
self.buttons = {}
@@ -379,27 +379,29 @@ class GameWindow:
for event in events:
if event.type == sdl2.SDL_QUIT:
self.running = False
elif event.type == sdl2.SDL_KEYDOWN and self.key_down:
elif event.type == sdl2.SDL_KEYDOWN:
# print in file keycode
keycode = event.key.keysym.sym
open("keycode.txt", "a").write(f"{keycode}\n")
key = sdl2.SDL_GetKeyName(event.key.keysym.sym).decode('utf-8')
# Check for Right Ctrl key to trigger white flash
if event.key.keysym.sym == sdl2.SDLK_RCTRL:
self.trigger_white_flash()
else:
self.key_down(key)
elif event.type == sdl2.SDL_KEYUP and self.key_up:
self.trigger(f"keydown_{key}")
elif event.type == sdl2.SDL_KEYUP:
key = sdl2.SDL_GetKeyName(event.key.keysym.sym).decode('utf-8')
self.key_up(key)
self.trigger(f"keyup_{key}")
elif event.type == sdl2.SDL_MOUSEMOTION:
self.key_down("mouse", coords=(event.motion.x, event.motion.y))
self.trigger(f"mousemove_{event.motion.x}, {event.motion.y}")
elif event.type == sdl2.SDL_JOYBUTTONDOWN:
key = event.jbutton.button
self.key_down(key)
self.trigger(f"joybuttondown_{key}")
elif event.type == sdl2.SDL_JOYBUTTONUP:
key = event.jbutton.button
self.key_up(key)
self.trigger(f"joybuttonup_{key}")
elif event.type == sdl2.SDL_JOYHATMOTION:
hat = event.jhat.hat
value = event.jhat.value
self.trigger(f"joyhatmotion_{hat}_{value}")
# Present the rendered frame
self.renderer.present()