其他分享
首页 > 其他分享> > 模拟2048游戏

模拟2048游戏

作者:互联网

模拟2048游戏

import random
list_merge = None

square_matrix = [
    [0, 0, 0, 0],
    [0, 0, 0, 0],
    [0, 0, 0, 0],
    [0, 0, 0, 0],
]


def zero_to_end():
    """
    将所有元素去0,依次放置,末尾添加0
    :return:
    """
    for i in range(len(list_merge) - 1, -1, -1):
        if list_merge[i] == 0:
            del list_merge[i]
            list_merge.append(0)


def add():
    """
    计算两个相邻相同元素的和
    :return:
    """
    zero_to_end()
    for i in range(1, len(list_merge)):
        if list_merge[i] == list_merge[i - 1]:
            list_merge[i - 1] += list_merge[i]
            del list_merge[i]
            list_merge.append(0)


def left_move():
    """
    左移
    :return:
    """
    for line in square_matrix:
        global list_merge
        list_merge = line
        add()


def right_move():
    """
    右移
    :return:
    """
    for line in square_matrix:
        global list_merge
        list_merge = line[::-1]
        add()
        line[::-1] = list_merge


def up_move():
    """
    上移
    :return:
    """
    square_matrix_transpose(square_matrix)
    left_move()
    square_matrix_transpose(square_matrix)


def down_move():
    """
    下移
    :return:
    """
    square_matrix_transpose(square_matrix)
    right_move()
    square_matrix_transpose(square_matrix)


def square_matrix_transpose(squ_matrix):
    """
    方阵转置
    :param squ_matrix:
    :return:
    """
    for r in range(1, len(squ_matrix)):
        for c in range(r, len(squ_matrix[r])):
            squ_matrix[c][r - 1], squ_matrix[r - 1][c] = \
                squ_matrix[r - 1][c], squ_matrix[c][r - 1]


def play_game():
    """
    执行
    :return:
    """
    create_square_matrix()
    print_square()
    while True:
        execute_square_matrix()
        run = input("请输入要进行的操作:上移[W],下移[S],左移[A],右移[D]:").title()
        print_square()
        if run == "W":
            up_move()
        if run == "S":
            down_move()
        if run == "A":
            left_move()
        if run == "D":
            right_move()


def create_square_matrix():
    """
    首次生成方阵
    :return:
    """
    r_c_index01 = random.randint(0, 3)
    r_c_index02 = random.randint(0, 3)
    r_c_index03 = random.randint(0, 3)
    r_c_index04 = random.randint(0, 3)
    index01 = random.randrange(2, 5, 2)
    index02 = random.randrange(2, 5, 2)

    if r_c_index01 != r_c_index03 or r_c_index02 != r_c_index04:
        square_matrix[r_c_index01][r_c_index02] = index01
        square_matrix[r_c_index03][r_c_index04] = index02
    return square_matrix


def execute_square_matrix():
    """
    方阵随机添加一个元素
    :return:
    """
    r_c_index01 = random.randint(0, 3)
    r_c_index02 = random.randint(0, 3)
    index01 = random.randrange(2, 5, 2)
    if square_matrix[r_c_index01][r_c_index02] == 0:
        square_matrix[r_c_index01][r_c_index02] = index01
    else:
        square_matrix[r_c_index01][r_c_index02] = square_matrix[r_c_index01][r_c_index02]
    return square_matrix


def print_square():
    """
    以矩阵的形式输出
    :return:
    """
    for i in range(0, 4):
        print(square_matrix[i])


play_game()

标签:index01,square,return,matrix,list,2048,merge,模拟,游戏
来源: https://blog.csdn.net/m0_51634934/article/details/120742412