2

I have a USB controller that I have mapped to be the arrow keys on the keyboard, outside of gaming programs (as in, you can use it as the normal arrow keys on the keyboard). I did this using the program ControlMK. My Pygame program will not recognize the controller as the keyboard. When I try to use the Joystick modules, the program does not function properly.

Here is my code:

import pygame, sys, time, random
from pygame.locals import *

# set up pygame
try:
    pygame.init()
    pygame.display.init()
    pygame.mixer.init(size=8, buffer=2048)

    pygame.mixer.get_init

except:
    print "error in init\n"

white=(255,255,255)

screen=pygame.display.set_mode((800,600), pygame.RESIZABLE)

class Loadsound:

    def __init__(self, name, key, sound_file):
        self.name = name
        self.name=key
        self.sound = pygame.mixer.Sound(sound_file)

sound_left=[]
sound_left.append(Loadsound("left","K_LEFT", "left.wav"))
sound_right=[]
sound_right.append(Loadsound("right","K_RIGHT", "right.wav"))
sound_up=[]
sound_up.append(Loadsound("up","K_UP","up.wav")) 
sound_down=[]
sound_down.append(Loadsound("down","K_DOWN","down.wav"))


while True:
  for i in pygame.event.get():
   if i.type==QUIT:
    exit()
  pressed=pygame.key.get_pressed()


  if pressed[K_LEFT]:
      for left in sound_left:
          left.sound.play()
  elif pressed[K_RIGHT]:
      for right in sound_right:
          right.sound.play()
  elif pressed[K_UP]:
      for up in sound_up:
          up.sound.play()
  elif pressed[K_DOWN]:
      for down in sound_down:
          down.sound.play()

Thank you for the help!

Edit:

I have been trying to use the Joystick module. Here is where I was taking example code from:

http://www.pygame.org/wiki/Joystick_analyzer

I edited my code to include some of it, but it still does not work. Here is my edited code:

import pygame, sys, time, random
from pygame.locals import *
from pygame import *

# set up pygame
try:
    pygame.init()
    pygame.display.init()
    pygame.mixer.init(size=8, buffer=2048)

    pygame.mixer.get_init

except:
    print "error in init\n"

white=(255,255,255)

screen=pygame.display.set_mode((800,600), pygame.RESIZABLE)

class Loadsound:

    def __init__(self, name, key, sound_file):
        self.name = name
        self.name=key
        self.sound = pygame.mixer.Sound(sound_file)

sound_left=[]
sound_left.append(Loadsound("left","K_LEFT", "left.wav"))
sound_right=[]
sound_right.append(Loadsound("right","K_RIGHT", "right.wav"))
sound_up=[]
sound_up.append(Loadsound("up","K_UP","up.wav")) 
sound_down=[]
sound_down.append(Loadsound("down","K_DOWN","down.wav"))

def __init__(self):
    pygame.joystick.init()
    self.my_joystick = None
    self.joystick_names = []

    for i in range(0, pygame.joystick.get_count()):
        self.joystick_names.append(pygame.joystick.Joystick(i).get_name())

    print self.joystick_names

    if (len(self.joystick_names) > 0):
        self.my_joystick = pygame.joystick.Joystick(0)
        self.my_joystick.init()

    #max_joy = max(self.my_joystick.get_numaxes(), self.my_joystick.get_numbuttons(), self.my_joystick.get_numhats()

while True:
    for i in pygame.event.get():
        pygame.event.pump()
    if i.type==QUIT:
        exit()
    #pygame.joystick.Joystick(0)
    #Joystick.init()
    pressed=pygame.key.get_pressed()
    #pressed_j=Joystick.get_hat()
    def check_hat(self, p_hat):
        if (self.my_joystick):
            if (p_hat,self.my_joystick.get_numhats()):
                return self.my_joystick.get_hat(p_hat)

        return (0, 0)



    if check_hat==(-1,0):
    #if pressed[K_LEFT]:
        for left in sound_left:
            left.sound.play()
    elif pressed[K_RIGHT]:
        for right in sound_right:
            right.sound.play()
    elif pressed[K_UP]:
        for up in sound_up:
            up.sound.play()
    elif pressed[K_DOWN]:
        for down in sound_down:
            down.sound.play()

For clarity, my code DOES work when using the keyboard. However, it does not translate to the controller which is mapped to the same keys.

user1848006
  • 21
  • 1
  • 3

2 Answers2

1

You'll need to use Pygame's Joystick module, or something similar, which has method's such as: Joystick.get_init(), which tells if the joystick is initialized in Pygame, and Joystick.get_axis(axis_number) which returns the Joystick's position, as a float, along a given axis axis_number. ControlMK is likely mapping the joystick to key inputs at too high of a level to interact with Pygame, though there may be some way to change that, its documentation seems limited.

rofls
  • 4,993
  • 3
  • 27
  • 37
  • I have been trying to use the Joystick module, but it will not work. I tried following a tutorial on the pygame website about it, but the code in it doesn't work. Here is the link to the code I was looking at: http://www.pygame.org/wiki/Joystick_analyzer Unfortunately, it gives me syntax errors. I tried implementing parts of it that weren't riddled with syntax in my code, but it wouldn't work, still. I will edit my original post to include this new code. – user1848006 Nov 25 '12 at 03:54
  • ok, this is going to be interactive help, should you choose to accept it: 1) plug in your USB joystick 2) start python interpreter (simply type `python` in the linux/Mac terminal, or do the same in the windows CMD) 3) type `import pygame` 4) type `pygame.joystick.init()` 5) type `Joystick.get_numaxes` which should tell us which "numbers" the axes are assigned 6) hold the joystick diagonally and type `Joystick.get_axis(axis_number)` for each axes, while holding it If it returns 0 for each, that's bad, and we'll have to troubleshoot that. Otherwise... – rofls Nov 25 '12 at 04:12
  • Well, I started trying to do that, and the same problem arose as before. It complains about the name Joystick not being defined. – user1848006 Nov 25 '12 at 05:01
  • Hmmm. How did you install Pygame? Try installing it with the package manager. It sounds like unfortunately you didn't install all of Pygame, as Joystick is one of the contained modules. – rofls Nov 25 '12 at 05:36
1

Try this:

import pygame

pygame.init()
print "Joystics: ", pygame.joystick.get_count()
my_joystick = pygame.joystick.Joystick(0)
my_joystick.init()
clock = pygame.time.Clock()

while 1:
    for event in pygame.event.get():
        print my_joystick.get_axis(0),  my_joystick.get_axis(1) 
        clock.tick(40)

pygame.quit ()
ZloAlien
  • 19
  • 1