吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1661|回复: 19
收起左侧

[讨论] 新手用python写了一个2048小游戏,该怎么继续美化界面

  [复制链接]
suneryzgg123 发表于 2023-6-20 10:23
[Python] 纯文本查看 复制代码
import pygame
import random
import copy

# 游戏参数
BOARD_SIZE = 4  # 棋盘大小
TILE_SIZE = 80  # 每个格子大小
SCORE_FONT_SIZE = 30  # 分数字体大小

# 配色方案
BACKGROUND_COLOR = (250, 248, 239)
TILE_COLORS = {
    0: (205, 193, 180),
    2: (238, 228, 218),
    4: (237, 224, 200),
    8: (242, 177, 121),
    16: (245, 149, 99),
    32: (246, 124, 95),
    64: (246, 94, 59),
    128: (237, 207, 114),
    256: (237, 204, 97),
    512: (237, 200, 80),
    1024: (237, 197, 63),
    2048: (237, 194, 46),
    'other': (60, 58, 50)
}
TEXT_COLOR = (119, 110, 101)

# 初始化Pygame
pygame.init()
pygame.display.set_caption('2048')

# 创建窗口
screen = pygame.display.set_mode((
    BOARD_SIZE * TILE_SIZE,
    BOARD_SIZE * TILE_SIZE
))

# 加载字体
FONT = pygame.font.Font(None, 40)
SCORE_FONT = pygame.font.Font(None, SCORE_FONT_SIZE)


def draw_board(board, score):
    """
    在屏幕上绘制棋盘,包括所有已经有数字的方块。
    """
    for i in range(BOARD_SIZE):
        for j in range(BOARD_SIZE):
            value = board[i][j]
            # 绘制每个格子
            pygame.draw.rect(
                screen, TILE_COLORS[value if value in TILE_COLORS else 'other'],
                (j * TILE_SIZE, i * TILE_SIZE, TILE_SIZE, TILE_SIZE)
            )
            # 绘制方块中的数字
            if value:
                text_surface = FONT.render(
                    str(value), True, TEXT_COLOR
                )
                rect = text_surface.get_rect()
                rect.center = (
                    (j+0.5) * TILE_SIZE,
                    (i+0.5) * TILE_SIZE
                )
                screen.blit(text_surface, rect)

    # 绘制分数
    score_text_surface = SCORE_FONT.render(
        f'Score: {score}', True, TEXT_COLOR
    )
    score_rect = score_text_surface.get_rect()
    score_rect.bottomright = (
        BOARD_SIZE * TILE_SIZE - 10,
        BOARD_SIZE * TILE_SIZE - 10
    )
    screen.blit(score_text_surface, score_rect)

    pygame.display.update()


def get_random_location(board):
    """
    从棋盘中随机选择一个空位置(值为0)。
    """
    empty_locations = []
    for i in range(BOARD_SIZE):
        for j in range(BOARD_SIZE):
            if board[i][j] == 0:
                empty_locations.append((i, j))
    if empty_locations:
        return random.choice(empty_locations)
    else:
        return None


def add_random_tile(board):
    """
    在棋盘上随机添加一个2或4。
    """
    location = get_random_location(board)
    if location:
        i, j = location
        board[i][j] = random.choice([2, 4])
    return board


def transpose_board(board):
    """
    矩阵转置。
    """
    return [list(row) for row in zip(*board)]


def reverse_rows(board):
    """
    翻转每一行。
    """
    return [row[::-1] for row in board]


def merge_tiles(row, score):
    """
    将一行里的所有方块合并。
    """
    new_row = [0] * BOARD_SIZE
    index = 0
    for i in range(BOARD_SIZE):
        if row[i] != 0:
            if new_row[index] == 0:
                new_row[index] = row[i]
            elif new_row[index] == row[i]:
                new_row[index] *= 2
                score += new_row[index]
                index += 1
            else:
                index += 1
                new_row[index] = row[i]
    return new_row, score


