Python pygame一次一个级别组织和运行功能

Python pygame一次一个级别组织和运行功能,python,pygame,Python,Pygame,我做了一个游戏,但我有两个屏幕显示,菜单页面和游戏页面。在“菜单”页面上,当我单击“播放”按钮时,它会毫无问题地显示游戏,但当我关闭正在运行的游戏时,它会返回到菜单屏幕,但我希望两个屏幕都退出 如果您有任何帮助,我们将不胜感激 示例代码: 菜单屏幕 def menu(): running = True while running: screen.blit(intros, (0,0)) for event in pygame.event.get()

我做了一个游戏,但我有两个屏幕显示,菜单页面和游戏页面。在“菜单”页面上,当我单击“播放”按钮时,它会毫无问题地显示游戏,但当我关闭正在运行的游戏时,它会返回到菜单屏幕,但我希望两个屏幕都退出

如果您有任何帮助,我们将不胜感激

示例代码:

菜单屏幕

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 _uname _;=“_main __;”的文章,但我发现它没用,也许我不知道如何正确实现它

这是因为你有两个while循环。设置
running=False
只会退出当前的while循环,因此离开
MAIN
只会返回到调用它的
菜单

如果要将
running=False
设置为退出两个循环,请将其设置为全局变量(并且只有一个)


如果要完全退出程序,还可以使用
import sys
sys.exit()
。但是我推荐第一个。

现在是拆分代码的好时机。我建议您只使用一个循环。查看
状态
模式

您需要的是一个单独的类,它将在适当的时候调用屏幕中的函数。我在这里起草一份草稿,然后你可以继续

class Window:
    def __init__(self):
        self.state = Menu()
        self.screen = initPygame()
    def run():
        self.state.draw(self.screen)
        self.state.update()
        for event in pygame.event.get():
            self.state.handleEvent(event) 
            if event.type == pygame.QUIT:
                running = False

class Menu:
    def __init__(self):
        pass
        #initialize all menu things (images etc)
    def handleEvent(self, event):
        #change state caused by event
        pass
    def draw(self, screen):
        pass
    def update(self):
        #called once per frame
        pass

代码中变化不大的一个简单解决方案是,只需使用查找
QUIT
事件,而无需将其从事件队列中删除:

def MAIN():
    while True:
        screen.blit(intros, (0,0))
        if pygame.event.peek(pygame.QUIT):
            # since we're in a function, we can simply use return
            return
        for event in pygame.event.get():
            # handle other events

        #all action starts here

另一种方法是用以下方法再次发布
QUIT
事件:

但是这有点难看,尽管它可以确保事件进入
菜单
循环


IMHO需要重写大量代码的最佳解决方案是使用单个主循环和状态来决定当前实际处理的场景(菜单或游戏)

看看这些问题/答案,从中获得灵感:


谢谢@Bartlomiej Lewandowski我以前尝试过这种方法,但由于我的代码结构,我最终还是陷入了类似的循环中。请更改您的结构以删除2 while循环如果我不使用while循环,代码不会在我想要的时间内运行这是因为一个while循环将显示主菜单,您可以在其中单击play按钮。单击播放按钮时,屏幕上会显示另一个屏幕,并运行新代码。它不会运行,因为此主游戏在一个函数中,当该函数被激活时,它需要一个while循环才能继续运行。请尝试将游戏更改为窗口状态。Blit在DrawThank中的另一个屏幕上,示例非常有用。我第一次看到pygame peek时会看一看
def MAIN():
    while True:
        screen.blit(intros, (0,0))
        if pygame.event.peek(pygame.QUIT):
            # since we're in a function, we can simply use return
            return
        for event in pygame.event.get():
            # handle other events

        #all action starts here
def MAIN():
    while True:
        screen.blit(intros, (0,0))
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.event.post(pygame.QUIT)
                # since we're in a function, we can simply use return
                return 

        #all action starts here