better touch function
This commit is contained in:
72
main.py
72
main.py
@@ -73,30 +73,39 @@ def init_sounds():
|
|||||||
|
|
||||||
case_images = {}
|
case_images = {}
|
||||||
|
|
||||||
def is_SPACE_event(event):
|
def touch(event):
|
||||||
global touch_start, touch_start_time
|
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:
|
if event.type == pygame.FINGERDOWN:
|
||||||
touch_start = (event.x * SCREEN_WIDTH, event.y * SCREEN_HEIGHT)
|
touch_start = (event.x * SCREEN_WIDTH, event.y * SCREEN_HEIGHT)
|
||||||
touch_start_time = time.time()
|
touch_start_time = time.time()
|
||||||
|
return None
|
||||||
|
|
||||||
elif event.type == pygame.FINGERUP and touch_start:
|
elif event.type == pygame.FINGERUP and touch_start:
|
||||||
end = (event.x * SCREEN_WIDTH, event.y * SCREEN_HEIGHT)
|
end = (event.x * SCREEN_WIDTH, event.y * SCREEN_HEIGHT)
|
||||||
duration = time.time() - touch_start_time
|
duration = time.time() - touch_start_time
|
||||||
dx = end[0] - touch_start[0]
|
dx = end[0] - touch_start[0]
|
||||||
dy = end[1] - touch_start[1]
|
dy = end[1] - touch_start[1]
|
||||||
distance = (dx ** 2 + dy ** 2) ** 0.5
|
|
||||||
|
|
||||||
touch_start = None
|
touch_start = None
|
||||||
touch_start_time = None
|
touch_start_time = None
|
||||||
|
|
||||||
if distance < 30 and duration < 0.5: # 30 px und 300 ms
|
if abs(dx) < 30 and abs(dy) < 30 and duration < 0.3:
|
||||||
return True
|
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)
|
# Mausklick (nur Linksklick, für Desktop-Tests)
|
||||||
if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
|
if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
|
||||||
@@ -105,29 +114,12 @@ def is_SPACE_event(event):
|
|||||||
return False
|
return False
|
||||||
|
|
||||||
def is_LEFT_event(event):
|
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
|
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.JOYHATMOTION and event.value[0] == -1) or
|
||||||
(event.type == pygame.JOYBUTTONDOWN and event.button == 2)):
|
(event.type == pygame.JOYBUTTONDOWN and event.button == 2)):
|
||||||
return True
|
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
|
return False
|
||||||
|
|
||||||
|
|
||||||
@@ -139,22 +131,6 @@ def is_UP_event(event):
|
|||||||
(event.type == pygame.JOYBUTTONDOWN and event.button == 3)):
|
(event.type == pygame.JOYBUTTONDOWN and event.button == 3)):
|
||||||
return True
|
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
|
return False
|
||||||
|
|
||||||
async def init_game():
|
async def init_game():
|
||||||
@@ -520,7 +496,7 @@ async def show_instruction_screen(image):
|
|||||||
if event.type == pygame.QUIT:
|
if event.type == pygame.QUIT:
|
||||||
pygame.quit()
|
pygame.quit()
|
||||||
sys.exit()
|
sys.exit()
|
||||||
elif is_SPACE_event(event):
|
elif is_SPACE_event(event) or touch(event) == "snap":
|
||||||
waiting = False
|
waiting = False
|
||||||
|
|
||||||
async def fail_current_case():
|
async def fail_current_case():
|
||||||
@@ -669,7 +645,7 @@ async def show_game_over(score):
|
|||||||
if event.type == pygame.QUIT:
|
if event.type == pygame.QUIT:
|
||||||
pygame.quit()
|
pygame.quit()
|
||||||
sys.exit()
|
sys.exit()
|
||||||
elif is_SPACE_event(event):
|
elif is_SPACE_event(event) or touch(event) == "snap":
|
||||||
if ready_to_submit and prepared_form:
|
if ready_to_submit and prepared_form:
|
||||||
print("Benutzer bestätigt, Punkte werden jetzt gesendet.")
|
print("Benutzer bestätigt, Punkte werden jetzt gesendet.")
|
||||||
prepared_form.submit()
|
prepared_form.submit()
|
||||||
@@ -728,12 +704,12 @@ async def main():
|
|||||||
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 or(event.type == pygame.MOUSEBUTTONDOWN and event.button == 1):
|
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 is_LEFT_event(event):
|
if is_LEFT_event(event) or touch(event) == "ontop":
|
||||||
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 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 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
|
||||||
@@ -750,7 +726,7 @@ async def main():
|
|||||||
stacked_cases.append(current_case)
|
stacked_cases.append(current_case)
|
||||||
spawn_case_in_game()
|
spawn_case_in_game()
|
||||||
shake_timer = 10
|
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:
|
if current_case and current_case.allow_snap:
|
||||||
await current_case.animate_snap()
|
await current_case.animate_snap()
|
||||||
can_use_tilt = True
|
can_use_tilt = True
|
||||||
|
|||||||
Reference in New Issue
Block a user