USB控制器无法在Pygame中运行

时间:2012-11-24 22:33:38

标签: python controller usb pygame joystick

我有一个USB控制器,我已将其映射为键盘上的箭头键,在游戏程序之外(例如,您可以将其用作键盘上的普通箭头键)。我是使用程序ControlMK完成的。我的Pygame程序不会将控制器识别为键盘。当我尝试使用操纵杆模块时,程序无法正常工作。

这是我的代码:

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()

感谢您的帮助!

编辑:

我一直在尝试使用操纵杆模块。这是我从以下代码中获取示例代码的地方:

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

我编辑了我的代码以包含其中的一部分,但它仍然不起作用。这是我编辑的代码:

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()

为清楚起见,我的代码在使用键盘时可以正常工作。但是,它不会转换为映射到相同键的控制器。

2 个答案:

答案 0 :(得分:1)

你需要使用Pygame的Joystick module,或类似的东西,它有方法,如:Joystick.get_init(),它告诉操纵杆是否在Pygame中初始化,Joystick.get_axis(axis_number)返回操纵杆在给定轴axis_number上的位置,如浮点数。 ControlMK很可能将操纵杆映射到太高级别的键输入以与Pygame交互,尽管可能有某种方法可以改变它,但它的文档似乎有限。

答案 1 :(得分:1)

试试这个:

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 ()