首页 > 解决方案 > Pygame 没有检测到 PS4 控制器上的 L2 和 R2

问题描述

我最近才开始涉足 python 编码的世界,这非常有趣。

目前,我正在尝试设置一些东西来检测来自我的 PS4 控制器的输入,并根据按下的按钮发生不同的事情。

在谷歌搜索和这个论坛的帮助下,我发现 pygame 对这项工作很有用,而且除了 1 个细节之外,我已经设置了一些似乎工作得很好的东西。

该程序似乎不承认来自L2和的输入R2。我怀疑问题在于这些按钮不是二进制的,而是从根本不按下到完全按下的范围。

就我的想法而言,只要检测到它是否被按下就足够了。

这是我的代码:

import pygame
import threading

def controller():
    pygame.init()

j = pygame.joystick.Joystick(0)
j.init()

try:
    while True:
        events = pygame.event.get()
        for event in events:
            if event.type == pygame.JOYBUTTONDOWN:
                print("Button Pressed")
                if j.get_button(0):
                    print("X")
                elif j.get_button(1):
                    print("Circle")
                elif j.get_button(2):
                    print("Square")
                elif j.get_button(3):
                    print("Triangle")
                elif j.get_button(4):
                    print("Share")
                elif j.get_button(5):
                    print("PS")
                elif j.get_button(6):
                    print("Start")
                elif j.get_button(7):
                    print("L3")
                elif j.get_button(8):
                    print("R3")
                elif j.get_button(9):
                    print("L1")
                elif j.get_button(10):
                    print("R1")
                elif j.get_button(11):
                    print("Pad_Up")
                elif j.get_button(12):
                    print("Pad_Down")
                elif j.get_button(13):
                    print("Pad_Left")
                elif j.get_button(14):
                    print("Pad_Right")
            elif event.type == pygame.JOYBUTTONUP:
                print("Button Released")

except KeyboardInterrupt:
    print("EXITING NOW")
    j.quit()

if __name__ =='__main__':
    t1 = threading.Thread(target = controller)
    t1 .start()

当我运行代码时,一切都运行良好,但按下L2R2没有做任何事情。这可能是显而易见的痛苦,但正如我所说,我是一个完全的初学者,所以请耐心等待!任何帮助表示赞赏!:)

编辑:也许这些按钮不属于事件类型JoyButtonDown?我会试试其他的!

编辑 2:按钮确实属于另一种事件类型!JoyAxisMotion!现在要弄清楚如何将其转换为简单的二进制函数..

我想这个问题的核心已经解决了,所以我想这个线程可能会被锁定或删除,或者你们在这里做什么。:)

标签: pythoninputpygamecontroller

解决方案


推荐阅读