在pygame中渲染文本时,为什么我的jupyter笔记本会不断崩溃?

我正在使用jupyter笔记本进行py游戏。我注意到,每次我在游戏中渲染一些文本时,笔记本都会崩溃。当我运行游戏并按预期显示文本时,它可以工作。但是,当我关闭游戏窗口并尝试再次运行代码时,它将崩溃。有时它会尝试几次,但有时会崩溃。其他时候,笔记本只要在我关闭游戏窗口时便会合拢。

代码的相关部分为:

import pygame

pygame.init()
run=True
#screensize
screensize = (width,height)=(600,600)
screen = pygame.display.set_mode(screensize)

#font used for the text
myfont = pygame.font.SysFont('Comic Sans MS',30)
#the text that will be rendered. It is usually some variable value,but the problem remains even if it is constant:
vel=3.001


while run:
    pygame.time.delay(20)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run=False 
    screen.fill((0,0))
    ########rendering the text############
    textsurface = myfont.render(str(int(vel)),False,(0,100,100))
    screen.blit(textsurface,(200,400))
    ######################################

    pygame.display.update()

pygame.quit()

让我说我运行代码,然后关闭窗口。如果我再次尝试运行代码,则消息为:

Le noyau sembleplanté。 Il varedémarrer自动化。

(内核似乎已经死亡,它将自动重新启动)

已建议我不要将pygame与jupyter笔记本一起使用,但是我不能在该环境之外进行编程。

myqq519334668 回答:在pygame中渲染文本时,为什么我的jupyter笔记本会不断崩溃?

如上述用户'furas'所建议,一次激活pygame并将pygame.init()替换为pygame.display.init();。 pygame.display.quit()和pygame.quit()为我解决了这个问题。不知道这是否意味着要使用更多的资源,但是到目前为止,在我的基本笔记本电脑中还没有出现任何问题。

import pygame
pygame.init()

在另一个单元格中:

pygame.display.init()
run=True
#screensize
screensize = (width,height)=(600,600)
screen = pygame.display.set_mode(screensize)

#font used for the text
myfont = pygame.font.SysFont('Comic Sans MS',30)
#the text that will be rendered. It is usually some variable value,but the problem remains even if it is constant:
vel=3.001


while run:
    pygame.time.delay(20)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run=False 
    screen.fill((0,0))
    ########rendering the text############
    textsurface = myfont.render(str(int(vel)),False,(0,100,100))
    screen.blit(textsurface,(200,400))
    ######################################

    pygame.display.update()

pygame.display.quit()
本文链接:https://www.f2er.com/3168858.html

大家都在问