From 7b59b685648b1d8e8c9109aad7b26018c4cf11c4 Mon Sep 17 00:00:00 2001 From: pascald Date: Fri, 2 May 2025 23:59:07 +0200 Subject: [PATCH] better touch function --- main.py | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 55 insertions(+), 7 deletions(-) diff --git a/main.py b/main.py index d3e3605..aac9740 100644 --- a/main.py +++ b/main.py @@ -104,6 +104,58 @@ def is_SPACE_event(event): return False +def is_LEFT_event(event): + global touch_start, touch_start_time + + if ((event.type == pygame.KEYDOWN and event.key == pygame.K_LEFT) or + (event.type == pygame.JOYHATMOTION and event.value[0] == -1) or + (event.type == pygame.JOYBUTTONDOWN and event.button == 2)): + return True + + if event.type == pygame.FINGERDOWN: + touch_start = (event.x * SCREEN_WIDTH, event.y * SCREEN_HEIGHT) + touch_start_time = time.time() + + elif event.type == pygame.FINGERUP and touch_start: + end = (event.x * SCREEN_WIDTH, event.y * SCREEN_HEIGHT) + duration = time.time() - touch_start_time + dx = end[0] - touch_start[0] + dy = end[1] - touch_start[1] + + touch_start = None + touch_start_time = None + + if abs(dx) > 50 and abs(dy) < 80 and dx < -30 and duration > 0.3: + return True # Swipe nach links erkannt + + return False + + +def is_UP_event(event): + global touch_start, touch_start_time + + if ((event.type == pygame.KEYDOWN and event.key == pygame.K_UP) or + (event.type == pygame.JOYHATMOTION and event.value[1] == 1) or + (event.type == pygame.JOYBUTTONDOWN and event.button == 3)): + return True + + if event.type == pygame.FINGERDOWN: + touch_start = (event.x * SCREEN_WIDTH, event.y * SCREEN_HEIGHT) + touch_start_time = time.time() + + elif event.type == pygame.FINGERUP and touch_start: + end = (event.x * SCREEN_WIDTH, event.y * SCREEN_HEIGHT) + duration = time.time() - touch_start_time + dx = end[0] - touch_start[0] + dy = end[1] - touch_start[1] + + touch_start = None + touch_start_time = None + + if abs(dy) > 50 and abs(dx) < 80 and dy < -30 and duration > 0.3: + return True # Swipe nach oben erkannt + + return False async def init_game(): global screen, clock, font, controls @@ -674,18 +726,14 @@ async def main(): for event in pygame.event.get(): if event.type == pygame.QUIT: running = False - elif event.type == pygame.KEYDOWN or event.type == pygame.JOYBUTTONDOWN or event.type == pygame.JOYHATMOTION or event.type == pygame.FINGERDOWN or touch_start: + elif event.type == pygame.KEYDOWN or event.type == pygame.JOYBUTTONDOWN or event.type == pygame.JOYHATMOTION or event.type == pygame.FINGERDOWN or touch_start or(event.type == pygame.MOUSEBUTTONDOWN and event.button == 1): if state == 1: # PLAYING - if ((event.type == pygame.KEYDOWN and event.key == pygame.K_LEFT) or - (event.type == pygame.JOYHATMOTION and event.value[0] == -1) or - (event.type == pygame.JOYBUTTONDOWN and event.button == 2)): + if is_LEFT_event(event): can_tip = current_case.length_units * UNIT_HEIGHT <= TRAILER_HEIGHT if current_case and current_case.allow_snap and can_use_tilt and can_tip: await current_case.animate_tip() can_use_tilt = False - elif ((event.type == pygame.KEYDOWN and event.key == pygame.K_UP) or - (event.type == pygame.JOYHATMOTION and event.value[1] == 1) or - (event.type == pygame.JOYBUTTONDOWN and event.button == 3)): + elif is_UP_event(event): if current_case and current_case.allow_snap and can_use_on_top and current_case.can_place_on_top(): if await current_case.animate_place_on_top(): transition_counter = transition_fps