Working on gamepad controls. /JL #2
1
Gamepad
Submodule
1
Gamepad
Submodule
Submodule Gamepad added at 0166286230
3
controls/__init__.py
Normal file
3
controls/__init__.py
Normal file
@@ -0,0 +1,3 @@
|
||||
from . import gamepad
|
||||
from . import joystick
|
||||
from . import keyboard
|
21
controls/controlsbase.py
Normal file
21
controls/controlsbase.py
Normal file
@@ -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
|
25
controls/gamepad.py
Normal file
25
controls/gamepad.py
Normal file
@@ -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
|
44
controls/joystick.py
Normal file
44
controls/joystick.py
Normal file
@@ -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")
|
23
controls/keyboard.py
Normal file
23
controls/keyboard.py
Normal file
@@ -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
|
3
main.py
3
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
|
5
startscreen/startscreen.py
Normal file
5
startscreen/startscreen.py
Normal file
@@ -0,0 +1,5 @@
|
||||
import pygame
|
||||
|
||||
class StartScreen:
|
||||
def __init__(self):
|
||||
pass
|
Reference in New Issue
Block a user