以下是使用Pygame编写一定有解的九宫格拼图游戏的代码。并使用Tkinter设计界面,包含开始游戏和自动复原的两个不重叠的按钮。
import pygame
from pygame.locals import *
import random
import tkinter as tk
from tkinter import messagebox
# 定义九宫格拼图的尺寸和参数
WIDTH, HEIGHT = 400, 400
BOARD_SIZE = 3
TILE_SIZE = WIDTH // BOARD_SIZE
# 定义拼图块的颜色
TILE_COLOR = (255, 255, 255)
# 定义初始状态和目标状态
INITIAL_STATE = [[1, 2, 3], [4, 5, 6], [7, 8, 0]]
GOAL_STATE = [[1, 2, 3], [4, 5, 6], [7, 8, 0]]
# 初始化Pygame
pygame.init()
# 创建游戏窗口
window = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Sliding Puzzle")
# 创建按钮
button_font = pygame.font.SysFont("Arial", 20)
start_button = button_font.render("开始游戏", True, (0, 0, 0))
auto_solve_button = button_font.render("自动复原", True, (0, 0, 0))
start_button_rect = start_button.get_rect(center=(WIDTH // 4, HEIGHT - 30))
auto_solve_button_rect = auto_solve_button.get_rect(center=(WIDTH * 3 // 4, HEIGHT - 30))
# 初始化状态
current_state = [row[:] for row in INITIAL_STATE]
def draw_board():
"""绘制游戏界面"""
# 绘制背景
window.fill((255, 255, 255))
# 绘制拼图块
for i in range(BOARD_SIZE):
for j in range(BOARD_SIZE):
tile = pygame.Rect(j * TILE_SIZE, i * TILE_SIZE, TILE_SIZE, TILE_SIZE)
if current_state[i][j] != 0:
pygame.draw.rect(window, TILE_COLOR, tile)
num_text = button_font.render(str(current_state[i][j]), True, (0, 0, 0))
num_text_rect = num_text.get_rect(center=tile.center)
window.blit(num_text, num_text_rect)
# 绘制按钮
pygame.draw.rect(window, (192, 192, 192), start_button_rect)
pygame.draw.rect(window, (192, 192, 192), auto_solve_button_rect)
window.blit(start_button, start_button_rect)
window.blit(auto_solve_button, auto_solve_button_rect)
# 刷新窗口
pygame.display.flip()
def shuffle_board():
"""打乱游戏拼图"""
for _ in range(100):
possible_moves = get_possible_moves()
move = random.choice(possible_moves)
move_tile(move)
def move_tile(move):
"""移动拼图块"""
x, y = get_blank_position()
dx, dy = move
new_x, new_y = x + dx, y + dy
current_state[x][y], current_state[new_x][new_y] = current_state[new_x][new_y], current_state[x][y]
def get_blank_position():
"""返回空白块(0)的位置"""
for i in range(BOARD_SIZE):
for j in range(BOARD_SIZE):
if current_state[i][j] == 0:
return i, j
def get_possible_moves():
"""返回可以移动的方向"""
x, y = get_blank_position()
moves = []
if x > 0:
moves.append((-1, 0)) # 上
if x < BOARD_SIZE - 1:
moves.append((1, 0)) # 下
if y > 0:
moves.append((0, -1)) # 左
if y < BOARD_SIZE - 1:
moves.append((0, 1)) # 右
return moves
def is_goal_state():
"""检查是否达到目标状态"""
return current_state == GOAL_STATE
def bfs():
"""宽度优先搜索解决拼图问题"""
queue = [(current_state, [])]
visited = set()
while queue:
state, path = queue.pop(0)
visited.add(str(state))
if is_goal_state():
return path
possible_moves = get_possible_moves()
for move in possible_moves:
new_state = [row[:] for row in state]
move_tile(move)
if not str(new_state) in visited:
queue.append((new_state, path + [move]))
# 恢复状态
current_state = [row[:] for row in state]
def solve_puzzle():
"""自动复原拼图"""
path = bfs()
for move in path:
move_tile(move)
draw_board()
pygame.time.wait(300)
def start_game():
"""开始游戏"""
shuffle_board()
draw_board()
def handle_mouse_click(position):
"""处理鼠标点击事件"""
x, y = position
if start_button_rect.collidepoint(position):
start_game()
if auto_solve_button_rect.collidepoint(position):
solve_puzzle()
def main():
running = True
while running:
draw_board()
for event in pygame.event.get():
if event.type == QUIT:
running = False
if event.type == MOUSEBUTTONDOWN:
handle_mouse_click(pygame.mouse.get_pos())
pygame.quit()
# Tkinter界面
root = tk.Tk()
root.title("Sliding Puzzle")
# 游戏信息标签
info_label = tk.Label(root, text="点击'开始游戏'以开始游戏," + "点击'自动复原'以自动复原拼图")
info_label.pack()
# 开始游戏按钮
start_button_tk = tk.Button(root, text="开始游戏", command=start_game)
start_button_tk.pack(pady=10)
# 自动复原按钮
auto_solve_button_tk = tk.Button(root, text="自动复原", command=solve_puzzle)
auto_solve_button_tk.pack(pady=10)
root.mainloop()
if __name__ == "__main__":
main()
上述代码中,我们使用Tkinter创建了一个游戏界面,包含了开始游戏和自动复原按钮。点击"开始游戏"按钮会打乱拼图并开始游戏,点击"自动复原"按钮会自动复原拼图。游戏界面使用Pygame进行绘制和交互。可以在Tkinter界面中修改按钮的文本、颜色和布局来满足你实际的需求。