How to use Pygame touch events in a mobile game?

Question:

Game Target is for android device.
When i add the buttons, they seem only to work one at a time, how can i make it so more button could work?

Here’s the function:

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()
Asked By: Thirtek

||

Answers:

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

You only have one mouse. You have to use the "touch" events. Use the FINGERDOWN and FINGERUP event. Store the position of the finger into a dictionary when a FINGERDOWN event occurs and remove it on 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)

    # [...]

Use the positions to detect if a button is touched. Use pygame.Rect and pygame.Rect.collidepoint for the "touch" detection. e.g.:

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

Minimal example:

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()
Answered By: Rabbid76
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.