Python:在pygame中进行多个循环的更好方法是什么? [英] Python : What is the better way to make multiple loops in pygame?

查看:49
本文介绍了Python:在pygame中进行多个循环的更好方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我会知道在pygame项目中进行多个循环的最佳方法是什么.这就是我所有的循环:

I would know what is the best way to make multiple loops inside my pygame project. This is all loops I would have :

load loop
game loop
main loop
setting loop
pause loop

但这是做到这一点的最好方法吗?

but is this the best way to do this ?

这是我的代码的样子

run = True
while run :
   # do load loop

def main():
   run = True
   while run :
      if loop == 'main' :
         run2 = True
         while run2 :
             # pygame loop

      if loop == 'pause' :
         run2 = True
         while run2 :
            # other loop
...

if __name__ == '__main__' :
   main()

推荐答案

为什么需要多个嵌套循环?可以存储游戏当前状态的变量:

Why do you need multiple nested loops? A variabel which stores the current state of the game is sufficient:

def main():
    gamestate = 'load'
    run = True
    while run:

        if gamestate == 'load':
            # [...]

        elif gamestate == 'game':
            # [...]

        # [...]
            # [...]

        pygame.display.flip()

if __name__ == '__main__' :
    main()

如果要切换游戏状态,则只需更改变量 gamestate .
您只需要一个运行游戏的主循环.循环中发生的事情可能会有所不同,具体取决于游戏的当前状态.但是,处理事件,清除显示内容,绘制场景并最终更新显示内容始终是相同的循环.

If you want to switch the state of the game, then all you have to do is change the variable gamestate.
One main loop which runs the game is all you need. That what happens in the loop, may vary, dependent on the current state of the game. But it is always the same loop, which handles the events, clears the display, draws the scene and finally updates the display.

请注意,您甚至可以定义执行游戏不同部分的函数,但不需要在函数中循环:

Note, you can even define functions which do the different parts of the game, but you don't need a loop in the functions:

def load(events):
    # [...]

def game(events):
    # [...]

def main():
    gamestate = 'load'
    run = True
    while run:

        events = pygame.event.get()
        for event in events:
            if event.type == pygame.QUIT:
                run = False

        # [...]

        if gamestate == 'load':
            load(events)

        elif gamestate == 'game':
            game(events)

        # [...]
            # [...]

        pygame.display.flip()

if __name__ == '__main__' :
    main()

甚至在课堂上:

class MyGame:
    def __init__(self):
        self.gamestate = 'load'
        self.run = True
        self.events = []

    def load(self):
        # [...]

    def game(self):
        # [...]

    def main(self):
        while self.run:

            self.events = pygame.event.get()
            for event in self.events:
                if event.type == pygame.QUIT:
                    self.run = False

            # [...]

            if self.gamestate == 'load':
                self.load()
            elif self.gamestate == 'game':
                self.game()
            # [...]
                # [...]

            pygame.display.flip()

if __name__ == '__main__' :
    app = MyGame()
    app.main()

这篇关于Python:在pygame中进行多个循环的更好方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