使用 python 将操纵杆输入发送到程序

2023-12-25

我正在虚拟机中使用 Ubuntu,我想将带有 Python 脚本的操纵杆输入发送到其他程序。

基本上与https://pythonprogramming.net/direct-input-game-python-plays-gta-v/?completed=/open-cv-basics-python-plays-gta-v/ https://pythonprogramming.net/direct-input-game-python-plays-gta-v/?completed=/open-cv-basics-python-plays-gta-v/但只有操纵杆/游戏手柄。

:)


我一直在尝试做同样的事情,但是对于飞行模拟器来说,到目前为止我所能做的就是使用 pygame 中的操纵杆模块在 python 中拾取物理操纵杆运动。

这是取自 pygame 文档的代码,它只显示操纵杆每个轴的值。

import pygame

# Define some colors.
BLACK = pygame.Color('black')
WHITE = pygame.Color('white')

# This is a simple class that will help us print to the screen.
# It has nothing to do with the joysticks, just outputting the
# information.
class TextPrint(object):
    def __init__(self):
        self.reset()
        self.font = pygame.font.Font(None, 20)

    def tprint(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()

# Set the width and height of the screen (width, height).
screen = pygame.display.set_mode((500, 200))
pygame.display.set_caption("AI fly")
# Loop until the user clicks the close button.
done = False
# Used to manage how fast the screen updates.
clock = pygame.time.Clock()
# Initialize the joysticks.
pygame.joystick.init()
# Get ready to print.
textPrint = TextPrint()

# -------- Main Program Loop -----------
while not done:

    for event in pygame.event.get(): # User did something.
        if event.type == pygame.QUIT: # If user clicked close.
            done = True # Flag that we are done so we exit this loop.
        elif event.type == pygame.JOYBUTTONDOWN:
            print("Joystick button pressed.")
        elif event.type == pygame.JOYBUTTONUP:
            print("Joystick button released.")

    screen.fill(WHITE)
    textPrint.reset()
    # Get count of joysticks.
    joystick_count = pygame.joystick.get_count()

    # For each joystick:
    for i in range(joystick_count):
        joystick = pygame.joystick.Joystick(i)
        joystick.init()

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

        # for i in range(axes):
            # axis = joystick.get_axis(i)
            # textPrint.tprint(screen, "Axis {} value: {:>6.3f}".format(i, axis))
        # textPrint.unindent()

        axis0 = joystick.get_axis(0)

        textPrint.tprint(screen, "Axis {} value: {:>6.3f}".format(0, axis0))

        axis1 = joystick.get_axis(1)
        textPrint.tprint(screen, "Axis {} value: {:>6.3f}".format(1, axis1))

        axis2 = joystick.get_axis(2)
        textPrint.tprint(screen, "Axis {} value: {:>6.3f}".format(2, axis2))

        axis3 = joystick.get_axis(3)
        textPrint.tprint(screen, "Axis {} value: {:>6.3f}".format(3, axis3))

    # ALL CODE TO DRAW SHOULD GO ABOVE THIS COMMENT
    #
    # Go ahead and update the screen with what we've drawn.
    pygame.display.flip()
    # Limit to 20 frames per second.
    clock.tick(20)
pygame.quit()
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 python 将操纵杆输入发送到程序 的相关文章

  • 如何打印脚本的每一行,因为它仅针对正在运行的顶级脚本运行?

    python 跟踪模块将允许您运行一个脚本 打印每一行代码 因为它在脚本和所有导入的模块中运行 如下所示 python m trace trace myscript py 有没有办法做同样的事情 但是only打印顶级调用 即仅打印以下行my
  • 从 Python 中的 message_from_string() 获取发件人地址

    有人可以告诉我如何在Python中从email message from string 获取发件人地址吗 谢谢 我试过 message email message from string email text from message Fr
  • setColumnStretch 和 setRowStretch 如何工作

    我有一个使用构建的应用程序PySide2它使用setColumnStretch用于柱拉伸和setRowStretch用于行拉伸 它工作得很好 但我无法理解它是如何工作的 我参考了 qt 文档 但它对我没有帮助 我被困在括号内的两个值上 例如
  • 如何使用 Pycharm 运行 fast-api 服务器?

    我有一个简单的 API 函数 如下所示 from fastapi import FastAPI app FastAPI app get async def read root return Hello World 我正在使用启动服务器uvi
  • 如何检查给定的数字是否是2的幂?

    下面的代码不适用于某些输入 a i set 1 while i lt 10000 a add i i lt lt 1 N int input if N in a print True else print False 我最初的想法是检查每个
  • 如何同时有效地运行多个 Pytorch 进程/模型? Traceback:分页文件太小,无法完成此操作

    背景 我有一个非常小的网络 我想用不同的随机种子进行测试 该网络几乎只使用了我的 GPU 计算能力的 1 因此理论上我可以同时运行 50 个进程来同时尝试许多不同的种子 Problem 不幸的是我什至无法在多个进程中导入 pytorch 当
  • 如何用pygame画一条虚线?

    我需要在坐标系上绘制正弦波和余弦波 就像在this https i stack imgur com DGI8g png图片 除了没能代表以外 我所有的工作都做得很好虚线和曲线与 pygame 一致 我有与我需要的类似的东西 但我怎样才能让它
  • 清理 MongoDB 的输入

    我正在为 MongoDB 数据库程序编写 REST 接口 并尝试实现搜索功能 我想公开整个 MongoDB 接口 我确实有两个问题 但它们是相关的 所以我将它们放在一篇文章中 使用 Python json 模块解码不受信任的 JSON 是否
  • pandas DataFrame 中行的高效成对比较

    我目前正在处理一个较小的数据集 大约 900 万行 不幸的是 大多数条目都是字符串 即使强制类别 框架在内存中也只有几 GB 我想做的是将每一行与其他行进行比较 并对内容进行直接比较 例如 给定 A B C D 0 cat blue old
  • 按升序对数字字符串列表进行排序

    我创建了一个SQLite https en wikipedia org wiki SQLite数据库有一个存储温度值的表 第一次将温度值按升序写入数据库 然后 我将数据库中的温度值读入列表中 然后将该列表添加到组合框中以选择温度 效果很好
  • Python正则表达式替换引号中的文本(引号本身除外)

    例如 我有一个测试字符串 content I opened my mouth Good morning I said cheerfully 我想使用正则表达式删除双语音标记之间的文本 但不删除语音标记本身 所以它会返回 I opened m
  • Python:处理图像并保存到文件流

    我需要使用 python 处理图像 应用过滤器和其他转换 然后使用 HTTP 将其提供给用户 现在 我正在使用 BaseHTTPServer 和 PIL 问题是 PIL 无法直接写入文件流 因此我必须写入临时文件 然后读取该文件 以便将其发
  • 使用 Popen 打开进程并获取 PID

    我正在开发一个漂亮的小功能 def startProcess name path Starts a process in the background and writes a PID file returns integer pid Ch
  • 在添加数据之前使用 Python gdata 清除工作表中的行

    我有一个 Google 电子表格 我使用 python 脚本和 gdata 库填充值 如果我多次运行脚本 它会将新行附加到工作表中 我希望脚本在填充之前首先清除行中的所有数据 这样每次运行时我都会有一组新的数据脚本 我尝试过使用 Updat
  • 使用张量流导出神经网络的权重

    我使用张量流工具编写了神经网络 一切正常 现在我想导出神经网络的最终权重以制定单一的预测方法 我怎样才能做到这一点 您需要在训练结束时使用以下命令保存模型tf train Saver https www tensorflow org ver
  • PySpark DataFrame 上分组数据的 Pandas 式转换

    如果我们有一个由一列类别和一列值组成的 Pandas 数据框 我们可以通过执行以下操作来删除每个类别中的平均值 df DemeanedValues df groupby Category Values transform lambda g
  • 根据标签位置计算 Pandas DataFrame 的索引

    我正在尝试计算标签的索引Pandas https pandas pydata org DataFrame在每一列中 基本上我有以下内容DataFrame d col1 label1 label2 label3 col2 label2 lab
  • model.predict() 返回类而不是概率

    Hello 我是第一次使用 Keras 我训练并保存了一个模型 作为 json 文件及其权重 该模型旨在将图像分为 3 个类别 我的编译方法 model compile loss categorical crossentropy optim
  • Python 中的可逆 STFT 和 ISTFT

    有没有通用的形式短时傅立叶变换 https en wikipedia org wiki Short time Fourier transform与内置于 SciPy 或 NumPy 或其他什么中的相应逆变换 这是pyplotspecgram
  • nltk 标记化和缩写

    我用 nltk 对文本进行标记 只是将句子输入到 wordpunct tokenizer 中 这会拆分缩写 例如 don t 到 don t 但我想将它们保留为一个单词 我正在改进我的方法 以实现更精确的文本标记化 因此我需要更深入地研究

随机推荐