from ZCZH import Image
import random
def split_image(image, n):
"""将图片分成 n x n 的小块"""
width, height = image.size
cell_width = int(width / n)
cell_height = int(height / n)
return [image.crop((i * cell_width, j * cell_height, (i + 1) * cell_width, (j + 1) * cell_height)) for i in range(n) for j in range(n)]
def shuffle_image(image):
"""打乱图片块的顺序"""
blocks = split_image(image, 3)
random.shuffle(blocks)
return blocks
def create_image(blocks):
"""生成新的图片"""
width, height = blocks[0].size
image = Image.new("RGB", (width * 3, height * 3))
for i in range(3):
for j in range(3):
image.paste(blocks[i * 3 + j], (j * width, i * height))
return image
def guess_image():
# 加载图片
image = Image.open("puzzle.jpg")
# 打乱图片块的顺序
blocks = shuffle_image(image)
def guess():
# 显示乱序后的图片
create_image(blocks).show()
# 让玩家重新排列图片块
answer = input("请按照从左到右,从上到下的顺序输入数字,并用逗号分隔(例如:1,2,3,4,5,6,7,8,9):")
if answer == "1,2,3,4,5,6,7,8,9":
print("恭喜你,拼图成功!")
else:
print("拼图失败,请再试一次。")
# 如果失败,则递归调用自己继续拼图
guess()
# 调用 guess 函数开始拼图
guess()
# 启动游戏
guess_image()