搖桿數(shù)據(jù)記錄
import pygame#加載庫
# 定義顏色
BLACK ? ?= ( ? 0, ? 0, ? 0)
WHITE ? ?= ( 255, 255, 255)
#這是一個簡單的類,將幫助我們打印到屏幕上。它與操縱桿無關(guān),只是輸出信息。
class TextPrint:
? ? def __init__(self):
? ? ? ? self.reset()
? ? ? ? self.font = pygame.font.Font(None, 20)
? ? def print(self, screen, textString):
? ? ? ? textBitmap = self.font.render(textString, True, BLACK)
? ? ? ? screen.blit(textBitmap, [self.x, self.y])
? ? ? ? self.y += self.line_height
? ? ? ?
? ? def reset(self):
? ? ? ? self.x = 10
? ? ? ? self.y = 10
? ? ? ? self.line_height = 15
? ? ? ?
? ? def indent(self):
? ? ? ? self.x += 10
? ? ? ?
? ? def unindent(self):
? ? ? ? self.x -= 10
#設(shè)置屏幕得到寬度和長度 [width,height]
pygame.init()#初始化
size = [300, 300]
screen = pygame.display.set_mode(size)
pygame.display.set_caption("controller_test")
#被用來管理屏幕更新的速度
clock = pygame.time.Clock()
# 初始化joystick
pygame.joystick.init()
# 準備好打印
textPrint = TextPrint()
date = open('controll_joystick_date.txt',"w+")#新建數(shù)據(jù)文本
done = False#循環(huán)
#----程序主體部分--------
while done == False:#無線循環(huán)主體程序
? ?for event in pygame.event.get():#獲取玩家輸入
? ? ? ?if event.type == pygame.QUIT:
? ? ? ? ? done=True
? ? # 繪制的步驟
? ? #首先,用白色清除屏幕。不要放其它的繪圖指令
? ? #在這條上面的指令,將會被擦除
? ?screen.fill(WHITE)
? ?textPrint.reset()
? ?joystick_count = pygame.joystick.get_count()#獲取搖桿數(shù)目
? ?for i in range ( joystick_count ):#
? ? ? ? joystick = pygame.joystick.Joystick(i)
? ? ? ? joystick.init()
? ? ? ? axes = joystick.get_numaxes()#獲取軸數(shù)目
? ? ? ? for i in range ( axes ) :
? ? ? ? ? ? axis_0 = joystick.get_axis( 0 )#左搖桿x軸
? ? ? ? ? ? axis_1 = joystick.get_axis( 1 )#左搖桿y軸
? ? ? ? ? ? axis_2 = joystick.get_axis( 2 )#右搖桿x軸
? ? ? ? ? ? axis_3 = joystick.get_axis( 3 )#右搖桿y軸
? ? ? ? ? ? axis_4 = joystick.get_axis( 4 )#左板機
? ? ? ? ? ? axis_5 = joystick.get_axis( 5 )#右板機
? ? ? ? textPrint.print(screen,"Left_stick_x_axis value: {:>6.4f}".format(axis_0))
? ? ? ? textPrint.print(screen,"Left_stick_y_axis value: {:>6.4f}".format(axis_1))
? ? ? ? textPrint.print(screen,"Right_stick_x_axis value: {:>6.4f}".format(axis_2))
? ? ? ? textPrint.print(screen,"Right_stick_y_axis value: {:>6.4f}".format(axis_3))
? ? ? ? textPrint.print(screen,"Left_trigger value: {:>6.4f}".format(axis_4))
? ? ? ? textPrint.print(screen,"Right_trigger value: {:>6.4f}".format(axis_5))
? ? ? ? textPrint.unindent()
? ? ? ? date.write("{:>6.4f}\n".format(axis_3))#把搖桿數(shù)值寫入文本
? ?pygame.display.flip()
? ?clock.tick(20)
date.close()
pygame.quit ()