我正在尝试建立我自己的迷你库,以简化操纵杆在pygame
中的使用。但是,当我试图读取按钮和轴时,它们都返回0,但只返回assign
函数(我使用的是罗技攻击3)。
import sys
import pygame
joysticks = []
pygame.joystick.init()
pygame.init()
# Variables for locations in list #
AXIS_PITCH = 1
AXIS_ROLL = 0
AXIS_THROTTLE = 2
BUTTON_TRIGGER = BUTTON_1 = 3
BUTTON_2 = 4
BUTTON_3 = 5
BUTTON_4 = 6
BUTTON_5 = 7
# Must be called first, initializes joysticks #
def init():
for joy in range(pygame.joystick.get_count()):
joysticks.append(pygame.joystick.Joystick(joy))
joysticks[joy].init()
# Needs player count, surface to draw to, font to use, colour of text, dimesions of screen #
def assign(players, screen, font, colour, dimensions):
# unassigned = joysticks.copy()
toReturn = []
for i in range(players):
assigned = False
while(not assigned):
if(pygame.key.get_pressed()[pygame.K_ESCAPE]): pygame.quit(); sys.exit();
screen.fill([0,0,0])
outString = "Player "+str(i+1)+" pull the trigger"
out = font.render(outString, 1, colour, None)
screen.blit(out, (dimensions[0]/2-out.get_width()/2, dimensions[1]/2-out.get_height()/2))
pygame.display.flip()
for stick in joysticks:
print(stick.get_axis(1))
if(stick.get_button(0) == 1):
print("Triggered")
if(stick not in toReturn):
toReturn.append(stick)
assigned = True
return(toReturn)
# Returns the values of three axis, and 5 buttons on the passed joystick
def check(joystick):
toReturn = []
toReturn.append(joystick.get_axis(0))
toReturn.append(joystick.get_axis(1))
toReturn.append(joystick.get_axis(2))
toReturn.append(joystick.get_button(0))
toReturn.append(joystick.get_button(1))
toReturn.append(joystick.get_button(2))
toReturn.append(joystick.get_button(3))
toReturn.append(joystick.get_button(4))
return(toReturn)
如果你需要更多的代码,就问一问。提前感谢!
发布于 2016-12-01 06:00:52
如果不处理事件,像get_pressed()
、get_button()
、get_axis()
这样的函数可能无法工作--当您不使用pygame.event.get()
或其他调用pygame.event.pump()
从系统获取信息的事件函数时。
定期使用pygame.event.get()
(或其他事件函数)(即。在您的while
循环中)从系统获取当前信息。
https://stackoverflow.com/questions/40891060
复制相似问题