我正在尝试将GPIO引脚按钮用于我在这里的Pygame游戏。我一直在尝试让GPIO按钮起作用。除了按钮之外,游戏还可以正常运行。
我尝试使用RPi.GPIO和gpiozero(当前)。我仍然无法弄清楚我做错了什么。主要游戏代码如下。如果需要,我可以提供完整的代码。
def game_loop():
x = (display_width * 0.45)
y = (display_height * 0.8)
x_change = 0
object_startx = random.randrange(0, display_width)
object_starty = -600
object_speed = 2
object_width = 20
object_height = 20
objectCount = 1
dodged = 0
gameExit = False
while not gameExit:
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(18, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.add_event_detect(23, GPIO.RISING)
GPIO.add_event_detect(18, GPIO.RISING)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.KEYDOWN:
if GPIO.event_detected(23):
x_change = -5
if GPIO.event_detected(18):
x_change = 5
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
x_change = 0
x += x_change
gameDisplay.fill(white)
objects(object_startx, object_starty, object_width, object_height, black)
object_starty += object_speed
car(x, y)
objects_dodged(dodged)
if x > display_width - car_width or x < 0:
crash()
if object_starty > display_height:
object_starty = 0-object_height
object_startx = random.randrange(0,display_width)
dodged += 1
object_speed +=.2
object_width += (dodged * 1.001)
if y < object_starty+object_height:
if x > object_startx and x < object_startx + object_width or x+car_width > object_startx and x+ car_width < object_startx+object_width:
crash()
pygame.display.update()
clock.tick(60)
当我单击GPIO左右按钮时,它应该向右或向左移动。 (GPIO引脚18在右侧,而GPIO引脚23在左侧)。