纯python代码快速实现简易带界面的数字华容道小游戏

        数字华容道是一种经典的益智游戏,旨在通过移动数字方块的位置,将它们按照正确的顺序排列。游戏板由一个4x4的方格组成,其中包含了编号为1到15的数字方块,以及一个空白方块。

        游戏开始时,数字方块被随机打乱并填充到方格中。玩家需要利用空白方块的位置,通过交换数字方块的位置,逐步将它们按照从小到大的顺序排列。玩家可以使用键盘上、下、左、右四个方向键来移动数字方块,将其移动到空白方块的位置。

        玩家的目标是在最短的步数内完成游戏,将所有数字方块按照正确的顺序排列在方格中。游戏的难度取决于数字方块的初始打乱程度,有些情况下可能需要进行一系列复杂的移动才能完成游戏。

        数字华容道是一款简单而又富有挑战性的游戏,可以锻炼玩家的逻辑思维、空间认知能力和问题解决能力。它也是一种常见的智力竞赛项目,被广泛应用于教育和娱乐领域。

主要依赖库为tkinter,完整源码如下:

import tkinter as tk
import random

# 初始化游戏板
def initialize_board():
    board = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, None]]
    return board

# 打乱游戏板
def shuffle_board(board, moves=100):
    directions = ['up', 'down', 'left', 'right']
    for _ in range(moves):
        direction = random.choice(directions)
        move(board, direction)

# 移动数字
def move(board, direction):
    i, j = get_blank_position(board)
    if direction == 'up' and i > 0:
        board[i][j], board[i-1][j] = board[i-1][j], board[i][j]
    elif direction == 'down' and i < 3:
        board[i][j], board[i+1][j] = board[i+1][j], board[i][j]
    elif direction == 'left' and j > 0:
        board[i][j], board[i][j-1] = board[i][j-1], board[i][j]
    elif direction == 'right' and j < 3:
        board[i][j], board[i][j+1] = board[i][j+1], board[i][j]

# 获取空格位置
def get_blank_position(board):
    for i in range(len(board)):
        for j in range(len(board[i])):
            if board[i][j] is None:
                return i, j

# 检查游戏是否完成
def is_game_over(board):
    return board == [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, None]]

# 更新游戏界面
def update_board():
    for i in range(4):
        for j in range(4):
            if board[i][j] is not None:
                button = buttons[board[i][j]]
                button.grid(row=i, column=j)
            else:
                buttons[None].grid(row=i, column=j)

# 处理键盘事件
def key_press(event):
    key = event.keysym.lower()
    if key == 'up':
        move(board, 'up')
    elif key == 'down':
        move(board, 'down')
    elif key == 'left':
        move(board, 'left')
    elif key == 'right':
        move(board, 'right')
    update_board()
    if is_game_over(board):
        label.config(text="恭喜你,游戏完成!", fg="green")

# 创建游戏界面
def create_gui():
    global board, buttons, label

    window = tk.Tk()
    window.title("数字华容道")

    board = initialize_board()
    shuffle_board(board)

    buttons = {}
    for i in range(1, 16):
        buttons[i] = tk.Button(window, text=str(i), width=10, height=5, command=lambda i=i: button_click(i))

    buttons[None] = tk.Button(window, text="", width=10, height=5, state=tk.DISABLED)

    update_board()

    label = tk.Label(window, text="请使用键盘上下左右方向键移动数字", fg="black")
    label.grid(row=4, columnspan=4)

    window.bind('<KeyPress>', key_press)
    window.mainloop()

if __name__ == '__main__':
    create_gui()

运行结果如下,使用键盘方向键移动没有数字的中心块即可:



也可以参考我另一篇主要依赖PyQt5库的快速实现数字华容道游戏的文章:

python快速实现数字华容道小游戏_华容道程序_ASS-ASH的博客-CSDN博客

这个的代码量相较增加了大约一倍(89->165),但游戏界面的美观度有一定提升

猜你喜欢

转载自blog.csdn.net/qq_38563206/article/details/134338127
今日推荐