Pure python code to quickly implement a simple digital Huarong Road game with interface

        Number Huarong Road is a classic puzzle game that aims to arrange the number squares in the correct order by moving their positions. The game board consists of a 4x4 grid containing numbered squares numbered 1 to 15, as well as a blank square.

        When the game starts, the number squares are randomly shuffled and filled into the squares. Players need to use the position of the blank squares and gradually arrange them in order from small to large by exchanging the positions of the numbered squares. Players can use the up, down, left, and right arrow keys on the keyboard to move the number square to the position of the blank square.

        The player's goal is to complete the game in the shortest number of steps by arranging all the numbered squares in the correct order in the grid. The difficulty of the game depends on the initial shuffling of the digital blocks, and in some cases a complex series of moves may be required to complete the game.

        "Digital Huarong Road" is a simple yet challenging game that can exercise players' logical thinking, spatial cognition and problem-solving abilities. It is also a common intelligence competition event and is widely used in education and entertainment fields.

The main dependent library is tkinter. The complete source code is as follows:

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()

The running results are as follows. Just use the keyboard arrow keys to move the center block without numbers:



You can also refer to my other article on quickly implementing digital Huarongdao games, which mainly relies on the PyQt5 library:

Python quickly implements digital Huarongdao game_Huarongdao program_ASS-ASH's blog-CSDN blog

The amount of code for this has approximately doubled (89->165), but the beauty of the game interface has been improved to a certain extent.

Guess you like

Origin blog.csdn.net/qq_38563206/article/details/134338127