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

问题描述 投票:0回答:1

我正在使用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,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自动化。

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

建议我不要在jupyter笔记本上使用pygame,但是我不能在该环境之外进行编码。

python text pygame rendering jupyter
1个回答
1
投票

根据上述用户'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,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()
© www.soinside.com 2019 - 2024. All rights reserved.