view mamba/world.py @ 601:915de6c7d342 default tip

Add support for making the editor fullscreen too.
author Simon Cross <hodgestar@gmail.com>
date Sat, 14 Jan 2023 19:34:26 +0100
parents 48e39d1ad5ae
children
line wrap: on
line source

"""World state (level, plus snake, plus other sprites)."""

import time

from mamba.snake import Snake
from mamba.sound import play_music, pause_music, resume_music, stop_sound


class World(object):
    def __init__(self, level):
        self.level = level
        self.restart()

    def get_size(self):
        return self.level.get_size()

    def draw(self, surface):
        self.level.draw(surface)
        self.snake.draw(surface)

    def get_sprite(self, sprite_id):
        return self.level.extra_sprites[sprite_id]

    def update(self):
        prev = self._prev
        self._prev = time.time()
        if self._paused:
            return
        if prev is None:
            return
        dt = self._prev - prev

        self.snake.update(dt, self)

    def interact(self, segment):
        tiles = segment.filter_collisions(self.level.sprites)
        for tile in tiles:
            tile.interact(self, segment)

    def pause(self):
        pause_music()
        was_paused, self._paused = self._paused, True
        return was_paused == False

    def resume(self):
        resume_music()
        was_paused, self._paused = self._paused, False
        return was_paused == True

    def toggle_pause(self):
        if self._paused:
            self.resume()
        else:
            self.pause()

    def restart(self):
        stop_sound()
        self.level.restart()
        play_music(self.level.background_track)
        tile_pos, orientation = self.level.get_entry()
        self.snake = Snake(tile_pos, orientation)
        self._prev = None
        self._paused = False