python - USB Controller 在 Pygame 中不起作用

标签 python controller usb pygame joystick

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

这是我的代码:

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

为了清楚起见,我的代码在使用键盘时确实有效。但是,它不会转换为映射到相同键的 Controller 。

最佳答案

您需要使用 Pygame 的 Joystick module ,或类似的东西,它具有以下方法:Joystick.get_init(),它告诉操纵杆是否在 Pygame 中初始化,以及 Joystick.get_axis(axis_number) 它返回操纵杆沿给定轴 axis_number 的位置(以 float 表示)。 ControlMK 可能将操纵杆映射到过高级别的按键输入,无法与 Pygame 交互,尽管可能有某种方法可以改变这一点,但其文档似乎有限。

关于python - USB Controller 在 Pygame 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13546332/

相关文章:

python - 避免在 django 的每个渲染中传递 RequestContext

python - 防止 Pandas 将日期时间对象转换为 Pandas 时间戳

html - 如何从 AngularJS 函数返回 html?

ruby-on-rails - Rails 中同一路由的 POST 和 GET 请求的不同 Controller 操作

docker - 使用 docker run --privileged 托管单个 USB 设备

Android USB 主机 API : bulk transfer buffer size

python - 使用 map 和 lambda 计算字典中的频率

python - 从 Web 界面控制基于 Twisted 的服务器?

php - 调用未定义的方法 ConsoleTVs\Charts\Builder::new() laravel

c++ - `libusb_attach_kernel_driver` 不工作