【问题标题】:Pygame error: pygame.error: display Surface quitPygame 错误:pygame.error: 显示 Surface 退出
【发布时间】:2020-01-13 21:47:53
【问题描述】:

于是我根据 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)

【问题讨论】:

  • @Rabbid76 所说的。你是说pygame.quit() 吗?
  • 在主应用程序循环之后执行pygame.quit()pygame.display.quit()pygame.display.QUIT() 不存在。
  • 谢谢。我会试试的。
  • @Kingsley 是的,我只是按照说明进行操作,所以可能会出错
  • 现在在我的代码中没有错误,但程序只是弹出几秒钟然后退出,我只是在我的帖子@Rabbid76 中更新了新代码,

标签: python pygame


【解决方案1】:
  1. 在您的 main_menu 循环中,您告诉它在本地布尔运行 == True 时循环。这没关系,但是您应该像 cmets 中提到的那样执行 pygame.quit() 和可选的 quit()(关闭窗口),而不是您现在拥有的 pygame.display.quit()sys.exit()

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

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

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

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

希望这会有所帮助!

【讨论】:

  • 你好,我试试你的代码,没有显示错误,只是打开应用几秒钟就突然退出了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-01
  • 2022-10-17
  • 2020-06-02
  • 1970-01-01
相关资源
最近更新 更多