【问题标题】:Why does my jupyter notebook keeps crashing when rendering text in pygame?为什么我的 jupyter notebook 在 pygame 中渲染文本时总是崩溃?
【发布时间】:2019-11-04 05:46:55
【问题描述】:

我正在使用 jupyter notebook 制作一个 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 semble Planté。 Il va redémarrer automatiquement。

(内核似乎已经死了。它会自动重启)

有人建议我不要将 pygame 与 jupyter notebook 一起使用,但我不能在那个环境之外编写代码。

【问题讨论】:

  • 你不能为此使用文本编辑器或 IDE 吗?
  • 我几乎是个业余爱好者。真不知道你的意思。 :|
  • 我的意思是不要使用Jupyter,而是使用文本编辑器(如Notepad++GeanSublime Text等)或IDE(如PyCharmVisual Studio Code等) .)。或者像任何其他程序一样运行代码 - 没有 Jupyter。
  • 感谢您的评论。但是从 jupyter notebook 迁移对我来说不是一个选择。我有很多东西要学,在这一点上改变我使用的环境会很困难,因为 jupyter 是我开始编码以来唯一使用过的环境
  • 唯一的想法:几天前有人在 Windows 上运行 pygame 窗口多次(没有 Jupyter)在 Windows 上(在 Linux 上它工作正常)。解决方案是只使用一次pygame.init(),不要使用pygame.quit(),在再次创建窗口之前使用pygame.display.init(),最后使用pygame.display.quit()关闭窗口。

标签: python text pygame rendering jupyter


【解决方案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()

【讨论】:

    猜你喜欢
    • 2011-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多