[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[pygame] more on joysticks



hi again

The game pad got me confused so actually i was getting buttomup and down events plus axes movements, despite of what i said in previous emails. What I dont get is ball motion and hat motion. I am not sure why i dont get this, neither of the pads i am using seems to have any effect when i press the left hand controllers.

I put together the code below to test the basic functionality if the joysticks, added some comments from peter, maybe someone finds it useful. But i am not sure it is correct.

After trying with couple of different pads I realised that actually they are all slightly different, so the buttom 1 and 2 are actually different in both pads i am using. I was wondering how does people usually go about this. If i was doing a space invaders for example each pad would move the spaceship into a different direction pressing the 'same' buttom (actually not the same, but on the same position in the pad). It looks like a setup screen if needed so that the user can test if the game or application if setup to run properly to his/her pad.

below the code of the testing example.


######### # joystick example

#JOYAXISMOTION    joy, axis, value
#JOYBALLMOTION    joy, ball, rel
#JOYHATMOTION    joy, hat, value
#JOYBUTTONUP    joy, button
#JOYBUTTONDOWN    joy, button

# from Peter Shinners :
# A basic joystick generally has two axis and a few buttons. Axis 0 is
# always left/right and axis 1 is up/down. There are also advanced flight
# simulator joysticks which have a lot more gadgets on them. First, the
# big joysticks will have more axis on them. Things like twist, throttle
# control, and even foot rudders will be counted as additional axis. The
# "hats" are miniature joysticks on the joystick, They are usually
# controlled by the thumb. Lastly, there are also "balls" on the joystick
# which are like little trackballs on the joystick. These work sort of
# like mice, but controlled with one finger. Also note that little
# gamepads will still use the first two axis for the direction pad, even
# though it's not really an analog axis. This is good since it keeps the
# analog joysticks and gamepads somewhat similar for software.

import pygame
from pygame import locals


jlist = [] # global joystick list


def init(): pygame.init()

    pygame.joystick.init() # init main joystick device system

    try:
        for n in range(pygame.joystick.get_count()): #
            stick = pygame.joystick.Joystick(n)
            jlist.append(stick) # create a joystick instance
            stick.init() # init instance
            print 'Enabled joystick: ' + stick.get_name()
    except pygame.error:
        print 'pygame.error, no joystick found.'


def main():

    init() # init joystick system

    while 1: # endless loop

        for e in pygame.event.get(): # iterate over event stack

            if e.type == pygame.locals.JOYAXISMOTION: # 7
                for j in jlist:
                    for n in range(j.get_numaxes()):
                        print 'axe num : ' + str(j.get_axis(n))

            elif e.type == pygame.locals.JOYBALLMOTION: # 8
                for j in jlist:
                    for n in range(j.get_numballs()):
                        if j.get_button(n) :
                            print 'ball num : '+ str(n)

            elif e.type == pygame.locals.JOYHATMOTION: # 9
                for j in jlist:
                    for n in range(j.get_numhats()):
                        if j.get_button(n) :
                            print 'hat num : '+ str(n)

            elif e.type == pygame.locals.JOYBUTTONDOWN: # 10
                for j in jlist:
                    for n in range(j.get_numbuttons()):
                        if j.get_button(n) :
                            print 'button num : '+ str(n)

            elif e.type == pygame.locals.JOYBUTTONUP: # 11
                for j in jlist:
                    for n in range(j.get_numbuttons()):
                        if j.get_button(n) :
                            print 'button num : '+ str(n)



if __name__ == '__main__': main()

#####

--
altern