【问题标题】:Sending Joystick input to program using python使用python将操纵杆输入发送到程序
【发布时间】:2018-03-18 20:35:25
【问题描述】:

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

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

:)

【问题讨论】:

  • 你有一个你已经尝试过的例子吗?
  • 我试图为操纵杆找到任何“类似 pyautogui 的库”,但没有找到。

标签: python ubuntu input joystick gamepad


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2015-04-24
    • 2021-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-06
    相关资源
    最近更新 更多