我制作了一个游戏,但我有两个屏幕要显示,菜单页面和游戏页面。在菜单页面上,当我单击播放按钮时,它显示游戏没有任何问题,但是当我关闭正在运行的游戏时,它会返回到菜单屏幕,但我希望两个屏幕都退出。
请提供任何帮助,我们将不胜感激。
示例代码:
菜单屏幕
def menu():
running = True
while running:
screen.blit(intros, (0,0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
GUI.button("Play game", 24,300, 128,50, violet, blue, screen, MAIN)
necessary things here!
def MAIN():
running = True
while running:
screen.blit(intros, (0,0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
all action starts here
请任何人给我一个关于如何运行菜单游戏的建议,这样当单击播放按钮并且主代码正在运行时,当我单击关闭时,游戏会停止并且不会返回到菜单代码.
我在几个论坛上阅读过有关使用 if __name__ = "__main__"
的内容,但我发现它没用,也许我不知道如何正确实现它
最佳答案
发生这种情况是因为有两个 while 循环。设置 running = False
仅退出当前的 while 循环,因此您离开 MAIN
只会返回到您从中调用它的 menu
。
如果您希望设置 running = False
退出两个循环,请将其设为全局变量(并且只有一个)。
running = True
def menu():
...
def MAIN():
...
如果您想完全退出程序,还可以使用import sys
和sys.exit()
。但我推荐第一个。
关于python - pygame 一次一个级别的组织和运行函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30715650/