闲的没事,用chatpgt弄了个小游戏,2048,利用pygame实现,无额外贴图。

只需要告诉他写个python游戏2048,只用pygame实现,不要额外贴图。然后在他暂停后说请继续,最后会有一些bug,把报错告诉他,慢慢改,10分钟就可以完成。

效果如下图:

具体代码如下:

import pygameimport random# 游戏界面的尺寸WIDTH = 400HEIGHT = 400# 游戏界面的背景颜色BACKGROUND_COLOR = (187, 173, 160)TEXT_COLOR = (119, 110, 101)GAME_OVER_COLOR = (0, 0, 0)# 格子的尺寸和间距CELL_SIZE = 80CELL_MARGIN = 10TILE_SIZE = 100TILE_MARGIN = 10GAME_OVER_FONT_SIZE = 72SIZE = 500# 数字方块的颜色和字体TILE_COLORS = {    0: (204, 192, 179),    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),    4096: (174, 207, 48)}pygame.font.init()TILE_FONT = pygame.font.SysFont("Arial", 40, bold=True)def draw_text(surface, text, color, x, y):    """    在指定位置绘制文本    """    text_surface = TILE_FONT.render(text, True, color)    text_rect = text_surface.get_rect()    text_rect.center = (x, y)    surface.blit(text_surface, text_rect)def draw_tile(surface, x, y, value):    """    在指定位置绘制数字方块    """    color = TILE_COLORS[value]    pygame.draw.rect(surface, color, (x, y, CELL_SIZE, CELL_SIZE))    if value != 0:        text_color = (255, 255, 255) if value < 8 else (119, 110, 101)        draw_text(surface, str(value), text_color, x + CELL_SIZE / 2, y + CELL_SIZE / 2)def draw_board(surface, board):    """    绘制游戏界面上的数字方块    """    for row in range(4):        for col in range(4):            x = col * CELL_SIZE + (col + 1) * CELL_MARGIN            y = row * CELL_SIZE + (row + 1) * CELL_MARGIN            draw_tile(surface, x, y, board[row][col])def generate_tile(board):    """    在空白格子中随机生成数字方块    """    available_cells = []    for row in range(4):        for col in range(4):            if board[row][col] == 0:                available_cells.append((row, col))    if available_cells:        row, col = random.choice(available_cells)        board[row][col] = 2        return True    else:        return Falsedef move_tiles(board, direction):    """    根据方向移动数字方块    """    moved = False    if direction == "left":        for row in range(4):            for col in range(1, 4):                if board[row][col] != 0:                    for k in range(col):                        if board[row][k] == 0 and no_obstacles(board, row, k, row, col):                            board[row][k] = board[row][col]                            board[row][col] = 0                            moved = True                            break                        elif board[row][k] == board[row][col] and no_obstacles(board, row, k, row, col):                            board[row][k] *= 2                            board[row][col] = 0                            moved = True                            break    elif direction == "right":        for row in range(4):            for col in range(2, -1, -1):                if board[row][col] != 0:                    for k in range(3, col, -1):                        if board[row][k] == 0 and no_obstacles(board, row, col, row, k):                            board[row][k] = board[row][col]                            board[row][col] = 0                            moved = True                            break                        elif board[row][k] == board[row][col] and no_obstacles(board, row, col, row, k):                            board[row][k] *= 2                            board[row][col] = 0                            moved = True                            break    elif direction == "up":        for col in range(4):            for row in range(1, 4):                if board[row][col] != 0:                    for k in range(row):                        if board[k][col] == 0 and no_obstacles(board, k, col, row, col):                            board[k][col] = board[row][col]                            board[row][col] = 0                            moved = True                            break                        elif board[k][col] == board[row][col] and no_obstacles(board, k, col, row, col):                            board[k][col] *= 2                            board[row][col] = 0                            moved = True                            break    elif direction == "down":        for col in range(4):            for row in range(2, -1, -1):                if board[row][col] != 0:                    for k in range(3, row, -1):                        if board[k][col] == 0 and no_obstacles(board, row, col, k, col):                            board[k][col] = board[row][col]                            board[row][col] = 0                            moved = True                            break                        elif board[k][col] == board[row][col] and no_obstacles(board, row, col, k, col):                            board[k][col] *= 2                            board[row][col] = 0                            moved = True                            break    return moveddef no_obstacles(board, row1, col1, row2, col2):    """    判断两个位置之间是否有障碍物(即是否有数字方块)    """    if row1 == row2:        for col in range(col1 + 1, col2):            if board[row1][col] != 0:                return False    elif col1 == col2:        for row in range(row1 + 1, row2):            if board[row][col1] != 0:                return False    return Truedef game_over(board):    """    判断游戏是否结束    """    for row in range(4):        for col in range(4):            if board[row][col] == 0:                return False    for row in range(4):        for col in range(3):            if board[row][col] == board[row][col + 1]:                return False    for col in range(4):        for row in range(3):            if board[row][col] == board[row + 1][col]:                return False    return Truedef add_new_tile(board):    # 查找所有空白的方格    empty_tiles = [(i, j) for i in range(4) for j in range(4) if board[i][j] == 0]    # 如果没有空白的方格,返回 False    if len(empty_tiles) == 0:        return False    # 随机选择一个空白的方格,并随机放入数字 2 或 4    row, col = random.choice(empty_tiles)    board[row][col] = random.choice([2, 4])    return Truedef get_tile_color(value):    colors = {        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),    }    return colors.get(value, (0, 0, 0))def get_font_size(value):    if value < 100:        return 64    elif value < 1000:        return 48    else:        return 36def main():    # 初始化游戏界面    pygame.init()    screen = pygame.display.set_mode((SIZE, SIZE))    pygame.display.set_caption("2048 Game")    # 初始化游戏板    board = [[0] * 4 for i in range(4)]    add_new_tile(board)    add_new_tile(board)    while True:        # 处理事件        for event in pygame.event.get():            if event.type == pygame.QUIT:                pygame.quit()                sys.exit()            elif event.type == pygame.KEYDOWN:                moved = False                if event.key == pygame.K_LEFT:                    moved = move_tiles(board, "left")                elif event.key == pygame.K_RIGHT:                    moved = move_tiles(board, "right")                elif event.key == pygame.K_UP:                    moved = move_tiles(board, "up")                elif event.key == pygame.K_DOWN:                    moved = move_tiles(board, "down")                if moved:                    add_new_tile(board)        # 绘制游戏界面        screen.fill(BACKGROUND_COLOR)        for row in range(4):            for col in range(4):                x = col * TILE_SIZE + TILE_MARGIN * (col + 1)                y = row * TILE_SIZE + TILE_MARGIN * (row + 1)                tile_color = get_tile_color(board[row][col])                pygame.draw.rect(screen, tile_color, (x, y, TILE_SIZE, TILE_SIZE))                if board[row][col] != 0:                    font_size = get_font_size(board[row][col])                    font = pygame.font.Font(None, font_size)                    text = font.render(str(board[row][col]), True, TEXT_COLOR)                    text_rect = text.get_rect()                    text_rect.center = (x + TILE_SIZE / 2, y + TILE_SIZE / 2)                    screen.blit(text, text_rect)        # 判断游戏是否结束        if game_over(board):            font = pygame.font.Font(None, GAME_OVER_FONT_SIZE)            text = font.render("Game Over!", True, GAME_OVER_COLOR)            text_rect = text.get_rect()            text_rect.center = (SIZE / 2, SIZE / 2)            screen.blit(text, text_rect)        pygame.display.update()if __name__ == "__main__":    main()