aboutsummaryrefslogtreecommitdiff
path: root/ddrinput.py
blob: a1bf4827e7f790e07735907296b7279b15ac9c11 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import pygame
JOY_EVENT = 7
KEY_EVENT = 2
X = 0
Y = 1
(LEFT, RIGHT, UP, DOWN) = range(4) 
KEY_LEFT = 276
KEY_UP = 273
KEY_DOWN = 274
KEY_RIGHT = 275
KEY_A = 97
KEY_S = 115
KEY_D = 100
KEY_W = 119
KEY_SPACE = 32
KEY_ESC = 27

DIRECTIONS = {0:'LEFT', 1:'RIGHT',  2:'UP', 3:'DOWN'}
class DdrInput(object):
  """
  DdrInput is a class to get input from the particular DDR pads and adapters we have.  It is not
  general or cross platform.  It uses pygame.  For something more general, use pad.py in the pydance
  library.  The pydance library doesn't work with our adapter, so we had to write our own code.  A
  few lines of code here are borrowed from the pydance library.


  DEBUG MODE:
    Use the arrow keys.  Hold down a modifier (alt, control, etc.) to get player 2
  """
  def __init__(self, debug_mode=True):
    """
    Debug mode prints inputs from the ddr pads and also enables the keyboard as an input
    """
    pygame.init() #safe to call multiple times
    self.init_joysticks()
    #This is just so that we can get key presses in the demo.  remove when we plug it into a ui
    screen = pygame.display.set_mode((640, 480))
    self.debug_mode = debug_mode

  def init_joysticks(self):
    pygame.joystick.init()
    try: totaljoy = pygame.joystick.get_count()
    except: totaljoy = 0
    print totaljoy, 'joysticks loaded'
    for i in range(totaljoy):
      m = pygame.joystick.Joystick(i)
      m.init()
  
  def poll(self):
    """
    Returns a tuple of player index (0 or 1) and move, 
    LEFT, RIGHT, UP, DOWN.  Returns None if there is no new input.  Only returns 1 input at a time.
    """
    event = pygame.event.poll()
    player_move = None
    if event.type == JOY_EVENT:
      player_index = event.joy
      #there may be a tricky quick way to code this, but this is more readable
      #value == 0 -> released
      if event.axis == X:
        if event.value < 0:
          player_move = LEFT
        elif event.value > 0:
          player_move = RIGHT
      else:
        if event.value > 0:
          player_move = DOWN
        elif event.value < 0:
          player_move = UP
      if self.debug_mode and player_move != None:
        print (player_index, player_move)
      if player_move != None:
        return (player_index, player_move)
      else:
        return None
    if self.debug_mode:
      if event.type == KEY_EVENT:
        if event.key == KEY_LEFT:
          player_index = 1
          player_move = LEFT
        elif event.key == KEY_RIGHT:
          player_index = 1
          player_move = RIGHT
        elif event.key == KEY_DOWN:
          player_index = 1
          player_move = DOWN
        elif event.key == KEY_UP:
          player_index = 1
          player_move = UP
        elif event.key == KEY_A:
          player_index = 0
          player_move = LEFT
        elif event.key == KEY_D:
          player_index = 0
          player_move = RIGHT
        elif event.key == KEY_S:
          player_index = 0
          player_move = DOWN
        elif event.key == KEY_W:
          player_index = 0
          player_move = UP
        elif event.key == KEY_ESC:
          player_index = 2
          player_move = "DIE"
        elif event.key == KEY_SPACE:
          player_index = 1
          player_move = "DROP"
       
        if player_move != None:
          return (player_index, player_move)
        else:
          return None