added input_handler.py module to help with inputs
This commit is contained in:
parent
da5bacf950
commit
7b8898b77b
0
pocket_friends/game_files/classes/__init__.py
Normal file
0
pocket_friends/game_files/classes/__init__.py
Normal file
71
pocket_friends/game_files/classes/input_handler.py
Normal file
71
pocket_friends/game_files/classes/input_handler.py
Normal file
@ -0,0 +1,71 @@
|
|||||||
|
import pygame
|
||||||
|
import importlib.util
|
||||||
|
from pocket_friends.hardware.gpio_handler import Constants, GPIOHandler
|
||||||
|
|
||||||
|
|
||||||
|
class InputHandler:
|
||||||
|
def __init__(self, pygame_clock):
|
||||||
|
self.clock = pygame_clock
|
||||||
|
try:
|
||||||
|
importlib.util.find_spec('RPi.GPIO')
|
||||||
|
import RPi.GPIO as GPIO
|
||||||
|
self.on_hardware = True
|
||||||
|
except ImportError:
|
||||||
|
import pocket_friends.development.FakeGPIO as GPIO
|
||||||
|
self.on_hardware = False
|
||||||
|
self.last_input_tick = 0
|
||||||
|
|
||||||
|
def create_event(self, pressed_button):
|
||||||
|
"""
|
||||||
|
Creates a pygame event with a given keyboard code
|
||||||
|
:param pressed_button:
|
||||||
|
"""
|
||||||
|
# Register a button click so long as the last button click happened no less than two frames ago
|
||||||
|
if pygame.time.get_ticks() - self.last_input_tick > self.clock.get_time() * 2 or not self.on_hardware:
|
||||||
|
pygame.event.post(pygame.event.Event(pygame.KEYDOWN, {'key': pressed_button}))
|
||||||
|
pygame.event.post(pygame.event.Event(pygame.KEYUP, {'key': pressed_button}))
|
||||||
|
self.last_input_tick = pygame.time.get_ticks()
|
||||||
|
|
||||||
|
def handle_gpio(self):
|
||||||
|
"""
|
||||||
|
Handles getting GPIO button presses and making a pygame event when a press is detected.
|
||||||
|
"""
|
||||||
|
for pressed_button in Constants.buttons:
|
||||||
|
code = Constants.buttons.get(pressed_button)
|
||||||
|
|
||||||
|
# Check if a button has been pressed. If it has, create a pygame event for it.
|
||||||
|
if GPIOHandler.get_press(code):
|
||||||
|
self.create_event(code)
|
||||||
|
|
||||||
|
def keyboard_handler(self):
|
||||||
|
"""
|
||||||
|
Simulates key presses to GPIO button presses. Also handles quitting the game.
|
||||||
|
"""
|
||||||
|
|
||||||
|
# Checks if a corresponding keyboard key has been pressed. If it has, emulate a button press.
|
||||||
|
for keyboard_event in pygame.event.get():
|
||||||
|
if keyboard_event.type == pygame.QUIT:
|
||||||
|
running = False
|
||||||
|
if keyboard_event.type == pygame.KEYDOWN:
|
||||||
|
if keyboard_event.key == pygame.K_a:
|
||||||
|
self.create_event(Constants.buttons.get('a'))
|
||||||
|
if keyboard_event.key == pygame.K_b:
|
||||||
|
self.create_event(Constants.buttons.get('b'))
|
||||||
|
if keyboard_event.key == pygame.K_PERIOD:
|
||||||
|
self.create_event(Constants.buttons.get('j_i'))
|
||||||
|
if keyboard_event.key == pygame.K_RIGHT:
|
||||||
|
self.create_event(Constants.buttons.get('j_r'))
|
||||||
|
if keyboard_event.key == pygame.K_LEFT:
|
||||||
|
self.create_event(Constants.buttons.get('j_l'))
|
||||||
|
if keyboard_event.key == pygame.K_DOWN:
|
||||||
|
self.create_event(Constants.buttons.get('j_d'))
|
||||||
|
if keyboard_event.key == pygame.K_UP:
|
||||||
|
self.create_event(Constants.buttons.get('j_u'))
|
||||||
|
if keyboard_event.key == pygame.K_ESCAPE:
|
||||||
|
running = False
|
||||||
|
|
||||||
|
def update(self):
|
||||||
|
if self.on_hardware:
|
||||||
|
self.handle_gpio()
|
||||||
|
else:
|
||||||
|
self.keyboard_handler()
|
@ -68,4 +68,4 @@ class GPIOHandler:
|
|||||||
:param button: button to be detected
|
:param button: button to be detected
|
||||||
:return: True if the button is has been pressed, False otherwise
|
:return: True if the button is has been pressed, False otherwise
|
||||||
"""
|
"""
|
||||||
return GPIO.event_detected(button)
|
return GPIO.event_detected(button)
|
Loading…
Reference in New Issue
Block a user