Ejemplo n.º 1
0
    def __init__(self, control):
        """ Do not override this unless there is a special need.

        All init for the State, loading of config, images, etc should
        be done in State.startup or State.resume, not here.

        :param control: State Manager / Control / Game... all the same
        :returns: None
        """
        self.game = control  # TODO: rename 'game' to 'control'?
        self.start_time = 0.0
        self.current_time = 0.0
        self.animations = pygame.sprite.Group()  # only animations and tasks
        self.sprites = SpriteGroup()  # all sprites that draw on the screen
Ejemplo n.º 2
0
    def __init__(self, control):
        """ Do not override this unless there is a special need.

        All init for the State, loading of config, images, etc should
        be done in State.startup or State.resume, not here.

        :param control: State Manager / Control / Game... all the same
        :returns: None
        """
        self.game = control  # TODO: rename 'game' to 'control'?
        self.start_time = 0.0
        self.current_time = 0.0
        self.animations = pygame.sprite.Group()  # only animations and tasks
        self.sprites = SpriteGroup()             # all sprites that draw on the screen
Ejemplo n.º 3
0
class State(object):
    """ This is a prototype class for States.

    All states should inherit from it. No direct instances of this
    class should be created. Update must be overloaded in the child class.

    Overview of Methods:
       startup       - Called when added to the state stack
       resume        - Called each time state is updated for first time
       update        - Called each frame while state is active
       process_event - Called when there is a new input event
       pause         - Called when state is no longer active
       shutdown      - Called before state is destroyed

    :ivar game: core.control.Control
    :cvar force_draw: If True, state will never be skipped in drawing phase
    :cvar rect: Area of the screen will be drawn on
    """
    __metaclass__ = ABCMeta

    rect = pygame.Rect((0, 0), prepare.SCREEN_SIZE)
    force_draw = False

    def __init__(self, control):
        """ Do not override this unless there is a special need.

        All init for the State, loading of config, images, etc should
        be done in State.startup or State.resume, not here.

        :param control: State Manager / Control / Game... all the same
        :returns: None
        """
        self.game = control  # TODO: rename 'game' to 'control'?
        self.start_time = 0.0
        self.current_time = 0.0
        self.animations = pygame.sprite.Group()  # only animations and tasks
        self.sprites = SpriteGroup()  # all sprites that draw on the screen

    @property
    def name(self):
        return self.__class__.__name__

    def load_sprite(self, filename, **kwargs):
        """ Load a sprite and add it to this state

        kwargs can be any value used by pygame Rect, or layer

        :param filename: filename, relative to the resources folder
        :type filename: String
        :param kwargs: Keyword arguments to pass to the Rect constructor
        :returns: core.components.sprite.Sprite
        """
        layer = kwargs.pop('layer', 0)
        sprite = tools.load_sprite(filename, **kwargs)
        self.sprites.add(sprite, layer=layer)
        return sprite

    def animate(self, *targets, **kwargs):
        """ Animate something in this state

        Animations are processed even while state is inactive

        :param targets: targets of the Animation
        :type targets: any
        :param kwargs: Attributes and their final value
        :returns: core.components.animation.Animation
        """
        ani = Animation(*targets, **kwargs)
        self.animations.add(ani)
        return ani

    def task(self, *args, **kwargs):
        """ Create a task for this state

        Tasks are processed even while state is inactive
        If you want to pass positional arguments, use functools.partial

        :param args: function to be called
        :param kwargs: kwargs passed to the function
        :returns: core.components.animation.Task
        """
        task = Task(*args, **kwargs)
        self.animations.add(task)
        return task

    def remove_animations_of(self, target):
        """ Given and object, remove any animations that it is used with

        :param target: any
        :returns: None
        """
        remove_animations_of(target, self.animations)

    def process_event(self, event):
        """ Processes events that were passed from the main event loop.

        This function can choose to return the event, or any other in
        response to the event passed.  If the same, or any other event
        is returned, then it will be passed down to other states.

        :param event: A pygame key event from pygame.event.get()
        :type event: PyGame Event
        :returns: Pygame Event or None
        :rtype: pygame Event

        """
        return event

    # def __del__(self):
    #     print "dying", self

    def update(self, time_delta):
        """ Time update function for state.  Must be overloaded in children.

        :param time_delta: amount of time in fractional seconds since last update
        :type time_delta: Float
        :returns: None
        :rtype: None
        """
        pass

    def draw(self, surface):
        """ Render the state to the surface passed.  Must be overloaded in children

        :param surface: Surface to be rendered onto
        :type surface: pygame.Surface
        :returns: None
        :rtype: None
        """
        pass

    def startup(self, **kwargs):
        """ Called when scene is added to State Stack

        This will be called:
        * after state is pushed and before next update
        * just once during the life of a state

        Example uses: loading images, configuration, sounds.

        :param kwargs: Configuration options
        :returns: None
        :rtype: None
        """
        pass

    def resume(self):
        """ Called before update when state is newly in focus

        This will be called:
        * before update after being pushed to the stack
        * before update after state has been paused

        After being called, state will begin to receive player input
        Could be called several times over lifetime of state

        Example uses: starting music, open menu, starting animations, timers, etc

        :returns: None
        :rtype: None
        """
        pass

    def pause(self):
        """ Called when state is pushed back in the stack, allowed to pause

        This will be called:
        * after update when state is pushed back
        * before being shutdown

        After being called, state will no longer receive player input
        Could be called several times over lifetime of state

        Example uses: stopping music, sounds, fading out, making state graphics dim

        :returns: None
        :rtype: None
        """
        pass

    def shutdown(self):
        """ Called when state is removed from stack and will be destroyed

        This will be called:
        * after update when state is popped

        Make sure to release any references to objects that may cause
        cyclical dependencies.

        :returns: None
        :rtype: None
        """
        pass
