python - 使用 PyGame 中的操纵杆模块

标签 python pygame joystick gamepad

带注释的确切代码可以找到here ,或here .

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(darkgrey)
textPrint.reset()

joystick_count = pygame.joystick.get_count()

textPrint.print(screen, "Number of joysticks: {}".format(joystick_count) )
textPrint.indent()

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

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

    name = joystick.get_name()
    textPrint.print(screen, "Joystick name: {}".format(name) )

    axes = joystick.get_numaxes()
    textPrint.print(screen, "Number of axes: {}".format(axes) )
    textPrint.indent()

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

    buttons = joystick.get_numbuttons()
    textPrint.print(screen, "Number of buttons: {}".format(buttons) )
    textPrint.indent()

    for i in range( buttons ):
        button = joystick.get_button( i )
        textPrint.print(screen, "Button {:>2} value: {}".format(i,button) )
    textPrint.unindent()

    hats = joystick.get_numhats()
    textPrint.print(screen, "Number of hats: {}".format(hats) )
    textPrint.indent()

    for i in range( hats ):
        hat = joystick.get_hat( i )
        textPrint.print(screen, "Hat {} value: {}".format(i, str(hat)) )
    textPrint.unindent()

pygame.display.flip()
clock.tick(20)

在在线 PyGame 文档的帮助下,我能够生成一个显示各个操纵杆值的屏幕。 如何将这些值转换为表示按下此按钮的事件,现在执行此操作?

类似的东西,

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

    if Joystick 2's Axis 3 is equal to 1:
        print("Joystick 2 is pointing to the right.")

    if Joystick 2's Axis 3 is equal to -1:
        print("Joystick 2 is pointing to the left.")

textPrint.unindent()

最佳答案

大多数游戏将游戏 handle 输入作为“主循环”的一部分进行处理,每帧至少运行一次。如果您有读取游戏 handle 输入的代码,那么您应该将其放入主循环中(可能靠近顶部),然后再执行另一个步骤来处理输入并更新游戏状态。您应该在此处执行一些操作,例如查看按钮值并决定如何将它们转换为游戏操作。游戏状态更新后,您可以重绘显示以匹配新的游戏状态并等待下一帧。

您还可以将游戏 handle 输入视为事件并同步处理它们,但很难做到这一点,因为输入可能随时出现。对于大多数应用程序来说,最好一次处理所有游戏 handle 输入。如果您同步处理输入更改,则当更新应该放在一起时,您可能会将更新视为独立的。例如,当操纵杆移动时,X 和 Y 的变化应被视为同一输入变化的一部分。如果您不小心,单独处理它们可能会在游戏中转化为移动时产生阶梯模式。

关于python - 使用 PyGame 中的操纵杆模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52457595/

相关文章:

python - 使用临时变量与从字典中重复读取相同的键/值

python - Windows/Python pygame.错误 : video system not initialized after adding Mp3 file

python - Pygame def 函数

python - pygame中的动画

c++ - 罗技 G29 操纵杆中的力反馈

java - Android 屏幕操纵杆问题

python - 什么是 Python 中的表达式?

python - 在python plotly包中的散点图中操纵图例

java.io.IOException : Could not read footer for file FileStatus when trying to read parquet file from Spark cluster from IBM Cloud Object Storage

macos - 如何知道 Cocoa 中何时连接了 HID USB/蓝牙设备?