首页 > 解决方案 > Pygame 错误:pygame.error: 显示 Surface 退出

问题描述

于是我根据Youtube教程创建了一个Python游戏俄罗斯方块: https ://www.youtube.com/watch?v=zfvxp7PgQ6c&t=2075s

但是 pygame.error: display Surface quit 发生。

我尝试在 pygame.quit 之后添加“break”、“sys.exit()”、“QUIT”,但不起作用。

有谁知道如何解决它?这是代码:(您可以跳到def main_menu)

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run == False

            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    current_piece.x -= 1
                    if not (valid_space(current_piece, grid)):
                        current_piece.x += 1
                if event.key == pygame.K_RIGHT:
                    current_piece.x += 1
                    if not (valid_space(current_piece, grid)):
                        current_piece.x -= 1
                if event.key == pygame.K_DOWN:
                    current_piece.y += 1
                    if not (valid_space(current_piece, grid)):
                        current_piece.y -= 1
                    if event.key == pygame.K_UP:
                        current_piece.rotation += current_piece.rotation + 1 % len(current_piece.shape)
                    if not (valid_space(current_piece, grid)):
                        current_piece.rotation -= 1

                    shape_pos = convert_shape_format(current_piece)

                    for i in range(len(shape_pos)):
                        x, y = shape_pos[i]
                    if y > -1:
                        grid[y][x] = current_piece.color

                    if change_piece:
                        for pos in shape_pos:
                         p = (pos[0], pos[1])
                    locked_positions[p] = current_piece.color
                    current_piece = next_piece
                    next_piece = get_shape()
                    change_piece = False
                    score += clear_rows(grid, locked_positions) * 10

                    draw_window(win, grid, score, last_score)
                    draw_next_shape(next_piece, win)
                    pygame.display.update()

                    if check_lost(locked_positions):
                        draw_text_middle(win, "You Lost!", 80, (255,255,255))
                        pygame.display.update()
                        pygame.time.delay(1500)
                        run = False
                        update_score(score)
def main_menu(win):
    run = True
    while run:
        win.fill((0,0,0))
        draw_text_middle(win, 'Press any key to play', 60, (255,255,255))
        pygame.display.update()
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False
                pygame.display.quit()
                sys.exit()

            if event.type == pygame.KEYDOWN:
                main(win)

    pygame.display.QUIT()

win = pygame.display.set_mode((s_width, s_height))
pygame.display.set_caption('Tetris')
main_menu(win)

更新代码:

def main_menu(win):
    run = True
    while run:
        win.fill((0,0,0))
        draw_text_middle(win, 'Press any key to play', 60, (255,255,255))
        pygame.display.update()
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False
            pygame.quit()
            quit()

            if event.type == pygame.KEYDOWN:
                main(win)

    pygame.quit()

win = pygame.display.set_mode((s_width, s_height))
pygame.display.set_caption('Tetris')
main_menu(win)

标签: pythonpygame

解决方案


  1. 在您的 main_menu 循环中,您告诉它在本地布尔运行 == True 时循环。没关系,但是您应该像评论中提到的那样做 a pygame.quit()and 可选quit()(关闭窗口),而不是您现在拥有的pygame.display.quit()and 。sys.exit()

  2. 如果您通过进入主循环开始游戏,则会出现第二个问题。我假设主循环运行顶部显示的事件函数?

    根据您编写代码的方式,事件函数中的布尔运行是本地的。这意味着它不会更改您在主循环中使用的运行值(也不会在 main_menu 循环中更改它)。我建议转移到 OOP 并创建一个 self.run 布尔值,否则您需要使布尔值全局运行。

    你应该在事件函数中写下这个而不是你现在在顶部的:

    for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False
                pygame.quit()
                quit()
    

希望这可以帮助!


推荐阅读