From 320569a0d0355bf02b2d2b9aead3ba58d0b834f4 Mon Sep 17 00:00:00 2001 From: pascald Date: Fri, 2 May 2025 17:11:46 +0200 Subject: [PATCH] test for touch function --- main.py | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/main.py b/main.py index 31cb110..84ae31d 100644 --- a/main.py +++ b/main.py @@ -70,6 +70,31 @@ def init_sounds(): case_images = {} +def is_snap_event(event): + global touch_start + + # Tastatur oder Joystick A + if (event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE) or \ + (event.type == pygame.JOYBUTTONDOWN and event.button == 0): + return True + + # Touch (Tap) + if event.type == pygame.FINGERDOWN: + touch_start = (event.x * SCREEN_WIDTH, event.y * SCREEN_HEIGHT) + + elif event.type == pygame.FINGERUP and touch_start: + end = (event.x * SCREEN_WIDTH, event.y * SCREEN_HEIGHT) + dx = end[0] - touch_start[0] + dy = end[1] - touch_start[1] + distance = (dx ** 2 + dy ** 2) ** 0.5 + touch_start = None + + if distance < 50: # Kurzer Tap = Snap + return True + + return False + + async def init_game(): global screen, clock, font, controls pygame.init() @@ -670,8 +695,7 @@ async def main(): stacked_cases.append(current_case) spawn_case_in_game() shake_timer = 10 - elif ((event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE) or - (event.type == pygame.JOYBUTTONDOWN and event.button == 0)): + elif is_snap_event(event): if current_case and current_case.allow_snap: await current_case.animate_snap() can_use_tilt = True