首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Pygame错误: pygame.error:显示界面退出

Pygame错误: pygame.error:显示界面退出
EN

Stack Overflow用户
提问于 2020-01-14 05:47:54
回答 1查看 146关注 0票数 0

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

但是发生了pygame.error:显示曲面退出。

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

有谁知道怎么解决这个问题吗?代码如下:(你可以跳到def main_menu)

代码语言:javascript
复制
        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)

更新代码:

代码语言:javascript
复制
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)
EN

回答 1

Stack Overflow用户

发布于 2020-01-15 20:26:58

在你的main_menu循环中,你告诉它循环,而本地的布尔值运行==。这是可以的,但您应该像评论中提到的人一样,执行pygame.quit()并选择性地执行quit() (关闭窗口),而不是您现在拥有的pygame.display.quit()sys.exit()

  • 第二个问题是,如果你通过进入主循环来启动游戏,就会发生第二个问题。我假设主循环运行顶部所示的事件函数?

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

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

对于pygame.event.get()中的事件: if event.type == pygame.QUIT: run = False pygame.quit() quit()

希望这能有所帮助!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59724644

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档