pocket-friends/pocket_friends/elements/surface.py

47 lines
1.8 KiB
Python

"""Module to aid in the drawing of surfaces to make switching from one screen to another easier."""
import pygame
from ..io.input_handler import InputHandler
class GameSurface(pygame.Surface):
"""
Class to be used as the backbone of all game surfaces.
Attributes:
running (bool): Boolean to tell whether the surface is running or not.
next_surface (:obj:`str`): What the next surface should be after halting.
resource_dir (:obj:`str`): The path of the game's main resource directory.
game_fps (int): How many frames per second the game will run at.
additional_args (dict): Additional arguments to send to the next surface after halting.
"""
def __init__(self, game_res, resources_dir, game_fps):
"""
Create a GameSurface object.
Args:
game_res (int): The internal resolution of the surface.
resources_dir (:obj:`str`): The path of the game's main resource directory.
game_fps: (int): How many frames per second the game will run at.
"""
super().__init__(game_res, pygame.SRCALPHA)
self.running = True
self.next_surface = None
self.resource_dir = resources_dir
self._clock = pygame.time.Clock()
self.game_fps = game_fps
self._input_handler = InputHandler(self._clock)
self.additional_args = {}
self._bg = pygame.image.load(self.resource_dir + '/images/bg.png').convert_alpha()
self.sprites = pygame.sprite.Group()
def preprocess(self):
"""
Advance the surface by one frame and draw the background.
"""
self._clock.tick(self.game_fps)
self.blit(self._bg, (0, 0))
self.sprites.update()
self.sprites.draw(self)
self._input_handler.update()