Skip to content

topdownengine.scenes Reference

BaseScene

A scene that determines the behavior of the game loop.

Attributes:

Name Type Description
game Game

The game object to use.

ui_containers list[UIContainer]

The list of UIContainers for this scene.

Source code in topdownengine\scenes.py
class BaseScene:
    """A scene that determines the behavior of the game loop.

    Attributes:
        game (Game): The game object to use.
        ui_containers (list[UIContainer]): The list of UIContainers for this scene.
    """
    def __init__(self, game):
        """Initialize the scene.

        Args:
            game (Game): The game object to use.
        """
        self.game = game
        self.ui_containers = []

    def handle_event(self, event: pg.Event):
        """Handle a single event.

        Args:
            event (pygame.Event): The event to handle.
        """
        for container in self.ui_containers:
            container.handle_event(event)

    def update(self, dt: float):
        """Update the scene.

        Args:
            dt (float): The deltatime.
        """
        for container in self.ui_containers:
            container.update(dt)

    def render(self):
        "Render the scene."
        for container in self.ui_containers:
            container.render(self.game.screen)

__init__(game)

Initialize the scene.

Parameters:

Name Type Description Default
game Game

The game object to use.

required
Source code in topdownengine\scenes.py
def __init__(self, game):
    """Initialize the scene.

    Args:
        game (Game): The game object to use.
    """
    self.game = game
    self.ui_containers = []

handle_event(event)

Handle a single event.

Parameters:

Name Type Description Default
event Event

The event to handle.

required
Source code in topdownengine\scenes.py
def handle_event(self, event: pg.Event):
    """Handle a single event.

    Args:
        event (pygame.Event): The event to handle.
    """
    for container in self.ui_containers:
        container.handle_event(event)

render()

Render the scene.

Source code in topdownengine\scenes.py
def render(self):
    "Render the scene."
    for container in self.ui_containers:
        container.render(self.game.screen)

update(dt)

Update the scene.

Parameters:

Name Type Description Default
dt float

The deltatime.

required
Source code in topdownengine\scenes.py
def update(self, dt: float):
    """Update the scene.

    Args:
        dt (float): The deltatime.
    """
    for container in self.ui_containers:
        container.update(dt)

GameplayScene

Bases: BaseScene

A scene that updates and renders all GameObject instances.

Attributes:

Name Type Description
global_alpha int

The global alpha for lighting.

Source code in topdownengine\scenes.py
class GameplayScene(BaseScene):
    """A scene that updates and renders all `GameObject` instances.

    Attributes:
        global_alpha (int): The global alpha for lighting.
    """

    def __init__(self, game):
        """Initialize the GameplayScene.

        Args:
            game (Game): The game object to use.
        """
        super().__init__(game)
        self.global_alpha = 0
        self._light_filters = {}

    def get_light(self, radius: int):
        if not radius in self._light_filters: self._light_filters[radius] = self.create_light(radius)
        return self._light_filters[(radius)]

    def create_light(self, radius: int):
        surface = pg.Surface((max(radius * 2, 1), max(radius * 2, 1)), pg.SRCALPHA)
        for i in range(int(radius), 0, -1):
            alpha = 255 * (1 - i / radius)
            pg.draw.circle(surface, (0, 0, 0, alpha), (radius, radius), i)
        return surface

    def update(self, dt):
        """Update the GameplayScene.

        Args:
            dt (float): The deltatime.
        """
        super().update(dt)
        self.game.game_object_group.update(dt, self.game)

    def render(self):
        "Render the GameplayScene."
        overlay = pg.Surface(self.game.screen.size, pg.SRCALPHA)
        overlay.fill((0, 0, 0, self.global_alpha))

        for game_obj in sorted(self.game.game_object_group.sprites(), key=lambda g: g.draw_index):
            cr = game_obj.rect.move(-self.game.camera.position * game_obj.SCALE)
            self.game.screen.blit(game_obj.image, cr)
            if self.global_alpha > 0 and game_obj.light_radius > 0:
                scaled_lr = game_obj.light_radius * game_obj.SCALE
                overlay.blit(
                    self.get_light(scaled_lr), 
                    (
                        cr.centerx - scaled_lr, 
                        cr.bottom - scaled_lr - game_obj.z * game_obj.SCALE - game_obj.current_frame.height / 2 + game_obj.elevation * game_obj.SCALE
                    ), 
                    special_flags=pg.BLEND_RGBA_SUB
                )

        # Draw debug in a separate loop so that it is drawn over images.
        if self.game.debug:
            for game_obj in self.game.game_object_group.sprites():
                # Draw Colliders
                for collider in game_obj.world_colliders:
                    pg.draw.rect(
                        self.game.screen, 
                        (0, 0, 255), 
                        scale_rect(collider.move(-self.game.camera.position), game_obj.SCALE),
                        1
                    )

        self.game.screen.blit(overlay, (0, 0))

        super().render() # Draw UI

__init__(game)

Initialize the GameplayScene.

Parameters:

Name Type Description Default
game Game

The game object to use.

required
Source code in topdownengine\scenes.py
def __init__(self, game):
    """Initialize the GameplayScene.

    Args:
        game (Game): The game object to use.
    """
    super().__init__(game)
    self.global_alpha = 0
    self._light_filters = {}

handle_event(event)

Handle a single event.

Parameters:

Name Type Description Default
event Event

The event to handle.

required
Source code in topdownengine\scenes.py
def handle_event(self, event: pg.Event):
    """Handle a single event.

    Args:
        event (pygame.Event): The event to handle.
    """
    for container in self.ui_containers:
        container.handle_event(event)

render()

Render the GameplayScene.

Source code in topdownengine\scenes.py
def render(self):
    "Render the GameplayScene."
    overlay = pg.Surface(self.game.screen.size, pg.SRCALPHA)
    overlay.fill((0, 0, 0, self.global_alpha))

    for game_obj in sorted(self.game.game_object_group.sprites(), key=lambda g: g.draw_index):
        cr = game_obj.rect.move(-self.game.camera.position * game_obj.SCALE)
        self.game.screen.blit(game_obj.image, cr)
        if self.global_alpha > 0 and game_obj.light_radius > 0:
            scaled_lr = game_obj.light_radius * game_obj.SCALE
            overlay.blit(
                self.get_light(scaled_lr), 
                (
                    cr.centerx - scaled_lr, 
                    cr.bottom - scaled_lr - game_obj.z * game_obj.SCALE - game_obj.current_frame.height / 2 + game_obj.elevation * game_obj.SCALE
                ), 
                special_flags=pg.BLEND_RGBA_SUB
            )

    # Draw debug in a separate loop so that it is drawn over images.
    if self.game.debug:
        for game_obj in self.game.game_object_group.sprites():
            # Draw Colliders
            for collider in game_obj.world_colliders:
                pg.draw.rect(
                    self.game.screen, 
                    (0, 0, 255), 
                    scale_rect(collider.move(-self.game.camera.position), game_obj.SCALE),
                    1
                )

    self.game.screen.blit(overlay, (0, 0))

    super().render() # Draw UI

update(dt)

Update the GameplayScene.

Parameters:

Name Type Description Default
dt float

The deltatime.

required
Source code in topdownengine\scenes.py
def update(self, dt):
    """Update the GameplayScene.

    Args:
        dt (float): The deltatime.
    """
    super().update(dt)
    self.game.game_object_group.update(dt, self.game)