better touch function
This commit is contained in:
16
main.py
16
main.py
@@ -3,6 +3,8 @@ import pygame
|
||||
import random
|
||||
import sys
|
||||
import os
|
||||
import time
|
||||
|
||||
|
||||
from pygame import FULLSCREEN
|
||||
|
||||
@@ -48,6 +50,7 @@ state = 1 # PLAYING
|
||||
prepared_form = None
|
||||
ready_to_submit = False
|
||||
touch_start = None
|
||||
touch_start_time = None
|
||||
|
||||
def init_sounds():
|
||||
global sound_fail, sound_place, sound_roll, sound_tut1, sound_tut2, sound_tuuut, zip_sound
|
||||
@@ -71,25 +74,28 @@ def init_sounds():
|
||||
case_images = {}
|
||||
|
||||
def is_SPACE_event(event):
|
||||
global touch_start
|
||||
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
|
||||
|
||||
# Touch (Tap)
|
||||
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]
|
||||
distance = (dx ** 2 + dy ** 2) ** 0.5
|
||||
touch_start = None
|
||||
|
||||
if distance < 50: # Kurzer Tap = Snap
|
||||
touch_start = None
|
||||
touch_start_time = None
|
||||
|
||||
if distance < 30 and duration < 0.3: # 30 px und 300 ms
|
||||
return True
|
||||
|
||||
# Mausklick (nur Linksklick, für Desktop-Tests)
|
||||
@@ -667,7 +673,7 @@ 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 is_SPACE_event(event):
|
||||
elif event.type == pygame.KEYDOWN or event.type == pygame.JOYBUTTONDOWN or event.type == pygame.JOYHATMOTION or event.type == pygame.FINGERDOWN:
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user