Ejemplo n.º 4
0
class State(object):
    """ This is a prototype class for States.

    All states should inherit from it. No direct instances of this
    class should be created. Update must be overloaded in the child class.

    Overview of Methods:
       startup       - Called when added to the state stack
       resume        - Called each time state is updated for first time
       update        - Called each frame while state is active
       process_event - Called when there is a new input event
       pause         - Called when state is no longer active
       shutdown      - Called before state is destroyed

    :ivar game: core.control.Control
    :cvar force_draw: If True, state will never be skipped in drawing phase
    :cvar rect: Area of the screen will be drawn on
    """
    __metaclass__ = ABCMeta

    rect = pygame.Rect((0, 0), prepare.SCREEN_SIZE)
    force_draw = False

    def __init__(self, control):
        """ Do not override this unless there is a special need.

        All init for the State, loading of config, images, etc should
        be done in State.startup or State.resume, not here.

        :param control: State Manager / Control / Game... all the same
        :returns: None
        """
        self.game = control  # TODO: rename 'game' to 'control'?
        self.start_time = 0.0
        self.current_time = 0.0
        self.animations = pygame.sprite.Group()  # only animations and tasks
        self.sprites = SpriteGroup()             # all sprites that draw on the screen

    @property
    def name(self):
        return self.__class__.__name__

    def load_sprite(self, filename, **kwargs):
        """ Load a sprite and add it to this state

        kwargs can be any value used by pygame Rect, or layer

        :param filename: filename, relative to the resources folder
        :type filename: String
        :param kwargs: Keyword arguments to pass to the Rect constructor
        :returns: core.components.sprite.Sprite
        """
        layer = kwargs.pop('layer', 0)
        sprite = tools.load_sprite(filename, **kwargs)
        self.sprites.add(sprite, layer=layer)
        return sprite

    def animate(self, *targets, **kwargs):
        """ Animate something in this state

        Animations are processed even while state is inactive

        :param targets: targets of the Animation
        :type targets: any
        :param kwargs: Attributes and their final value
        :returns: core.components.animation.Animation
        """
        ani = Animation(*targets, **kwargs)
        self.animations.add(ani)
        return ani

    def task(self, *args, **kwargs):
        """ Create a task for this state

        Tasks are processed even while state is inactive
        If you want to pass positional arguments, use functools.partial

        :param args: function to be called
        :param kwargs: kwargs passed to the function
        :returns: core.components.animation.Task
        """
        task = Task(*args, **kwargs)
        self.animations.add(task)
        return task

    def remove_animations_of(self, target):
        """ Given and object, remove any animations that it is used with

        :param target: any
        :returns: None
        """
        remove_animations_of(target, self.animations)

    def process_event(self, event):
        """ Processes events that were passed from the main event loop.

        This function can choose to return the event, or any other in
        response to the event passed.  If the same, or any other event
        is returned, then it will be passed down to other states.

        :param event: A pygame key event from pygame.event.get()
        :type event: PyGame Event
        :returns: Pygame Event or None
        :rtype: pygame Event

        """
        return event

    # def __del__(self):
    #     print "dying", self

    def update(self, time_delta):
        """ Time update function for state.  Must be overloaded in children.

        :param time_delta: amount of time in fractional seconds since last update
        :type time_delta: Float
        :returns: None
        :rtype: None
        """
        pass

    def draw(self, surface):
        """ Render the state to the surface passed.  Must be overloaded in children

        :param surface: Surface to be rendered onto
        :type surface: pygame.Surface
        :returns: None
        :rtype: None
        """
        pass

    def startup(self, **kwargs):
        """ Called when scene is added to State Stack

        This will be called:
        * after state is pushed and before next update
        * just once during the life of a state

        Example uses: loading images, configuration, sounds.

        :param kwargs: Configuration options
        :returns: None
        :rtype: None
        """
        pass

    def resume(self):
        """ Called before update when state is newly in focus

        This will be called:
        * before update after being pushed to the stack
        * before update after state has been paused

        After being called, state will begin to receive player input
        Could be called several times over lifetime of state

        Example uses: starting music, open menu, starting animations, timers, etc

        :returns: None
        :rtype: None
        """
        pass

    def pause(self):
        """ Called when state is pushed back in the stack, allowed to pause

        This will be called:
        * after update when state is pushed back
        * before being shutdown

        After being called, state will no longer receive player input
        Could be called several times over lifetime of state

        Example uses: stopping music, sounds, fading out, making state graphics dim

        :returns: None
        :rtype: None
        """
        pass

    def shutdown(self):
        """ Called when state is removed from stack and will be destroyed

        This will be called:
        * after update when state is popped

        Make sure to release any references to objects that may cause
        cyclical dependencies.

        :returns: None
        :rtype: None
        """
        pass
Ejemplo n.º 5
0
 def create_new_menu_items_group(self):
     # these groups will not automatically position the sprites
     self.menu_items = MenuSpriteGroup()
     self.menu_sprites = SpriteGroup()