Pygame 奇怪的画面效果,分段错误和崩溃

Pygame weird effects on the screen, segmentation fault and crash

这几天我运行遇到了一个严重的问题,这是终端抛出的分段错误(核心转储)错误。

输完回到菜单再玩就出现这个问题

下面是实际发生的事情:

上图中的window瞬间出现然后关闭

顺便说一句,我正在玩的游戏的帧数限制为 50,而且我的笔记本电脑是游戏机,所以我不知道这次崩溃的原因是什么,但我认为这是由于代码造成的.

这是我尝试返回菜单然后再次播放时运行的代码片段:

# this is inside the main loop in the game function
for event in events:
    if event.type == KEYDOWN:
        if event.key == K_c: # back to menu
            pygame.quit()
            main_menu()

# this is inside the main_menu() function
if play_button.get_rect(x=320, y=270).colliderect(cursor_rect) and pygame.mouse.get_pressed()[0]:
        pygame.quit()
        pilot()
                

更新:

  1. 发生错误时没有显示特定的错误消息,但所有错误消息都包含 core dumped 问题。

    例如:corrupted size vs. prev_size Aborted (core dumped)

  2. 经过一番研究,我发现这个问题可能与pygame和Ubuntu(linux)之间的兼容性问题有关,我是Linux Mint.

    上的编码方式

现在,我已经成功解决了这个问题,它与多次 pygame.display.set_mode() 调用有关。

我一直在为每个菜单和游戏创建 window,这导致了分段错误(核心转储)错误。

根据评论中@furas 的说法,pygame.init()pygame.display.set_mode() 应该只被调用一次,而 pygame.quit() 只有在退出整个程序时才被调用。