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

标签 python ubuntu input joystick gamepad

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

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

:)

最佳答案

我一直在尝试做同样的事情,但对于飞行模拟器,到目前为止,我所能做的就是使用 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()

关于python - 使用python将操纵杆输入发送到程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49352561/

相关文章:

python - 如何使用python子进程使用shell'source'命令

python - 如何将一维数组转换为逻辑矩阵

JQuery onclick 函数聚焦输入

c - 如何动态获取整数输入并让循环在按回车键时终止?

php - 是否可以编写仅针对 Ubuntu Web Server 的代码?

python - 询问用户输入,直到他们给出有效的响应

python - 当 View 函数中修改表单字段的属性时,validate_on_submit 在 POST 上失败

python - 记住Python中的静态属性

c++ - 在 ubuntu 上编译 lsnes 模拟器时出错

postgresql - 在 Ubuntu 9.10 上为 Pg 8.4 构建 Postgis 1.5.x 时出现问题