Thrustmaster(图马思特) HOTAS Warthog 疣猪杆 读取按钮数据

我所使用的摇杆是THRUSTMASTER(右下图所示),用于在目标追踪页面进行框选物体(实际应用场景不涉及鼠标键盘,只能使用操作杆进行操作)

1、首先,安装摇杆所需环境,win+R输入cmd后,输入activate(这部分可以参考之前anaconda安装)

进入base环境后,安装pygame的命令行:

       python -m pip install --user pygame

使用pygame进行读取摇杆的操作:数据为1证明已经读到,数据为0证明还未读到:

2、读取joysticks各个按钮所对应的标识:

使用操纵杆实际上是使用了不同的button标识,如下图所示:

代码如下:

import pygame

print(pygame.ver)

#判断是否读取到摇杆

pygame.joystick.init()

count = pygame.joystick.get_count()

count

#coding:utf-8

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 defprint(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

pygame.init()

 

# 设置屏幕的宽度和高度(宽度、高度)

size = [500, 700]

screen = pygame.display.set_mode(size)#屏幕、显示、样式

 

pygame.display.set_caption("对应的按钮标识")#标题

 

#循环,直到用户点击关闭按钮。

done = False

 

# 用来管理屏幕更新的速度

clock = pygame.time.Clock()

 

#初始化操纵杆

pygame.joystick.init()

   

#准备打印

textPrint = TextPrint()

 

# -------- 主循环程序-----------

while done==False:

    #事件处理步骤

    for event in pygame.event.get():  #用户做了什么

        if event.type == pygame.QUIT: #如果用户点击关闭

            done=True #标记我们已经完成,所以我们退出这个循环

       

        # 可能的操纵杆动作: 轴  球  按钮  帽子

        if event.type == pygame.JOYBUTTONDOWN:

            print("Joystick button pressed.")#操纵杆按钮按下

            # 在这里获取操作杆按钮按下时,坐标位置,当作是框框的左上角的点

        if event.type == pygame.JOYBUTTONUP:

            print("Joystick button released.")# 操纵杆按钮解除   

            # 在这里获取操作杆按钮解除是,坐标位置,当作是框框的右下角的点

            # 根据框框左上角的点和右下角的点就可以确定一个框框

 

    #绘图设置

    #首先,将屏幕显示为白色,不要输入其他绘图命令

    #否则,他们将会被这个命令抹去。

    screen.fill(WHITE)

    textPrint.reset()

 

    #获得操纵杆的数量

    joystick_count = pygame.joystick.get_count()#数量

 

    textPrint.defprint(screen, "Number of joysticks: {}".format(joystick_count) )#格式

    textPrint.indent()#缩进

   

    #遍历每个操纵杆

    for i in range(joystick_count): #范围

        joystick = pygame.joystick.Joystick(i) #新建一个Joystick对象

        joystick.init() #初始化joystick模块

   

        textPrint.defprint(screen, "Joystick {}".format(i) )

        textPrint.indent() #初始化joystick模块

   

        #从操作系统中找到操纵杆的名字

        name = joystick.get_name() #获得Joystick系统名称

        textPrint.defprint(screen, "Joystick name: {}".format(name) )

       

        # 通常轴成对运行,一个向上 / 向下,另一个向左 / 向右

        axes = joystick.get_numaxes()  #获得Joystick操纵轴的数量

        textPrint.defprint(screen, "Number of axes: {}".format(axes) )

        textPrint.indent()

       

        for i in range( axes ):

            axis = joystick.get_axis(i)  #获得操纵轴的当前坐标

            textPrint.defprint(screen, "Axis {} value: {:>6.3f}".format(i,axis) )

            #:>6.3f :总长度为6位,精确到小数点后三位的浮点类型

        textPrint.unindent()#不缩进

           

        buttons = joystick.get_numbuttons() #获得Joystick上按钮的数量

        textPrint.defprint(screen, "Number of buttons: {}".format(buttons) )

        textPrint.indent()

 

        for i in range( buttons ):

            button = joystick.get_button(i) #获得当前按钮的状态。

            textPrint.defprint(screen, "Button {:>2} value: {}".format(i,button) )

        textPrint.unindent()

           

        # 帽子开关,要么全有,要么没有,不像操纵杆

        # 值返回到数组中。

        hats = joystick.get_numhats() #获得Joystick上帽键的数量

        textPrint.defprint(screen, "Number of hats: {}".format(hats) )

        textPrint.indent()

 

        for i in range( hats ):

            hat = joystick.get_hat( i ) #获得Joystick上帽键的位置

            textPrint.defprint(screen, "Hat {} value: {}".format(i, str(hat)) )

        textPrint.unindent()

       

        textPrint.unindent()

 

    # 所有要绘制的代码都应该超过这个注释   

    # 继续用我们绘制的图像更新屏幕。

    pygame.display.flip()

 

    # 最多20个帧率

    clock.tick(20)

   

# 关上窗口,退出

# 如果你忘了这一行,程序会“挂起”

# 在退出,如果从IDLE运行

pygame.quit ()  #卸载joystick模块

 

  • 2
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值