aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xsrc/pico/.Trash-10000
-rwxr-xr-xsrc/pico/.fseventsd/no_log0
-rwxr-xr-xsrc/pico/.metadata_never_index0
-rw-r--r--src/splitscreen_duo/games/breakout.py12
-rw-r--r--src/splitscreen_duo/games/pong.py10
-rw-r--r--src/splitscreen_duo/games/snake.py23
-rw-r--r--src/splitscreen_duo/input.py91
-rw-r--r--src/splitscreen_duo/menu.py2
8 files changed, 65 insertions, 73 deletions
diff --git a/src/pico/.Trash-1000 b/src/pico/.Trash-1000
new file mode 100755
index 0000000..e69de29
--- /dev/null
+++ b/src/pico/.Trash-1000
diff --git a/src/pico/.fseventsd/no_log b/src/pico/.fseventsd/no_log
new file mode 100755
index 0000000..e69de29
--- /dev/null
+++ b/src/pico/.fseventsd/no_log
diff --git a/src/pico/.metadata_never_index b/src/pico/.metadata_never_index
new file mode 100755
index 0000000..e69de29
--- /dev/null
+++ b/src/pico/.metadata_never_index
diff --git a/src/splitscreen_duo/games/breakout.py b/src/splitscreen_duo/games/breakout.py
index ef369de..3f6aa16 100644
--- a/src/splitscreen_duo/games/breakout.py
+++ b/src/splitscreen_duo/games/breakout.py
@@ -125,13 +125,13 @@ class Breakout(GameBase):
if result is not None:
return result
- elif event.type == pygame.KEYDOWN:
- if action == "LEFT":
- self.move_paddle(-20)
- elif action == "RIGHT":
- self.move_paddle(20)
- self.paddle[0] = pygame.mouse.get_pos()[0] - PADDLE_WIDTH // 2
+ if action == "LEFT":
+ self.move_paddle(-20)
+ elif action == "RIGHT":
+ self.move_paddle(20)
+
+ # self.paddle[0] = pygame.mouse.get_pos()[0] - PADDLE_WIDTH // 2
self.move_paddle(0)
self.check_serial()
diff --git a/src/splitscreen_duo/games/pong.py b/src/splitscreen_duo/games/pong.py
index 464e529..0c0df84 100644
--- a/src/splitscreen_duo/games/pong.py
+++ b/src/splitscreen_duo/games/pong.py
@@ -169,11 +169,11 @@ class Pong(GameBase):
if result is not None:
return result
- elif event.type == pygame.KEYDOWN:
- if action == "LEFT":
- self.move_player_paddle(-PADDLE_SPEED)
- elif action == "RIGHT":
- self.move_player_paddle(PADDLE_SPEED)
+
+ if action == "LEFT":
+ self.move_player_paddle(-PADDLE_SPEED)
+ elif action == "RIGHT":
+ self.move_player_paddle(PADDLE_SPEED)
keys = pygame.key.get_pressed()
diff --git a/src/splitscreen_duo/games/snake.py b/src/splitscreen_duo/games/snake.py
index b593ea1..83ee987 100644
--- a/src/splitscreen_duo/games/snake.py
+++ b/src/splitscreen_duo/games/snake.py
@@ -107,15 +107,15 @@ class Snake(GameBase):
if result is not None:
return result
- elif event.type == pygame.KEYDOWN:
- if action == "UP":
- self.change_to = "UP"
- elif action == "DOWN":
- self.change_to = "DOWN"
- elif action == "LEFT":
- self.change_to = "LEFT"
- elif action == "RIGHT":
- self.change_to = "RIGHT"
+
+ if action == "UP":
+ self.change_to = "UP"
+ elif action == "DOWN":
+ self.change_to = "DOWN"
+ elif action == "LEFT":
+ self.change_to = "LEFT"
+ elif action == "RIGHT":
+ self.change_to = "RIGHT"
self.check_serial()
self.move()
@@ -125,11 +125,6 @@ class Snake(GameBase):
continue
- if self.check_collision():
- self.end_game(self.score)
-
- continue
-
self.screen.fill(BLACK)
for segment in self.body:
diff --git a/src/splitscreen_duo/input.py b/src/splitscreen_duo/input.py
index b42cd7d..a947315 100644
--- a/src/splitscreen_duo/input.py
+++ b/src/splitscreen_duo/input.py
@@ -2,27 +2,31 @@ import pygame
class Input:
- def __init__(self, debug=True):
- self.debug = debug
+ def __init__(self, debug=False):
+ pygame.joystick.init()
+
self.controller = None
- self.prev_button_states = {}
- self.prev_axis_states = {"x": 0, "y": 0}
- self.prev_hat_states = (0, 0)
- if not self.debug:
- pygame.joystick.init()
+ if pygame.joystick.get_count() > 0:
+ self.controller = pygame.joystick.Joystick(0)
- if pygame.joystick.get_count() > 0:
- self.controller = pygame.joystick.Joystick(0)
- self.controller.init()
+ self.controller.init()
+
+ self.button_states = {} # Current states
+ self.prev_button_states = {} # Previous states
+ self.prev_axis_states = {"x": 0, "y": 0}
+ self.prev_hat_states = (0, 0) # D-pad
def get_input(self, event):
- if self.debug:
- return self.get_keyboard_input(event)
- else:
- return self.get_controller_input(event)
+ if self.controller:
+ controller_input = self._get_controller_input(event)
+
+ if controller_input:
+ return controller_input
- def get_keyboard_input(self, event):
+ return self._get_keyboard_input(event)
+
+ def _get_keyboard_input(self, event):
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
return "UP"
@@ -44,60 +48,51 @@ class Input:
return None
- def get_controller_input(self, event):
- if not self.controller:
- return None
-
+ def _get_controller_input(self, event):
+ # Analog stick
if event.type == pygame.JOYAXISMOTION:
- if event.axis == 0: # X-axis (left/right)
- prev_x = self.prev_axis_states["x"]
+ if event.axis == 0: # X-axis
curr_x = event.value
- self.prev_axis_states["x"] = curr_x
-
- if prev_x > -0.5 and curr_x < -0.5: # Transition to left
- return "LEFT"
- if prev_x < 0.5 and curr_x > 0.5: # Transition to right
+ if curr_x <= -0.1:
return "RIGHT"
+ if curr_x >= 0.1:
+ return "LEFT"
- elif event.axis == 1: # Y-axis (up/down)
- prev_y = self.prev_axis_states["y"]
+ elif event.axis == 1: # Y-axis
curr_y = event.value
- self.prev_axis_states["y"] = curr_y
- if prev_y > -0.5 and curr_y < -0.5: # Transition to up
+ if curr_y <= -0.1:
return "UP"
-
- if prev_y < 0.5 and curr_y > 0.5: # Transition to down
+ if curr_y >= 0.1:
return "DOWN"
-
+ # D-pad
elif event.type == pygame.JOYHATMOTION:
- # D-pad
prev_hat = self.prev_hat_states
curr_hat = event.value
self.prev_hat_states = curr_hat
- if prev_hat[0] == 0 and curr_hat[0] == -1: # Transition to left
+ if prev_hat[0] == 0 and curr_hat[0] == -1:
return "LEFT"
- if prev_hat[0] == 0 and curr_hat[0] == 1: # Transition to right
+ if prev_hat[0] == 0 and curr_hat[0] == 1:
return "RIGHT"
- if prev_hat[1] == 0 and curr_hat[1] == 1: # Transition to up
+ if prev_hat[1] == 0 and curr_hat[1] == 1:
return "UP"
- if prev_hat[1] == 0 and curr_hat[1] == -1: # Transition to down
+ if prev_hat[1] == 0 and curr_hat[1] == -1:
return "DOWN"
-
+ # Buttons
elif event.type == pygame.JOYBUTTONDOWN:
button = event.button
+ self.button_states[button] = True
- if button not in self.prev_button_states:
- self.prev_button_states[button] = False
-
- if not self.prev_button_states[button]:
- self.prev_button_states[button] = True
-
+ # Debounced
+ if (
+ button not in self.prev_button_states
+ or not self.prev_button_states[button]
+ ):
if button == 0:
return "DOWN"
@@ -116,7 +111,9 @@ class Input:
if button == 5:
return "SELECT"
elif event.type == pygame.JOYBUTTONUP:
- if event.button in self.prev_button_states:
- self.prev_button_states[event.button] = False
+ button = event.button
+ self.button_states[button] = False
+
+ self.prev_button_states = self.button_states.copy()
return None
diff --git a/src/splitscreen_duo/menu.py b/src/splitscreen_duo/menu.py
index eab3eec..18dcca6 100644
--- a/src/splitscreen_duo/menu.py
+++ b/src/splitscreen_duo/menu.py
@@ -25,7 +25,7 @@ def init_display():
FONT = pygame.font.Font(None, 36)
screen = pygame.display.set_mode(
(WIDTH, HEIGHT) if IS_DEVELOPMENT_MODE else (0, 0),
- # pygame.RESIZABLE if IS_DEVELOPMENT_MODE else pygame.FULLSCREEN,
+ pygame.RESIZABLE if IS_DEVELOPMENT_MODE else pygame.FULLSCREEN,
)
selected_index = 0