[英]Import loop only works twice, gives pygame.error Display surface quit
我是初学者 python class,我们正在做我们的最终项目。 但是,对于我的游戏,我希望能够在其内部重新启动游戏。 我通过使用导入来启动一个新脚本然后重新运行我的游戏来做到这一点。 但是,每当我尝试多次执行此操作时,我的 window 都会中断,或者代码不会导入下一个文件。 我的游戏有点长,所以我创建了一个非常短的程序来举例说明我的问题。
(这是文件 yin.py)
import pygame
BLACK = (0, 0, 0)
pygame.init()
SIZE = [300, 300]
screen = pygame.display.set_mode(SIZE)
clock = pygame.time.Clock()
done = False
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
if event.type == pygame.MOUSEBUTTONDOWN:
import yang
done = True
screen.fill(BLACK)
pygame.display.flip()
clock.tick(30)
pygame.quit()
(和这个文件yang.py)
import pygame
WHITE = (255, 255, 255)
pygame.init()
SIZE = [300, 300]
screen = pygame.display.set_mode(SIZE)
clock = pygame.time.Clock()
done = False
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
if event.type == pygame.MOUSEBUTTONDOWN:
import yin
done = True
screen.fill(WHITE)
pygame.display.flip()
clock.tick(30)
pygame.quit()
我已经尝试过做很多事情,但到目前为止我一无所获,即使我有'pygame.init()',它仍然给我显示未初始化。 我的老师没有回复我的电子邮件,我的截止日期快到了。 任何帮助将不胜感激。 :p
“import”语句只导入一次。 如果名称已存在,则不会再次导入。 您应该将代码放入 function。 然后,您可以根据需要多次调用 function。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.