在手机游戏中一次只能使用一个按钮

No more than one button works at a time in a mobile game

游戏目标适用于 android 设备。 当我添加按钮时,它们似乎一次只能工作一个,我怎样才能让更多的按钮工作?

函数如下:

def button_move(player_car):
    pressed = pygame.mouse.get_pressed()
    pos = pygame.mouse.get_pos()
    moved = False
    
    if pressed[0] == 1:
        if 300 < pos[0] < 400 and 800 < pos[1] < 900:
            player_car.move_forward()
            moved = True
        if 300 < pos[0] < 400 and 1100 < pos[1] < 1200:
            player_car.move_backward()
            moved = True
        if 100 < pos[0] < 200 and 950 < pos[1] < 1050:
            player_car.rotate(left=True)
        if 500 < pos[0] < 600 and 950 < pos[1] < 1050:
            player_car.rotate(right=True)
            
        if not moved:
            player_car.reduce_speed()

[...] they seem only to work one at a time [...]"

您只有一只鼠标。您必须使用“触摸”事件。使用 FINGERDOWNFINGERUP 事件。当 FINGERDOWN 事件发生时,将手指的位置存储到字典中,并在 FINGERUP:

时将其删除
fingers = {}
run = True
while run:
    clock.tick(60)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
        if event.type == pygame.FINGERDOWN:
            x = event.x * window.get_height()
            y = event.y * window.get_width()
            fingers[event.finger_id] = x, y
        if event.type == pygame.FINGERUP:
            fingers.pop(event.finger_id, None)

    # [...]

使用这些位置来检测按钮是否被触摸。使用 pygame.Rect and pygame.Rect.collidepoint 进行“触摸”检测。例如:

rect = pygame.Rect(300, 800, 100, 100)
touched = False
for finger, pos in fingers.items():       
    if rect.collidepoint(pos):
        touched = True   

最小示例:

import pygame

pygame.init()
window = pygame.display.set_mode((300, 300))
clock = pygame.time.Clock()

buttons = [
    pygame.Rect(25, 25, 100, 100),
    pygame.Rect(175, 25, 100, 100),
    pygame.Rect(25, 175, 100, 100),
    pygame.Rect(175, 175, 100, 100)]
colors = [(64, 0, 0), (64, 64, 0), (0, 64, 0), (0, 0, 64)]
colorsH = [(255, 0, 0), (255, 255, 0), (0, 255, 0), (0, 0, 255)]

fingers = {}
run = True
while run:
    clock.tick(60)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
        if event.type == pygame.FINGERDOWN:
            x = event.x * window.get_height()
            y = event.y * window.get_width()
            fingers[event.finger_id] = x, y
        if event.type == pygame.FINGERUP:
            fingers.pop(event.finger_id, None)

    highlight = []  
    for i, rect in enumerate(buttons): 
        touched = False
        for finger, pos in fingers.items():       
            if rect.collidepoint(pos):
                touched = True
        highlight.append(touched)   

    # the same with list comprehensions
    #highlight = [any(r.collidepoint(p) for _, p in fingers.items()) for _, r in enumerate(buttons)]

    window.fill(0)
    for rect, color, colorH, h in zip(buttons, colors, colorsH, highlight):
        c = colorH if h else color
        pygame.draw.rect(window, c, rect)
    pygame.display.flip()

pygame.quit()
exit()