created surface.py and GameSurface class

This commit is contained in:
Nicholas Dyer 2023-05-13 13:07:33 -04:00
parent 41ab0406f8
commit 83a7e98a7f
7 changed files with 82 additions and 89 deletions

View File

@ -4,7 +4,7 @@ import pocket_friends
script_dir = os.path.dirname(os.path.abspath(__file__)) script_dir = os.path.dirname(os.path.abspath(__file__))
PyInstaller.__main__.run([ PyInstaller.__main__.preprocess([
'{0}/pocket_friends/__main__.py'.format(script_dir), '{0}/pocket_friends/__main__.py'.format(script_dir),
'--clean', '--clean',
'--noconsole', '--noconsole',

View File

@ -63,7 +63,7 @@ class Menu:
:param kwargs: keyword arguments to be passed to the function :param kwargs: keyword arguments to be passed to the function
""" """
try: try:
return self._options[self._selection].run(*args, **kwargs) return self._options[self._selection].preprocess(*args, **kwargs)
except IndexError as ex: except IndexError as ex:
raise Exception('menu has no options, cannot run a non-existent option') from ex raise Exception('menu has no options, cannot run a non-existent option') from ex

View File

@ -0,0 +1,46 @@
"""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()

View File

@ -1,22 +1,9 @@
import pygame import pygame
from ..elements import sprites from ..elements import sprites, surface
from ..io.input_handler import InputHandler
class Surface(surface.GameSurface):
class Surface(pygame.Surface): def __init__(self, game_res, resources_dir, game_fps, **kwargs):
def __init__(self, window_size, resources_dir, game_fps, **kwargs): super().__init__(game_res, resources_dir, game_fps)
super().__init__(window_size, pygame.SRCALPHA)
self.name = 'egg_select'
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()
preselected_color = None preselected_color = None
for key in kwargs.keys(): for key in kwargs.keys():
@ -116,13 +103,7 @@ class Surface(pygame.Surface):
self.selected_egg += self.eggs_per_row self.selected_egg += self.eggs_per_row
def update(self): def update(self):
self.clock.tick(self.game_fps) self.preprocess()
self.blit(self.bg, (0, 0))
self.sprites.update()
self.sprites.draw(self)
self.input_handler.update()
cursor = pygame.image.load( cursor = pygame.image.load(
self.resource_dir + '/images/gui/egg_selector.png').convert_alpha() self.resource_dir + '/images/gui/egg_selector.png').convert_alpha()

View File

@ -1,29 +1,21 @@
import pygame import pygame
from ..elements import surface
from pocket_friends.game_files.io.input_handler import InputHandler from pocket_friends.game_files.io.input_handler import InputHandler
class Surface(pygame.Surface): class Surface(surface.GameSurface):
def __init__(self, window_size, resources_dir, game_fps, **kwargs): def __init__(self, game_res, resources_dir, game_fps, **kwargs):
super().__init__(window_size, pygame.SRCALPHA) super().__init__(game_res, resources_dir, game_fps)
self.name = 'error_screen'
self.running = True
self.next_surface = 'title'
self.clock = pygame.time.Clock()
self.input_handler = InputHandler(self.clock)
self.additional_args = {}
self.bg = pygame.image.load(resources_dir + '/images/bg.png').convert_alpha()
self.title = pygame.image.load(resources_dir + '/images/debug/invalid.png').convert_alpha()
self.frames = 1 self.frames = 1
self.game_fps = game_fps self.game_fps = game_fps
self.delay = 1 self.delay = 1
self.font = pygame.font.Font(resources_dir + '/fonts/5Pts5.ttf', 10) self.font = pygame.font.Font(resources_dir + '/fonts/5Pts5.ttf', 10)
self.title = pygame.image.load(resources_dir + '/images/debug/invalid.png').convert_alpha()
self.next_surface = 'title'
def update(self): def update(self):
self.clock.tick(self.game_fps) self.preprocess()
self.blit(self.bg, (0, 0))
self.blit(self.title, (0, -4)) self.blit(self.title, (0, -4))
text = self.font.render('Frames: {0}'.format(self.frames), False, (64, 64, 64)) text = self.font.render('Frames: {0}'.format(self.frames), False, (64, 64, 64))
self.blit(text, (3, 68)) self.blit(text, (3, 68))
@ -31,8 +23,6 @@ class Surface(pygame.Surface):
self.frames += 1 self.frames += 1
self.frames %= self.game_fps self.frames %= self.game_fps
self.input_handler.update()
for event in pygame.event.get(): for event in pygame.event.get():
if event.type == pygame.KEYDOWN: if event.type == pygame.KEYDOWN:
if event.key == pygame.K_b: if event.key == pygame.K_b:

View File

@ -1,33 +1,16 @@
import pygame import pygame
from ..elements import sprites from ..elements import sprites, surface
from ..io.input_handler import InputHandler from ..io.input_handler import InputHandler
class Surface(pygame.Surface): class Surface(surface.GameSurface):
""" def __init__(self, game_res, resources_dir, game_fps, **kwargs):
super().__init__(game_res, resources_dir, game_fps)
""" preselected_color = None
def __init__(self, window_size, resources_dir, game_fps, **kwargs): for key in kwargs.keys():
""" if key == 'selected_color':
preselected_color = kwargs.get(key)
Args:
window_size:
resources_dir:
game_fps:
**kwargs:
"""
super().__init__(window_size, pygame.SRCALPHA)
self.name = 'selection_info'
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()
self.selected_egg = None self.selected_egg = None
for key in kwargs.keys(): for key in kwargs.keys():
@ -39,20 +22,15 @@ class Surface(pygame.Surface):
egg.rect.y = 3 egg.rect.y = 3
self.sprites.add(egg) self.sprites.add(egg)
self.info_text = sprites.InfoText(resources_dir, window_size[0], egg.description) self.info_text = sprites.InfoText(resources_dir, game_res[0], egg.description)
self.info_icons = sprites.EggInfo(resources_dir, egg.contentedness, egg.metabolism, (32, 4)) self.info_icons = sprites.EggInfo(resources_dir, egg.contentedness, egg.metabolism, (32, 4))
def update(self): def update(self):
self.clock.tick(self.game_fps) self.preprocess()
self.blit(self.bg, (0, 0))
self.sprites.update()
self.sprites.draw(self)
self.info_text.draw(self) self.info_text.draw(self)
self.info_icons.draw(self) self.info_icons.draw(self)
self.input_handler.update()
for event in pygame.event.get(): for event in pygame.event.get():
if event.type == pygame.KEYDOWN: if event.type == pygame.KEYDOWN:
if event.key == pygame.K_DOWN: if event.key == pygame.K_DOWN:

View File

@ -1,26 +1,24 @@
import pygame import pygame
from ..elements import surface
class Surface(pygame.Surface): class Surface(surface.GameSurface):
def __init__(self, window_size, resources_dir, game_fps): def __init__(self, game_res, resources_dir, game_fps):
super().__init__(window_size, pygame.SRCALPHA) super().__init__(game_res, resources_dir, game_fps)
self.name = 'title'
self.running = True
self.next_surface = None
self.clock = pygame.time.Clock()
self.additional_args = {}
self.bg = pygame.image.load(resources_dir + '/images/bg.png').convert_alpha() self.frames = 0
self.title = pygame.image.load(resources_dir + '/images/title.png').convert_alpha()
self.frames = 1
self.game_fps = game_fps
self.delay = 1 self.delay = 1
self.title = pygame.image.load(resources_dir + '/images/title.png').convert_alpha()
def update(self): def update(self):
self.clock.tick(self.game_fps) """
self.blit(self.bg, (0, 0)) Advance the surface logic by one frame.
"""
self.preprocess()
self.blit(self.title, (0, 0)) self.blit(self.title, (0, 0))
print(self.frames)
self.frames += 1 self.frames += 1
if self.frames > self.game_fps * self.delay: if self.frames > self.game_fps * self.delay:
self.next_surface = 'egg_select' self.next_surface = 'egg_select'