欢迎光临散文网 会员登陆 & 注册

摇杆数据记录

2022-10-27 18:40 作者:weezyr  | 我要投稿


import pygame#加载库

# 定义颜色

BLACK    = (   0,   0,   0)

WHITE    = ( 255, 255, 255)


#这是一个简单的类,将帮助我们打印到屏幕上。它与操纵杆无关,只是输出信息。

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

#设置屏幕得到宽度和长度 [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+")#新建数据文本

done = False#循环

#----程序主体部分--------

while done == False:#无线循环主体程序

   for event in pygame.event.get():#获取玩家输入

       if event.type == pygame.QUIT:

          done=True

    # 绘制的步骤

    #首先,用白色清除屏幕。不要放其它的绘图指令

    #在这条上面的指令,将会被擦除

   screen.fill(WHITE)

   textPrint.reset()

   joystick_count = pygame.joystick.get_count()#获取摇杆数目

   for i in range ( joystick_count ):#

        joystick = pygame.joystick.Joystick(i)

        joystick.init()

        axes = joystick.get_numaxes()#获取轴数目

        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))#把摇杆数值写入文本

   pygame.display.flip()

   clock.tick(20)

date.close()

pygame.quit ()


摇杆数据记录的评论 (共 条)

分享到微博请遵守国家法律