2

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

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

4

2 回答 2

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 ()
于 2013-02-14T09:14:00.650 回答
1

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

于 2012-11-25T02:42:00.857 回答