我正在尝试使用我的XB1控制器通过PyGame控制无人机,我正在运行python 3.8.3和pygame 1.9.6。收集控制器输入很好,但是,我打算让该程序在用户每次向右握住操纵杆帽子时增加。每当我将控制器操纵杆向右移动而不希望更新轴值时,代码就会陷入无限循环。
代码:
def aileron_right():
axis = joystick.get_axis(i)
degree = 0
while(joystick.get_axis(i) >= 0.999969482421875):
print(joystick.get_axis(i))
for degree in range(90):
if(joystick.get_axis(i) >= 0.999969482421875):
degree +=1
print("UAV AILERON RIGHT DEGREE:",degree)
else:
break
joystick.init()
aileron_right()
我尝试将其更改为IF语句,以查看循环是否将停止,并且我已将增量语句移至ELSE语句,但无济于事。任何建议将不胜感激。
编辑:我已在下面包含完整的代码。
import pygame
import time
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
class TextPrint(object):
def __init__(self):
""" Constructor """
self.reset()
self.x_pos = 10
self.y_pos = 10
self.font = pygame.font.Font(None, 20)
def print(self, my_screen, text_string):
""" Draw text onto the screen. """
text_bitmap = self.font.render(text_string, True, BLACK)
my_screen.blit(text_bitmap, [self.x_pos, self.y_pos])
self.y_pos += self.line_height
def reset(self):
""" Reset text to the top of the screen. """
self.x_pos = 10
self.y_pos = 10
self.line_height = 15
def indent(self):
""" Indent the next line of text """
self.x_pos += 10
def unindent(self):
""" Unindent the next line of text """
self.x_pos -= 10
pygame.init()
size = [500, 700]
screen = pygame.display.set_mode(size)
pygame.display.set_caption("My Game")
done = False
clock = pygame.time.Clock()
pygame.joystick.init()
textPrint = TextPrint()
while not done:
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(WHITE)
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):
joystick.init()
axis = joystick.get_axis(i)
textPrint.print(screen, "Axis {} value: {:>6.3f}".format(i, axis))
degree = 0
if(joystick.get_axis(i) >= 0.966):
print("UAV AILERON RIGHT")
degree = 0
while(joystick.get_axis(i) >= 0.966):
while(degree in range(10)):
degree+=1
print(degree+1)
print("UAV AILERON LEFT")
pygame.init()
'''while(joystick.get_axis(i) >= float(0.097)):
print("hello")'''
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)))
if(joystick.get_hat(i) == (0,1)):
print("UAV PITCH UP")
elif(joystick.get_hat(i) == (0,-1)):
print("UAV PITCH DOWN")
textPrint.unindent()
textPrint.unindent()
pygame.display.flip()
clock.tick(100)
pygame.quit()
我尚不清楚您想要的结果是什么,但是要停止循环,请删除一会儿。根据您每次戴上帽子正确时增加的要求,类似以下的内容可能会有所帮助。目前尚不清楚在您的问题中变量i
的位置,因此我将其复制为原样。
RIGHT_MAX = 0.999969
def isAileronRight( joystick, index ):
""" Return True of the joystick hat is positioned right """
result = False
axis = joystick.get_axis( index )
if ( axis > RIGHT_MAX ):
result = True
print( "Joystick at %0.10f" % ( axis ) )
return result
hat_right_already = False # is the hat still to the right?
hat_right_count = 0
# Main loop
while not finsihed:
[...]
# Count the number of times the hat is moved to the right
if ( isAileronRight( joystick, i ) ):
if ( not hat_right_already ):
hat_right_count += 1
hat_right_already = True
else:
hat_right_already = False # hat no longer position right