编写一个python代码,利用Gui的 tkinter库设计一个4*4的华容道游戏。 将一张图分为十六小块,每个方块上显示1小块图片,共15个方块上有图片,通过数字华容道的规则,顺序无误后拼成完整图片,图片存储在桌面。
时间: 2024-05-16 18:15:43 浏览: 83
Tkinter基本入门文字教程(详细介绍Tkinter各项例程)
好的,我可以为您提供代码实现。 请注意,由于我是一个AI语言模型,无法在本地访问您的桌面,因此在此代码中,我将使用一张名为 "hua.jpg" 的示例图像。
```
import tkinter as tk
from tkinter import messagebox
from PIL import Image, ImageTk
import random
class HuaRongDao:
def __init__(self, master):
self.master = master
self.master.title("华容道")
self.master.resizable(False, False)
self.master.geometry("400x400")
self.image = Image.open("hua.jpg")
self.pieces = []
self.empty_piece = None
self.piece_size = (self.image.width // 4, self.image.height // 4)
self.create_widgets()
self.cut_image()
self.shuffle()
def create_widgets(self):
self.canvas = tk.Canvas(self.master, width=self.image.width, height=self.image.height)
self.canvas.pack()
self.button_shuffle = tk.Button(self.master, text="重新开始", command=self.shuffle)
self.button_shuffle.pack()
def cut_image(self):
for i in range(4):
for j in range(4):
x = j * self.piece_size[0]
y = i * self.piece_size[1]
piece = self.image.crop((x, y, x+self.piece_size[0], y+self.piece_size[1]))
if i == 3 and j == 3:
self.empty_piece = {"piece": None, "position": (i, j)}
else:
self.pieces.append({"piece": piece, "position": (i, j)})
def shuffle(self):
random.shuffle(self.pieces)
for i, piece in enumerate(self.pieces):
position = (i // 4, i % 4)
piece["position"] = position
self.draw_piece(piece, position)
self.empty_piece["position"] = (3, 3)
self.canvas.delete("empty")
self.canvas.create_rectangle(3 * self.piece_size[0], 3 * self.piece_size[1], 4 * self.piece_size[0], 4 * self.piece_size[1], fill="white", tags="empty")
def draw_piece(self, piece, position):
x0 = position[1] * self.piece_size[0]
y0 = position[0] * self.piece_size[1]
x1 = x0 + self.piece_size[0]
y1 = y0 + self.piece_size[1]
image = ImageTk.PhotoImage(piece["piece"])
self.canvas.create_image(x0, y0, image=image, anchor="nw", tags="piece")
self.canvas.itemconfig("piece", image=image)
self.canvas.coords("piece", x0, y0)
self.canvas.tag_bind("piece", "<Button-1>", lambda event, arg=piece: self.move_piece(arg))
def move_piece(self, piece):
position = piece["position"]
empty_position = self.empty_piece["position"]
if position[0] == empty_position[0] and abs(position[1] - empty_position[1]) == 1:
self.swap_pieces(piece, self.empty_piece)
elif position[1] == empty_position[1] and abs(position[0] - empty_position[0]) == 1:
self.swap_pieces(piece, self.empty_piece)
if all([piece["position"] == (i // 4, i % 4) for i, piece in enumerate(self.pieces)]):
messagebox.showinfo("完成!", "你完成了一张华容道拼图!")
def swap_pieces(self, piece1, piece2):
piece1_position = piece1["position"]
piece2_position = piece2["position"]
piece1["position"] = piece2_position
piece2["position"] = piece1_position
x0 = piece1_position[1] * self.piece_size[0]
y0 = piece1_position[0] * self.piece_size[1]
self.canvas.coords("piece", x0, y0)
self.empty_piece = piece1
root = tk.Tk()
app = HuaRongDao(root)
root.mainloop()
```
运行该程序后,您将看到一个4乘4的窗口,其中包含一个华容道拼图。 您可以单击任何一块拼图,将其移动到空白区域,以此来解决整个拼图。
阅读全文