changed the code to gesture
This commit is contained in:
parent
769aa6ce05
commit
2f30624900
163
game.py
163
game.py
|
|
@ -1,101 +1,132 @@
|
||||||
from raylibpy import *
|
from raylibpy import *
|
||||||
import math
|
import math
|
||||||
|
|
||||||
# Grid setup
|
# ---------- Constants ----------
|
||||||
ROWS = 3
|
ROWS = 3
|
||||||
COLS = 3
|
COLS = 3
|
||||||
TILE_SIZE = 120
|
TILE_SIZE = 120
|
||||||
MARGIN = 20
|
MARGIN = 20
|
||||||
SWIPE_MIN_DISTANCE = 30.0 # Minimum distance to detect swipe
|
|
||||||
|
|
||||||
# Board: 0 means empty tile, others are numbered
|
# Animation settings
|
||||||
board = []
|
ANIM_SPEED = 0.3 # interpolation speed
|
||||||
|
FPS = 60
|
||||||
|
|
||||||
|
# ---------- Tile Class ----------
|
||||||
|
class Tile:
|
||||||
|
def __init__(self, value, row, col):
|
||||||
|
self.value = value
|
||||||
|
self.row = row
|
||||||
|
self.col = col
|
||||||
|
self.x = MARGIN + col * TILE_SIZE
|
||||||
|
self.y = MARGIN + row * TILE_SIZE
|
||||||
|
self.target_row = row
|
||||||
|
self.target_col = col
|
||||||
|
|
||||||
|
def update(self):
|
||||||
|
target_x = MARGIN + self.target_col * TILE_SIZE
|
||||||
|
target_y = MARGIN + self.target_row * TILE_SIZE
|
||||||
|
# Smooth interpolation
|
||||||
|
self.x += (target_x - self.x) * ANIM_SPEED * FPS / 60
|
||||||
|
self.y += (target_y - self.y) * ANIM_SPEED * FPS / 60
|
||||||
|
|
||||||
|
def draw(self):
|
||||||
|
color = LIGHTGRAY if self.value != 0 else GRAY
|
||||||
|
draw_rectangle(int(self.x), int(self.y), TILE_SIZE - 4, TILE_SIZE - 4, color)
|
||||||
|
if self.value != 0:
|
||||||
|
text = str(self.value).encode('utf-8')
|
||||||
|
tw = measure_text(text, 30)
|
||||||
|
draw_text(text, int(self.x + TILE_SIZE / 2 - tw / 2),
|
||||||
|
int(self.y + TILE_SIZE / 2 - 15), 30, BLACK)
|
||||||
|
|
||||||
|
# ---------- Board ----------
|
||||||
|
tiles = []
|
||||||
|
|
||||||
def init_board():
|
def init_board():
|
||||||
global board
|
global tiles
|
||||||
board = [i + 1 for i in range(ROWS * COLS - 1)] + [0] # 1..8 + 0 empty
|
tiles = []
|
||||||
|
n = ROWS * COLS
|
||||||
def idx_row(idx):
|
values = [i + 1 for i in range(n - 1)] + [0]
|
||||||
return idx // COLS
|
for idx, val in enumerate(values):
|
||||||
|
r = idx // COLS
|
||||||
def idx_col(idx):
|
c = idx % COLS
|
||||||
return idx % COLS
|
tiles.append(Tile(val, r, c))
|
||||||
|
|
||||||
def draw_board():
|
|
||||||
for i, value in enumerate(board):
|
|
||||||
r, c = idx_row(i), idx_col(i)
|
|
||||||
x = MARGIN + c * TILE_SIZE
|
|
||||||
y = MARGIN + r * TILE_SIZE
|
|
||||||
if value == 0:
|
|
||||||
draw_rectangle(x, y, TILE_SIZE - 4, TILE_SIZE - 4, DARKGRAY)
|
|
||||||
else:
|
|
||||||
draw_rectangle(x, y, TILE_SIZE - 4, TILE_SIZE - 4, LIGHTGRAY)
|
|
||||||
draw_text(str(value), x + TILE_SIZE // 2 - 10, y + TILE_SIZE // 2 - 12, 24, BLACK)
|
|
||||||
|
|
||||||
def find_empty():
|
def find_empty():
|
||||||
return board.index(0)
|
for idx, t in enumerate(tiles):
|
||||||
|
if t.value == 0:
|
||||||
|
return idx
|
||||||
|
return -1
|
||||||
|
|
||||||
|
def swap_tiles(tile1, tile2):
|
||||||
|
tile1.target_row, tile1.target_col, tile2.target_row, tile2.target_col = tile2.target_row, tile2.target_col, tile1.target_row, tile1.target_col
|
||||||
|
tile1.row, tile1.col, tile2.row, tile2.col = tile2.row, tile2.col, tile1.row, tile1.col
|
||||||
|
|
||||||
def move_empty(direction):
|
def move_empty(direction):
|
||||||
empty = find_empty()
|
empty_idx = find_empty()
|
||||||
er, ec = idx_row(empty), idx_col(empty)
|
er = tiles[empty_idx].row
|
||||||
|
ec = tiles[empty_idx].col
|
||||||
tr, tc = er, ec
|
tr, tc = er, ec
|
||||||
|
if direction == 0: tr -= 1 # Up
|
||||||
if direction == 0: tr = er - 1 # up
|
elif direction == 1: tr += 1 # Down
|
||||||
elif direction == 1: tr = er + 1 # down
|
elif direction == 2: tc -= 1 # Left
|
||||||
elif direction == 2: tc = ec - 1 # left
|
elif direction == 3: tc += 1 # Right
|
||||||
elif direction == 3: tc = ec + 1 # right
|
|
||||||
|
|
||||||
if tr < 0 or tr >= ROWS or tc < 0 or tc >= COLS:
|
if tr < 0 or tr >= ROWS or tc < 0 or tc >= COLS:
|
||||||
return # out of bounds
|
return
|
||||||
|
# Find target tile
|
||||||
target = tr * COLS + tc
|
for t in tiles:
|
||||||
board[empty], board[target] = board[target], board[empty]
|
if t.row == tr and t.col == tc:
|
||||||
|
swap_tiles(t, tiles[empty_idx])
|
||||||
|
break
|
||||||
|
|
||||||
|
# ---------- Main Loop ----------
|
||||||
def main():
|
def main():
|
||||||
screenW = MARGIN * 2 + COLS * TILE_SIZE
|
screenW = MARGIN * 2 + COLS * TILE_SIZE
|
||||||
screenH = MARGIN * 2 + ROWS * TILE_SIZE
|
screenH = MARGIN * 2 + ROWS * TILE_SIZE
|
||||||
|
init_window(screenW, screenH, b"Ultra Smooth Puzzle with Gestures")
|
||||||
init_window(screenW, screenH, b"Puzzle Swipe Test (Python)")
|
set_target_fps(FPS)
|
||||||
set_target_fps(60)
|
|
||||||
|
|
||||||
init_board()
|
init_board()
|
||||||
|
|
||||||
# Swipe variables
|
swipe_start = None
|
||||||
touch_start = Vector2(0, 0)
|
|
||||||
touch_end = Vector2(0, 0)
|
|
||||||
touching = False
|
|
||||||
|
|
||||||
while not window_should_close():
|
while not window_should_close():
|
||||||
# Detect touch or mouse drag
|
pos = get_mouse_position()
|
||||||
|
|
||||||
|
# ---------- Mouse swipe detection ----------
|
||||||
if is_mouse_button_pressed(MOUSE_LEFT_BUTTON):
|
if is_mouse_button_pressed(MOUSE_LEFT_BUTTON):
|
||||||
touching = True
|
swipe_start = pos
|
||||||
touch_start = get_mouse_position()
|
|
||||||
|
|
||||||
if touching and is_mouse_button_released(MOUSE_LEFT_BUTTON):
|
if is_mouse_button_released(MOUSE_LEFT_BUTTON) and swipe_start:
|
||||||
touching = False
|
swipe_end = pos
|
||||||
touch_end = get_mouse_position()
|
dx = swipe_end.x - swipe_start.x
|
||||||
|
dy = swipe_end.y - swipe_start.y
|
||||||
dx = touch_end.x - touch_start.x
|
|
||||||
dy = touch_end.y - touch_start.y
|
|
||||||
dist = math.sqrt(dx * dx + dy * dy)
|
dist = math.sqrt(dx * dx + dy * dy)
|
||||||
|
if dist >= 8.0:
|
||||||
if dist >= SWIPE_MIN_DISTANCE:
|
|
||||||
# Determine swipe direction
|
|
||||||
if abs(dx) > abs(dy):
|
if abs(dx) > abs(dy):
|
||||||
if dx > 0:
|
move_empty(3 if dx > 0 else 2)
|
||||||
move_empty(3) # right
|
|
||||||
else:
|
|
||||||
move_empty(2) # left
|
|
||||||
else:
|
else:
|
||||||
if dy > 0:
|
move_empty(1 if dy > 0 else 0)
|
||||||
move_empty(1) # down
|
swipe_start = None
|
||||||
else:
|
|
||||||
move_empty(0) # up
|
|
||||||
|
|
||||||
|
# ---------- Gesture detection ----------
|
||||||
|
if is_gesture_detected(GESTURE_SWIPE_RIGHT):
|
||||||
|
move_empty(3)
|
||||||
|
elif is_gesture_detected(GESTURE_SWIPE_LEFT):
|
||||||
|
move_empty(2)
|
||||||
|
elif is_gesture_detected(GESTURE_SWIPE_UP):
|
||||||
|
move_empty(0)
|
||||||
|
elif is_gesture_detected(GESTURE_SWIPE_DOWN):
|
||||||
|
move_empty(1)
|
||||||
|
|
||||||
|
# ---------- Update tiles ----------
|
||||||
|
for t in tiles:
|
||||||
|
t.update()
|
||||||
|
|
||||||
|
# ---------- Draw ----------
|
||||||
begin_drawing()
|
begin_drawing()
|
||||||
clear_background(RAYWHITE)
|
clear_background(RAYWHITE)
|
||||||
draw_text(b"Swipe or drag to move empty tile", 10, 10, 18, DARKGRAY)
|
draw_text(b"Swipe tiles! (Mouse or Gesture)", 10, 10, 18, DARKGRAY)
|
||||||
draw_board()
|
for t in tiles:
|
||||||
|
t.draw()
|
||||||
end_drawing()
|
end_drawing()
|
||||||
|
|
||||||
close_window()
|
close_window()
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user