pocket-friends/pocket_friends/elements/surface.py

59 lines
2.5 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.
The GameSurface class is what the game uses to draw all other elements on top of. It controls a number of game
operations, including input handling, frame rate limiting, and surface switching. It is a child class of Pygame's
Surface class and utilizes Pygame's sprite handling along with the ability to blit certain things on the screen
in order to draw elements easily.
Attributes:
running: Boolean to tell whether the surface is running or not.
next_surface: String that represents the next surface to be switched to after halting.
resource_dir: String that contains the path of the game's main resource directory.
game_fps: An integer value of how many frames per second the game will run at.
additional_args: Dictionary of additional arguments to send to the next surface after halting.
background: A Pygame surface that will be drawn behind all other elements.
"""
def __init__(self, game_res: int, resources_dir: str, game_fps: pygame.Surface):
"""
Create a GameSurface object.
Args:
game_res: The internal resolution of the surface.
resources_dir: The path of the game's main resource directory.
game_fps: How many frames per second the game will run at.
"""
super().__init__(game_res, pygame.SRCALPHA)
self.running = True # Surfaces should be running by default
self.next_surface = None
self.resource_dir = resources_dir
self.game_fps = game_fps
self.additional_args = {}
self.dev_override = False
self.background = pygame.image.load(self.resource_dir + '/images/bg.png').convert_alpha()
self._clock = pygame.time.Clock()
self._input_handler = InputHandler(self._clock)
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.background, (0, 0))
self._sprites.update()
self._sprites.draw(self)
self._input_handler.update()
if self._input_handler.dev_found:
self.dev_override = True
self.running = False