From 05e3b842b8cef5b67e0ea835e21cf111cca3dbed Mon Sep 17 00:00:00 2001 From: pascald Date: Sat, 3 May 2025 00:12:56 +0200 Subject: [PATCH] better touch function --- main.py | 72 +++++++++++++++++++-------------------------------------- 1 file changed, 24 insertions(+), 48 deletions(-) diff --git a/main.py b/main.py index aac9740..b18604f 100644 --- a/main.py +++ b/main.py @@ -73,30 +73,39 @@ def init_sounds(): case_images = {} -def is_SPACE_event(event): +def touch(event): global touch_start, touch_start_time - # 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 - if event.type == pygame.FINGERDOWN: touch_start = (event.x * SCREEN_WIDTH, event.y * SCREEN_HEIGHT) touch_start_time = time.time() + return None 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] - distance = (dx ** 2 + dy ** 2) ** 0.5 touch_start = None touch_start_time = None - if distance < 30 and duration < 0.5: # 30 px und 300 ms - return True + if abs(dx) < 30 and abs(dy) < 30 and duration < 0.3: + return "snap" + elif dx < -50 and abs(dy) < 80 and duration > 0.3: + return "tilt" + elif dy < -50 and abs(dx) < 80 and duration > 0.3: + return "ontop" + else: + return None + return None + +def is_SPACE_event(event): + + # 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 # Mausklick (nur Linksklick, für Desktop-Tests) if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1: @@ -105,29 +114,12 @@ def is_SPACE_event(event): return False def is_LEFT_event(event): - global touch_start, touch_start_time - + # Tastatur oder Joystick links 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 @@ -139,22 +131,6 @@ def is_UP_event(event): (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(): @@ -520,7 +496,7 @@ async def show_instruction_screen(image): if event.type == pygame.QUIT: pygame.quit() sys.exit() - elif is_SPACE_event(event): + elif is_SPACE_event(event) or touch(event) == "snap": waiting = False async def fail_current_case(): @@ -669,7 +645,7 @@ async def show_game_over(score): if event.type == pygame.QUIT: pygame.quit() sys.exit() - elif is_SPACE_event(event): + elif is_SPACE_event(event) or touch(event) == "snap": if ready_to_submit and prepared_form: print("Benutzer bestätigt, Punkte werden jetzt gesendet.") prepared_form.submit() @@ -728,12 +704,12 @@ async def main(): 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 or(event.type == pygame.MOUSEBUTTONDOWN and event.button == 1): if state == 1: # PLAYING - if is_LEFT_event(event): + if is_LEFT_event(event) or touch(event) == "ontop": 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 is_UP_event(event): + elif is_UP_event(event) or touch(event) == "tilt": 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 @@ -750,7 +726,7 @@ async def main(): stacked_cases.append(current_case) spawn_case_in_game() shake_timer = 10 - elif is_SPACE_event(event): + elif is_SPACE_event(event) or touch(event) == "snap": if current_case and current_case.allow_snap: await current_case.animate_snap() can_use_tilt = True