android 如何在移动的游戏中使用Pygame触摸事件?

vh0rcniy  于 2022-11-03  发布在  Android
关注(0)|答案(1)|浏览(224)

游戏目标是为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()
ws51t4hk

ws51t4hk1#

[...]他们似乎一次只工作一个[...]”
你只有一个鼠标。你必须使用“touch”事件。使用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.Rectpygame.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()

相关问题