better touch function

This commit is contained in:
2025-05-02 23:59:07 +02:00
parent da186f0dee
commit 7b59b68564

62
main.py
View File

@@ -104,6 +104,58 @@ def is_SPACE_event(event):
return False 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(): async def init_game():
global screen, clock, font, controls global screen, clock, font, controls
@@ -674,18 +726,14 @@ async def main():
for event in pygame.event.get(): for event in pygame.event.get():
if event.type == pygame.QUIT: if event.type == pygame.QUIT:
running = False 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 state == 1: # PLAYING
if ((event.type == pygame.KEYDOWN and event.key == pygame.K_LEFT) or if is_LEFT_event(event):
(event.type == pygame.JOYHATMOTION and event.value[0] == -1) or
(event.type == pygame.JOYBUTTONDOWN and event.button == 2)):
can_tip = current_case.length_units * UNIT_HEIGHT <= TRAILER_HEIGHT 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: if current_case and current_case.allow_snap and can_use_tilt and can_tip:
await current_case.animate_tip() await current_case.animate_tip()
can_use_tilt = False can_use_tilt = False
elif ((event.type == pygame.KEYDOWN and event.key == pygame.K_UP) or elif is_UP_event(event):
(event.type == pygame.JOYHATMOTION and event.value[1] == 1) or
(event.type == pygame.JOYBUTTONDOWN and event.button == 3)):
if current_case and current_case.allow_snap and can_use_on_top and current_case.can_place_on_top(): 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(): if await current_case.animate_place_on_top():
transition_counter = transition_fps transition_counter = transition_fps