无法使打印语句工作 PS4 模拟棒

Can't get print statements to work PS4 analog stick

一切正常,但我无法让程序打印 ('Backward totally') 和打印 ("Right totally")。我很确定我的值在 if 语句中是正确的。

我有另一个程序可以显示模拟摇杆所在位置的值,并且我的值是正确的。我试过切换更大的 than/less than 语句但是什么也没做,我已经仔细检查了值。

        joystick = pygame.joystick.Joystick(i)###########
        joystick.init()

        for i in range( 0, 2 ):
            axis = joystick.get_axis( i )
            #print('Axis {} value: {:>6.3f}'.format(i, axis))
            axis0 = joystick.get_axis(0)
            axis1 = joystick.get_axis(1)
            #backward totally
            if axis1 == 1.000:
                print("backward totally")
            #Nothing GOOD
            if -.100 < axis0 < .100 and -.100 < axis1 < .100:
                print('centered')
            #forward totally GOOD
            if axis1 == -1.000:
                print('forward totally')

            #left totally GOOD
            if axis0 == -1.000 and -.599 < axis1 < 0.200:
                print("left totally")
            #right totallly 
            if axis0 == 1.000 and -.599 < axis1 < 0.200:
                print('Right totally')

它不会出错,它只是不打印任何东西,我不知道为什么,我希望它能够完全正确或完全向后打印。

我修改了您代码中的条件语句以添加 "dead zone" 和 "edge zone" 的概念。您可以将 "dead zone" 视为一个内半径,在该半径内运动被注册为 "centered",而在较大的外半径之外(即在边缘区域)的运动是在指定方向上的运动。这是该代码的样子;考虑到我的 xbox 360 控制器的布局,每根棍子的复制肯定有改进的空间:

import pygame

pygame.init()

"""
Just a check to ensure a joystick is plugged in.
Only going to retrieve data for the 0th joystick anyways...
"""
num_joysticks = pygame.joystick.get_count()
if num_joysticks == 0:
    raise ValueError("No joysticks attached!")

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

#Just some info about the controller
j_id = joystick.get_id()
j_name = joystick.get_name()
num_axes = joystick.get_numaxes()
num_balls = joystick.get_numballs()
num_buttons = joystick.get_numbuttons()
num_hats = joystick.get_numhats()
print(j_id, j_name)
print(num_axes, num_balls, num_buttons, num_hats)

#Define the inner and outer radii that are considered 0 and 100% movement
dead_zone = 0.2#inner radius
edge_zone = 0.9#outer radius

while True:
    for event in pygame.event.get():
        pass

    """
    Only have an xbox 360 controller to work with
    There, axes 0 and 1 correspond the left stick x and y
    And axes 3 and 4 correspond to the right stick x and y
    Not sure what axes 2 and 5 are listed for...
    For me, the ranges only go from [-1.0, 1.0), so 1.0 is not included for right and bottom.
    """
    left_x = joystick.get_axis(0)
    left_y = joystick.get_axis(1)
    right_x = joystick.get_axis(3)
    right_y = joystick.get_axis(4)

    if (-dead_zone < left_x < dead_zone) and (-dead_zone < left_y < dead_zone):
        print("Left stick centered")
    elif left_x < -edge_zone:
        print("Left stick is totally left")
    elif left_x > edge_zone:
        print("Left stick is totally right")
    elif left_y < -edge_zone:
        print("Left stick is totally up")
    elif left_y > edge_zone:
        print("Left stick is totally down")
    else:
        print("Left stick is somewhere")

    if (-dead_zone < right_x < dead_zone) and (-dead_zone < right_y < dead_zone):
        print("Right stick centered")
    elif right_x < -edge_zone:
        print("Right stick is totally left")
    elif right_x > edge_zone:
        print("Right stick is totally right")
    elif right_y < -edge_zone:
        print("Right stick is totally up")
    elif right_y > edge_zone:
        print("Right stick is totally down")
    else:
        print("Right stick is somewhere")