pip install pillow
puzzle.py
import tkinter as tk
from PIL import Image, ImageTk
import random
GRID_SIZE = 3 # 3x3 puzzle
TILE_SIZE = 100 # Pixel size for each tile
IMAGE_PATH = "sample.jpg" # Replace with your image path
class PuzzleGame:
def __init__(self, root):
self.root = root
self.root.title("🧩 Image Puzzle Game")
self.image = Image.open(IMAGE_PATH)
self.image = self.image.resize((TILE_SIZE * GRID_SIZE, TILE_SIZE * GRID_SIZE))
self.tiles = []
self.tile_images = []
self.buttons = []
self.empty_pos = (GRID_SIZE - 1, GRID_SIZE - 1)
self.create_tiles()
self.shuffle_tiles()
self.draw_tiles()
def create_tiles(self):
for row in range(GRID_SIZE):
row_tiles = []
for col in range(GRID_SIZE):
if (row, col) == self.empty_pos:
row_tiles.append(None)
continue
left = col * TILE_SIZE
upper = row * TILE_SIZE
right = left + TILE_SIZE
lower = upper + TILE_SIZE
tile = self.image.crop((left, upper, right, lower))
tk_img = ImageTk.PhotoImage(tile)
self.tile_images.append(tk_img)
row_tiles.append(tk_img)
self.tiles.append(row_tiles)
def shuffle_tiles(self):
# Flatten and shuffle the tiles
flat_tiles = [tile for row in self.tiles for tile in row if tile is not None]
random.shuffle(flat_tiles)
idx = 0
for row in range(GRID_SIZE):
for col in range(GRID_SIZE):
if (row, col) == self.empty_pos:
continue
self.tiles[row][col] = flat_tiles[idx]
idx += 1
def draw_tiles(self):
for row in range(GRID_SIZE):
for col in range(GRID_SIZE):
btn = tk.Button(self.root, image=self.tiles[row][col],
command=lambda r=row, c=col: self.move_tile(r, c))
btn.grid(row=row, column=col)
self.buttons.append(btn)
def move_tile(self, r, c):
er, ec = self.empty_pos
if abs(er - r) + abs(ec - c) == 1: # Must be adjacent
# Swap tiles
self.tiles[er][ec], self.tiles[r][c] = self.tiles[r][c], self.tiles[er][ec]
self.empty_pos = (r, c)
self.update_tiles()
def update_tiles(self):
for row in range(GRID_SIZE):
for col in range(GRID_SIZE):
btn = self.root.grid_slaves(row=row, column=col)[0]
img = self.tiles[row][col]
if img:
btn.config(image=img)
else:
btn.config(image='', text='')
if __name__ == "__main__":
root = tk.Tk()
game = PuzzleGame(root)
root.mainloop()
No comments:
Post a Comment