[英]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.