gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 18:31:42 25 4
gpt4 key购买 nike

我在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com