summaryrefslogtreecommitdiff
path: root/cameltris.py
diff options
context:
space:
mode:
authorOlivier Gayot <olivier.gayot@sigexec.com>2020-12-22 23:29:23 +0100
committerOlivier Gayot <olivier.gayot@sigexec.com>2020-12-22 23:33:47 +0100
commit392e231011805b710ffb7517e3d41d1446e23df5 (patch)
tree0e87b6ff2cbb2f457bcc13c79a08cd81acf0bc4d /cameltris.py
parent9fc44219df19a62b27e87eaa35e9687b677c6f65 (diff)
Implement use of joystick
Signed-off-by: Olivier Gayot <olivier.gayot@sigexec.com>
Diffstat (limited to 'cameltris.py')
-rwxr-xr-xcameltris.py43
1 files changed, 41 insertions, 2 deletions
diff --git a/cameltris.py b/cameltris.py
index 5d1d9ba..58de00e 100755
--- a/cameltris.py
+++ b/cameltris.py
@@ -1,12 +1,22 @@
#!/usr/bin/env python3
import contextlib
+import enum
import random
import sys
import time
import pygame
+class PS3Controller(enum.Enum):
+ CROSS = 0
+ CIRCLE = 1
+ TRIANGLE = 2
+ SQUARE = 3
+ DOWN = 14
+ LEFT = 15
+ RIGHT = 16
+
class WouldCollide(Exception):
pass
@@ -354,6 +364,11 @@ das = 0
pressing_down_countdown = None
+joysticks = [pygame.joystick.Joystick(x) for x in range(pygame.joystick.get_count())]
+for joystick in joysticks:
+ joystick.init()
+joystick = joysticks[0]
+
while True:
piece_drop_frames += 1
@@ -385,13 +400,37 @@ while True:
if event.key in (pygame.K_DOWN, pygame.K_j):
pressing_down_countdown = None
+ elif event.type == pygame.JOYBUTTONDOWN:
+ with contextlib.suppress(WouldCollide):
+ if event.button == PS3Controller.RIGHT.value:
+ move_piece_right()
+ das = 0
+ if event.button == PS3Controller.LEFT.value:
+ move_piece_left()
+ das = 0
+ if event.button == PS3Controller.CROSS.value:
+ rotate_piece_clockwise()
+ if event.button == PS3Controller.CIRCLE.value:
+ rotate_piece_counter_clockwise()
+ if event.button == PS3Controller.DOWN.value:
+ piece_drop_frames = 0
+ pressing_down_countdown = 3
+ try:
+ move_piece_down()
+ except WouldCollide:
+ stick_piece()
+ elif event.type == pygame.JOYBUTTONUP:
+ if event.button == PS3Controller.DOWN.value:
+ pressing_down_countdown = None
+
das += 1
if das == 16:
pressed_keys = pygame.key.get_pressed()
+ pressed_buttons = [joystick.get_button(x) for x in range(joystick.get_numbuttons())]
with contextlib.suppress(WouldCollide):
- if pressed_keys[pygame.K_RIGHT] or pressed_keys[pygame.K_l]:
+ if pressed_keys[pygame.K_RIGHT] or pressed_keys[pygame.K_l] or pressed_buttons[PS3Controller.RIGHT.value]:
move_piece_right()
- if pressed_keys[pygame.K_LEFT] or pressed_keys[pygame.K_h]:
+ if pressed_keys[pygame.K_LEFT] or pressed_keys[pygame.K_h] or pressed_buttons[PS3Controller.LEFT.value]:
move_piece_left()
das = 10