def move_up(board, score):
    """
    将棋盘上的数字全部往上移动。
    """
    transposed_board = transpose_board(board)
    new_board = []
    for row in transposed_board:
        new_row, score = merge_tiles(row, score)
        new_board.append(new_row)
    new_board = transpose_board(new_board)
    return new_board, score


def move_down(board, score):
    """
    将棋盘上的数字全部往下移动。
    """
    transposed_board = transpose_board(board)
    reversed_board = reverse_rows(transposed_board)
    new_board = []
    for row in reversed_board:
        new_row, score = merge_tiles(row, score)
        new_board.append(new_row)
    new_board = reverse_rows(new_board)
    new_board = transpose_board(new_board)
    return new_board, score


def move_left(board, score):
    """
    将棋盘上的数字全部往左移动。
    """
    new_board = []
    for row in board:
        new_row, score = merge_tiles(row, score)
        new_board.append(new_row)
    return new_board, score


def move_right(board, score):
    """
    将棋盘上的数字全部往右移动。
    """
    reversed_board = reverse_rows(board)
    new_board = []
    for row in reversed_board:
        new_row, score = merge_tiles(row, score)
        new_board.append(new_row)
    new_board = reverse_rows(new_board)
    return new_board, score


def is_game_over(board):
    """
    检查游戏是否结束。
    """
    for i in range(BOARD_SIZE):
        for j in range(BOARD_SIZE):
            if board[i][j] == 0:
                # 如果还有空位置,则游戏未结束
                return False
            elif j < BOARD_SIZE - 1 and board[i][j] == board[i][j+1]:
                # 如果存在相邻两个方块相等,则游戏未结束
                return False
            elif i < BOARD_SIZE - 1 and board[i][j] == board[i+1][j]:
                # 如果存在相邻两个方块相等,则游戏未结束
                return False
    return True


def game_loop():
    """
    游戏主循环。
    """
    board = [[0 for _ in range(BOARD_SIZE)] for _ in range(BOARD_SIZE)]
    score = 0
    board = add_random_tile(board)
    board = add_random_tile(board)
    draw_board(board, score)

    while True:
        events = pygame.event.get()
        for event in events:
            if event.type == pygame.QUIT:
                pygame.quit()
                return

            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_UP:
                    board_new, score = move_up(board, score)
                elif event.key == pygame.K_DOWN:
                    board_new, score = move_down(board, score)
                elif event.key == pygame.K_LEFT:
                    board_new, score = move_left(board, score)
                elif event.key == pygame.K_RIGHT:
                    board_new, score = move_right(board, score)
                else:
                    continue

                if board != board_new:
                    # 如果棋盘有更新,则添加新的方块并更新分数
                    board = board_new
                    board = add_random_tile(board)
                    draw_board(board, score)

                    if is_game_over(board):
                        # 如果游戏结束,则打印结束信息并重新开始游戏
                        print('Game over!')
                        board = [[0 for _ in range(BOARD_SIZE)] for _ in range(BOARD_SIZE)]
                        score = 0
                        board = add_random_tile(board)
                        board = add_random_tile(board)
                        draw_board(board, score)


if __name__ == '__main__':
    game_loop()

免费评分

参与人数 1吾爱币 +1 热心值 +1 收起 理由
SproutL + 1 + 1 谢谢@Thanks!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

52soft 发表于 2023-6-20 10:38
高手,学习
yearnth 发表于 2023-6-20 10:44
51cbb 发表于 2023-6-20 10:45
IsMe9666 发表于 2023-6-20 11:17
No module named 'pygame'
wuxin9749 发表于 2023-6-20 11:18
运行了一下 牛逼
liyanna1546 发表于 2023-6-20 12:27
运行正常,太帅了
wdongdt 发表于 2023-6-20 12:40
学习,谢谢分享
忘記優慯 发表于 2023-6-20 12:47
IsMe9666 发表于 2023-6-20 11:17
No module named 'pygame'

我开始运行的时候,也是样的,安装 'pygame' 依赖就可以成功运行了
csf2022 发表于 2023-6-20 15:03
学习了 学习了
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止回复与主题无关非技术内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-5-10 16:51

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表