【问题标题】:USB controller will not work in PygameUSB 控制器在 Pygame 中不起作用
【发布时间】:2012-11-24 22:33:38
【问题描述】:

我有一个 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()

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

【问题讨论】:

  • 您的程序可以使用常规键盘按键吗?
  • 您可以尝试在主循环中添加pygame.event.pump() 调用

标签: python controller usb pygame joystick


【解决方案1】:

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

【讨论】:

  • 我一直在尝试使用操纵杆模块,但它不起作用。我尝试按照 pygame 网站上的教程进行操作,但其中的代码不起作用。这是我正在查看的代码的链接:pygame.org/wiki/Joystick_analyzer 不幸的是,它给了我语法错误。我尝试在我的代码中实现其中没有充满语法的部分,但它仍然无法正常工作。我将编辑我的原始帖子以包含此新代码。
  • 好的,这将是交互式帮助,如果您选择接受它:1) 插入您的 USB 游戏杆 2) 启动 python 解释器(只需在 linux/Mac 终端输入python,或在 Windows CMD 中执行相同操作)3) 输入 import pygame 4) 输入 pygame.joystick.init() 5) 输入 Joystick.get_numaxes 这应该告诉我们轴被分配了哪些“数字” 6) 对角握住操纵杆并输入 Joystick.get_axis(axis_number)对于每个轴,同时持有它如果每个轴都返回 0,那就不好了,我们必须解决这个问题。否则...
  • 好吧,我开始尝试这样做,并且出现了与以前相同的问题。它抱怨名称 Joystick 没有被定义。
  • 嗯。你是如何安装 Pygame 的?尝试使用包管理器安装它。听起来很遗憾,您没有安装所有 Pygame,因为 Joystick 是包含的模块之一。
【解决方案2】:

试试这个:

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

【讨论】:

    猜你喜欢
    • 2015-05-26
    • 1970-01-01
    • 2021-04-30
    • 1970-01-01
    • 1970-01-01
    • 2016-03-11
    • 2013-04-30
    • 2019-06-28
    • 1970-01-01
    相关资源
    最近更新 更多