[英]Import loop only works twice, gives pygame.error Display surface quit
I am in a beginner python class and we are working on our final projects.我是初学者 python class,我们正在做我们的最终项目。 For my game however I wanted to be able to restart the game within itself.
但是,对于我的游戏,我希望能够在其内部重新启动游戏。 I did this by using imports to launch a new script to then re-run my game.
我通过使用导入来启动一个新脚本然后重新运行我的游戏来做到这一点。 However whenever I try to do it more than once my window breaks, or the code does not import the next file.
但是,每当我尝试多次执行此操作时,我的 window 都会中断,或者代码不会导入下一个文件。 My game is kinda long so I've created a very short program to give an example of my issue.
我的游戏有点长,所以我创建了一个非常短的程序来举例说明我的问题。
(This being the file yin.py) (这是文件 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()
(and this file yang.py) (和这个文件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()
I've tried doing multiple things but so far I have gotten nowhere, it still is giving me display not initialized even though I have 'pygame.init()'.我已经尝试过做很多事情,但到目前为止我一无所获,即使我有'pygame.init()',它仍然给我显示未初始化。 My teacher isn't responding to my emails and I have an encroaching deadline.
我的老师没有回复我的电子邮件,我的截止日期快到了。 Any help will be appreciated.
任何帮助将不胜感激。 :p
:p
The "import" statement only imports once. “import”语句只导入一次。 If the name already exists, it doesn't import again.
如果名称已存在,则不会再次导入。 You should put your code into a function.
您应该将代码放入 function。 Then, you can call the function as many times as you want.
然后,您可以根据需要多次调用 function。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.