From 2f306249003062f0da140d640e9a6a186169ab65 Mon Sep 17 00:00:00 2001 From: Visvamitha Date: Thu, 9 Oct 2025 22:48:26 +0530 Subject: [PATCH] changed the code to gesture --- game.py | 163 +++++++++++++++++++++++++++++++++----------------------- 1 file changed, 97 insertions(+), 66 deletions(-) diff --git a/game.py b/game.py index 73a292fe2..eb0f668bd 100644 --- a/game.py +++ b/game.py @@ -1,101 +1,132 @@ from raylibpy import * import math -# Grid setup +# ---------- Constants ---------- ROWS = 3 COLS = 3 TILE_SIZE = 120 MARGIN = 20 -SWIPE_MIN_DISTANCE = 30.0 # Minimum distance to detect swipe -# Board: 0 means empty tile, others are numbered -board = [] +# Animation settings +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(): - global board - board = [i + 1 for i in range(ROWS * COLS - 1)] + [0] # 1..8 + 0 empty - -def idx_row(idx): - return idx // COLS - -def idx_col(idx): - return idx % COLS - -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) + global tiles + tiles = [] + n = ROWS * COLS + values = [i + 1 for i in range(n - 1)] + [0] + for idx, val in enumerate(values): + r = idx // COLS + c = idx % COLS + tiles.append(Tile(val, r, c)) 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): - empty = find_empty() - er, ec = idx_row(empty), idx_col(empty) + empty_idx = find_empty() + er = tiles[empty_idx].row + ec = tiles[empty_idx].col tr, tc = er, ec - - if direction == 0: tr = er - 1 # up - elif direction == 1: tr = er + 1 # down - elif direction == 2: tc = ec - 1 # left - elif direction == 3: tc = ec + 1 # right - + if direction == 0: tr -= 1 # Up + elif direction == 1: tr += 1 # Down + elif direction == 2: tc -= 1 # Left + elif direction == 3: tc += 1 # Right if tr < 0 or tr >= ROWS or tc < 0 or tc >= COLS: - return # out of bounds - - target = tr * COLS + tc - board[empty], board[target] = board[target], board[empty] + return + # Find target tile + for t in tiles: + if t.row == tr and t.col == tc: + swap_tiles(t, tiles[empty_idx]) + break +# ---------- Main Loop ---------- def main(): screenW = MARGIN * 2 + COLS * TILE_SIZE screenH = MARGIN * 2 + ROWS * TILE_SIZE - - init_window(screenW, screenH, b"Puzzle Swipe Test (Python)") - set_target_fps(60) - + init_window(screenW, screenH, b"Ultra Smooth Puzzle with Gestures") + set_target_fps(FPS) init_board() - # Swipe variables - touch_start = Vector2(0, 0) - touch_end = Vector2(0, 0) - touching = False + swipe_start = None 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): - touching = True - touch_start = get_mouse_position() + swipe_start = pos - if touching and is_mouse_button_released(MOUSE_LEFT_BUTTON): - touching = False - touch_end = get_mouse_position() - - dx = touch_end.x - touch_start.x - dy = touch_end.y - touch_start.y + if is_mouse_button_released(MOUSE_LEFT_BUTTON) and swipe_start: + swipe_end = pos + dx = swipe_end.x - swipe_start.x + dy = swipe_end.y - swipe_start.y dist = math.sqrt(dx * dx + dy * dy) - - if dist >= SWIPE_MIN_DISTANCE: - # Determine swipe direction + if dist >= 8.0: if abs(dx) > abs(dy): - if dx > 0: - move_empty(3) # right - else: - move_empty(2) # left + move_empty(3 if dx > 0 else 2) else: - if dy > 0: - move_empty(1) # down - else: - move_empty(0) # up + move_empty(1 if dy > 0 else 0) + swipe_start = None + # ---------- 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() clear_background(RAYWHITE) - draw_text(b"Swipe or drag to move empty tile", 10, 10, 18, DARKGRAY) - draw_board() + draw_text(b"Swipe tiles! (Mouse or Gesture)", 10, 10, 18, DARKGRAY) + for t in tiles: + t.draw() end_drawing() close_window()