Joystick: Difference between revisions

From XPUB & Lens-Based wiki
No edit summary
No edit summary
 
(One intermediate revision by the same user not shown)
Line 1: Line 1:
A simple pygame project to view joystick / gamepad data...
A simple pygame project to view joystick / gamepad data...
see [http://www.pygame.org/docs/ref/joystick.html pygame docs]


<source lang="python">
<source lang="python">
Line 33: Line 34:
<Event(9-JoyHatMotion {'joy': 0, 'hat': 0, 'value': (0, 0)})>
<Event(9-JoyHatMotion {'joy': 0, 'hat': 0, 'value': (0, 0)})>
<Event(7-JoyAxisMotion {'joy': 0, 'value': 0.0, 'axis': 0})>
<Event(7-JoyAxisMotion {'joy': 0, 'value': 0.0, 'axis': 0})>
<Event(7-JoyAxisMotion {'joy': 0, 'value': 0.06183050019837031, 'axis': 0})>
<Event(7-JoyAxisMotion {'joy': 0, 'value': 0.27832880642109442, 'axis': 1})>
<Event(10-JoyButtonDown {'joy': 0, 'button': 4})>
<Event(10-JoyButtonDown {'joy': 0, 'button': 4})>
<Event(10-JoyButtonDown {'joy': 0, 'button': 5})>
<Event(10-JoyButtonDown {'joy': 0, 'button': 5})>
</pre>
</pre>

Latest revision as of 23:34, 27 July 2009

A simple pygame project to view joystick / gamepad data... see pygame docs

import sys, pygame
pygame.init()
pygame.joystick.init()
if pygame.joystick.get_count() > 0:
    print "creating joystick object"
    stick = pygame.joystick.Joystick(0)
#    print stick
#    print dir(stick)
    stick.init()

size = width, height = 320, 240
black = 0, 0, 0
screen = pygame.display.set_mode(size)
running = True

while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT or (event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE):
            running = False
        print event

    screen.fill(black)
    pygame.display.flip()

produces output like:

<Event(9-JoyHatMotion {'joy': 0, 'hat': 0, 'value': (1, 0)})>
<Event(11-JoyButtonUp {'joy': 0, 'button': 0})>
<Event(9-JoyHatMotion {'joy': 0, 'hat': 0, 'value': (0, 0)})>
<Event(7-JoyAxisMotion {'joy': 0, 'value': 0.0, 'axis': 0})>
<Event(7-JoyAxisMotion {'joy': 0, 'value': 0.06183050019837031, 'axis': 0})>
<Event(7-JoyAxisMotion {'joy': 0, 'value': 0.27832880642109442, 'axis': 1})>
<Event(10-JoyButtonDown {'joy': 0, 'button': 4})>
<Event(10-JoyButtonDown {'joy': 0, 'button': 5})>