Issue
Everything works but I can't get the program to print('Backward totally') & print("Right totally"). I am pretty sure my values are correct with the if statements.
I have another program that shows my the values of where the analog stick is and my values are correct. I have tried switching the greater than/less than statements but that did nothing, I have double-checked the values.
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')
It does not give errors it just does not print anything and I do not know why and I expected it to print either right totally or backward totally.
Solution
I modified the conditional statements in your code to add the concept of a "dead zone" as well as an "edge zone". You can think of the "dead zone" as an inner radius within which movement is registered as "centered", while movement outside of the larger outer radius (i.e. in the edge zone) is movement in a committed direction. Here is what that code could look like; there is definitely room for improvement with the duplication for each of the sticks to take into account the layout of my xbox 360 controller:
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")
Answered By - CodeSurgeon Answer Checked By - Gilberto Lyons (WPSolving Admin)