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.
77 lines
3.2 KiB
77 lines
3.2 KiB
# 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 |
|
|
|
class KeyBindings: |
|
def key_pressed(self, key, coords=None): |
|
keybindings = self.configs[f"keybinding_{self.game_status}"] |
|
if key in keybindings.get("quit", []): |
|
self.render_engine.close() |
|
elif key in keybindings.get("new_rat", []): |
|
self.spawn_rat() |
|
elif key in keybindings.get("kill_rat", []): |
|
if self.units: |
|
self.units[random.choice(list(self.units.keys()))].die(score=5) |
|
elif key in keybindings.get("toggle_audio", []): |
|
self.audio = not self.audio |
|
elif key in keybindings.get("toggle_full_screen", []): |
|
self.full_screen = not self.full_screen |
|
self.render_engine.full_screen(self.full_screen) |
|
elif key in keybindings.get("scroll_up", []): |
|
self.start_scrolling("Up") |
|
elif key in keybindings.get("scroll_down", []): |
|
self.start_scrolling("Down") |
|
elif key in keybindings.get("scroll_left", []): |
|
self.start_scrolling("Left") |
|
elif key in keybindings.get("scroll_right", []): |
|
self.start_scrolling("Right") |
|
elif key in keybindings.get("spawn_bomb", []): |
|
self.spawn_bomb(self.pointer) |
|
elif key in keybindings.get("spawn_mine", []): |
|
self.spawn_mine(self.pointer) |
|
elif key in keybindings.get("spawn_nuclear_bomb", []): |
|
self.spawn_nuclear_bomb(self.pointer) |
|
elif key in keybindings.get("pause", []): |
|
self.game_status = "paused" if self.game_status == "game" else "game" |
|
elif key in keybindings.get("start_game", []): |
|
self.pause = False |
|
self.game_status = "game" |
|
self.start_game() |
|
elif key in keybindings.get("reset_game", []): |
|
self.pause = False |
|
self.game_status = "game" |
|
self.game_end = (False, None) |
|
self.units.clear() |
|
self.points = 0 |
|
self.start_game() |
|
|
|
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: |
|
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) |