【发布时间】: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++、Gean、Sublime Text等)或IDE(如PyCharm、Visual 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