为什么一个场景以场景管理器为参数? [英] Why does a scene take a scene manager as a parameter?

查看:79
本文介绍了为什么一个场景以场景管理器为参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在理解 pygame 中的游戏场景管理器 (FSM) 时遇到问题,我尝试从该网站复制:https://nicolasivanhoe.wordpress.com/2014/03/10/game-scene-manager-in-python-pygame/

I'm having problems understanding a game scene manager (FSM) in pygame that I try to replicate from this website: https://nicolasivanhoe.wordpress.com/2014/03/10/game-scene-manager-in-python-pygame/

我会复制代码:

# -*- encoding: utf-8 -*-
 
# Modules
import pygame, sys
 
class Director:
    """Represents the main object of the game.
 
    The Director object keeps the game on, and takes care of updating it,
    drawing it and propagate events.
 
    This object must be used with Scene objects that are defined later."""
 
    def __init__(self):
        self.screen = pygame.display.set_mode((640, 480))
        pygame.display.set_caption("Game Name")
        self.scene = None
        self.quit_flag = False
        self.clock = pygame.time.Clock()
 
    def loop(self):
        "Main game loop."
 
        while not self.quit_flag:
            time = self.clock.tick(60)
 
            # Exit events
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    self.quit()
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_ESCAPE:
                        self.quit()
 
            # Detect events
            self.scene.on_event()
 
            # Update scene
            self.scene.on_update()
 
            # Draw the screen
            self.scene.on_draw(self.screen)
            pygame.display.flip()
 
    def change_scene(self, scene):
        "Changes the current scene."
        self.scene = scene
 
    def quit(self):
        self.quit_flag = True


class Scene:
     """Represents a scene of the game.
 
     Scenes must be created inheriting this class attributes
     in order to be used afterwards as menus, introduction screens,
     etc."""
 
     def __init__(self, director):
         self.director = director
 
     def on_update(self):
         "Called from the director and defined on the subclass."
 
         raise NotImplementedError("on_update abstract method must be defined in subclass.")
 
     def on_event(self, event):
         "Called when a specific event is detected in the loop."
 
         raise NotImplementedError("on_event abstract method must be defined in subclass.")
 
     def on_draw(self, screen):
         "Se llama cuando se quiere dibujar la pantalla."
 
         raise NotImplementedError("on_draw abstract method must be defined in subclass.")

class SceneHome(Scene):
    """ Welcome screen of the game, the first one to be loaded."""
 
    def __init__(self, director):
        Scene.__init__(self, director)
 
    def on_update(self):
        pass
 
    def on_event(self):
        pass
 
    def on_draw(self):
        pass
 
def main():
    dir = Director()
    scene = SceneHome(dir)
    dir.change_scene(scene)
    dir.loop()
 
if __name__ == '__main__':
    pygame.init()
    main()

在主函数中,一个导演对象和一个场景主对象被实例化.我明白那个.然后它调用方法change_scene 并传递scenehome 对象.但我不明白为什么导演对象是 SceneHome 类中的一个参数.好像什么都没做.它在我看来是一个循环调用,但我不知道它的作用.为什么在 Scene 类中需要一个导演?

In the main function a director object and a scenehome object are instantiated. I understand that. Then it calls the method change_scene and passes the scenehome object. But I can't understand why a director object is a parameter inside the SceneHome class. It seems it does nothing. It looks to me as a circular call, but I don't know what it does. Why does it need a director inside the Scene class?

推荐答案

Director 类充当您的主要游戏循环,同时让场景处理它们自己的事件和更新方法中的所有逻辑.这意味着要将场景从场景类更改为另一个场景,您需要引用导演对象以调用 self.director.change_scene(new_scene).

The Director class is acting as your main game loop while letting the scenes handle all the logic within their own event and update methods. Which means to change scenes from a scene class to another, you'd need a reference to the director object to call self.director.change_scene(new_scene).

这篇关于为什么一个场景以场景管理器为参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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