From 4e3e91580aff9149032f3f0d01d1f3844eb0d43c Mon Sep 17 00:00:00 2001 From: Jan Lerking Date: Fri, 21 Mar 2025 14:45:22 +0100 Subject: [PATCH] Working on gamepad controls. /JL --- Gamepad | 1 + controls/__init__.py | 3 +++ controls/controlsbase.py | 21 ++++++++++++++++++ controls/gamepad.py | 25 ++++++++++++++++++++++ controls/joystick.py | 44 ++++++++++++++++++++++++++++++++++++++ controls/keyboard.py | 23 ++++++++++++++++++++ main.py | 3 +++ startscreen/startscreen.py | 5 +++++ 8 files changed, 125 insertions(+) create mode 160000 Gamepad create mode 100644 controls/__init__.py create mode 100644 controls/controlsbase.py create mode 100644 controls/gamepad.py create mode 100644 controls/joystick.py create mode 100644 controls/keyboard.py create mode 100644 startscreen/startscreen.py diff --git a/Gamepad b/Gamepad new file mode 160000 index 0000000..0166286 --- /dev/null +++ b/Gamepad @@ -0,0 +1 @@ +Subproject commit 016628623078f0efb8b366c59d83a841827c21c6 diff --git a/controls/__init__.py b/controls/__init__.py new file mode 100644 index 0000000..bf2d4fe --- /dev/null +++ b/controls/__init__.py @@ -0,0 +1,3 @@ +from . import gamepad +from . import joystick +from . import keyboard \ No newline at end of file diff --git a/controls/controlsbase.py b/controls/controlsbase.py new file mode 100644 index 0000000..452964d --- /dev/null +++ b/controls/controlsbase.py @@ -0,0 +1,21 @@ +""" +This is an abstract baseclass for the controls of snake. +""" +from abc import ABC, abstractmethod + +class ControlsBase(ABC): + @abstractmethod + def left(self): + pass + + def right(self): + pass + + def up(self): + pass + + def down(self): + pass + + def pause(self): + pass \ No newline at end of file diff --git a/controls/gamepad.py b/controls/gamepad.py new file mode 100644 index 0000000..203015a --- /dev/null +++ b/controls/gamepad.py @@ -0,0 +1,25 @@ +""" +Class for grabbing gamepad signals +""" +from controls.controlsbase import ControlsBase +import pygame +import gamepad + +class GamePadControls(ControlsBase): + def __init__(self): + gamapadType = gamepad.ps5 + + def left(self): + pass + + def right(self): + pass + + def up(self): + pass + + def down(self): + pass + + def pause(self): + pass \ No newline at end of file diff --git a/controls/joystick.py b/controls/joystick.py new file mode 100644 index 0000000..e7268f6 --- /dev/null +++ b/controls/joystick.py @@ -0,0 +1,44 @@ +""" +Class for grabbing joystick signals +""" +from controls.controlsbase import ControlsBase +import pygame +import pyjoystick +from pyjoystick.pygame import Key, Joystick, run_event_loop + +class JoystickControls(ControlsBase): + def __init__(self): + mngr = pyjoystick.ThreadEventManager(event_loop=run_event_loop, + handle_key_event=self.handle_key_event) + mngr.start() + + def handle_key_event(self, key): + if key.keytype != Key.HAT: + if key.value != pygame.K_P: + return + + if key.value == Key.HAT_UP: + self.up() + elif key.value == Key.HAT_DOWN: + self.down() + if key.value == Key.HAT_LEFT: + self.left() + elif key.value == Key.HAT_RIGHT: + self.right() + elif key.value == Key.KEY_P: + self.pause() + + def left(self): + print("Joystick moved left") + + def right(self): + print("Joystick moved right") + + def up(self): + print("Joystick moved up") + + def down(self): + print("Joystick moved down") + + def pause(self): + print("Paused") \ No newline at end of file diff --git a/controls/keyboard.py b/controls/keyboard.py new file mode 100644 index 0000000..7e03b85 --- /dev/null +++ b/controls/keyboard.py @@ -0,0 +1,23 @@ +""" +Class for grabbing key presses +""" +from controls.controlsbase import ControlsBase + +class KeyboardControls(ControlsBase): + def __init__(self): + pass + + def left(self): + pass + + def right(self): + pass + + def up(self): + pass + + def down(self): + pass + + def pause(self): + pass \ No newline at end of file diff --git a/main.py b/main.py index d7718d1..074cd22 100644 --- a/main.py +++ b/main.py @@ -1,5 +1,8 @@ +import pygame +import controls from tilemap.playground import PlayGround if __name__ == "__main__": pg = PlayGround(geometry = (1, 3)) + pygame.init() pass \ No newline at end of file diff --git a/startscreen/startscreen.py b/startscreen/startscreen.py new file mode 100644 index 0000000..fa7ac6a --- /dev/null +++ b/startscreen/startscreen.py @@ -0,0 +1,5 @@ +import pygame + +class StartScreen: + def __init__(self): + pass \ No newline at end of file