2023-05-13 13:07:33 -04:00
|
|
|
"""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.
|
2023-05-15 00:24:57 -04:00
|
|
|
|
|
|
|
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.
|
|
|
|
|
2023-05-13 13:07:33 -04:00
|
|
|
Attributes:
|
2023-05-15 00:24:57 -04:00
|
|
|
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.
|
2023-05-13 13:07:33 -04:00
|
|
|
"""
|
2023-05-15 00:24:57 -04:00
|
|
|
def __init__(self, game_res: int, resources_dir: str, game_fps: pygame.Surface):
|
2023-05-13 13:07:33 -04:00
|
|
|
"""
|
|
|
|
Create a GameSurface object.
|
|
|
|
Args:
|
2023-05-15 00:24:57 -04:00
|
|
|
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.
|
2023-05-13 13:07:33 -04:00
|
|
|
"""
|
|
|
|
super().__init__(game_res, pygame.SRCALPHA)
|
2023-05-15 00:26:44 -04:00
|
|
|
self.running = True # Surfaces should be running by default
|
2023-05-13 13:07:33 -04:00
|
|
|
self.next_surface = None
|
|
|
|
self.resource_dir = resources_dir
|
|
|
|
self.game_fps = game_fps
|
|
|
|
self.additional_args = {}
|
2023-05-14 19:26:09 -04:00
|
|
|
self.dev_override = False
|
2023-05-13 13:07:33 -04:00
|
|
|
|
2023-05-15 00:26:44 -04:00
|
|
|
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()
|
2023-05-13 13:07:33 -04:00
|
|
|
|
|
|
|
def preprocess(self):
|
|
|
|
"""
|
|
|
|
Advance the surface by one frame and draw the background.
|
|
|
|
"""
|
|
|
|
self._clock.tick(self.game_fps)
|
|
|
|
|
2023-05-15 00:26:44 -04:00
|
|
|
self.blit(self.background, (0, 0))
|
|
|
|
self._sprites.update()
|
|
|
|
self._sprites.draw(self)
|
2023-05-13 13:07:33 -04:00
|
|
|
|
|
|
|
self._input_handler.update()
|
2023-05-14 19:26:09 -04:00
|
|
|
if self._input_handler.dev_found:
|
|
|
|
self.dev_override = True
|
|
|
|
self.running = False
|