簡體   English   中英

如何在手機游戲中使用 Pygame 觸摸事件?

[英]How to use Pygame touch events in a mobile game?

游戲目標適用於安卓設備。 當我添加按鈕時,它們似乎一次只能工作一個,我怎樣才能讓它工作更多的按鈕?

這是功能:

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()

[...] 他們似乎一次只工作一個 [...]”

你只有一只鼠標。 您必須使用“觸摸”事件。 使用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()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM