1
pygame
でジョイスティックの使用を簡素化するために私自身のミニライブラリを構築しようとしています。しかし、私はボタンや軸を読み込もうとしたとき、彼らはすべての0を返しますが、あなただけ聞いて、任意のより多くのコードが必要な場合にのみassign
機能にPygame.joystick returned 0s
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)
(私はロジクールの攻撃3を使用しています)。前もって感謝します!あなたはシステムから情報を取得するためにpygame.event.pump()
を呼び出しpygame.event.get()
またはその他のイベントの機能を使用しないとき - あなたがイベントを処理していない場合get_pressed()
、get_button()
、get_axis()
よう
イベントを処理しないと 'get_pressed()'、 'get_button()'、 'get_axis()'は機能しないかもしれません。 'pygame.event.get() 'または' pygame.event.pump() 'を呼び出してシステムから情報を取得する他の' event'関数を呼び出します。 – furas
@furasコードにアクセスすると明日テストをします。 –
@furrasそれは完璧に機能しました。あなたが答えとして書式を設定する場合、私はそれを正しいとマークします。 –