From 8c49c62f66be70123e8f1ad5c00c7b4459795ac0 Mon Sep 17 00:00:00 2001 From: Nicholas Dyer Date: Mon, 15 May 2023 00:27:14 -0400 Subject: [PATCH] marked error_screen as a protected module, added --dev launch option --- pocket_friends/__main__.py | 5 ++-- pocket_friends/_development/_dev.py | 1 + .../surfaces/_error_screen.py} | 0 pocket_friends/game.py | 26 +++++++++++++------ 4 files changed, 22 insertions(+), 10 deletions(-) rename pocket_friends/{surfaces/error_screen.py => _development/surfaces/_error_screen.py} (100%) diff --git a/pocket_friends/__main__.py b/pocket_friends/__main__.py index c742d8a..3ab8009 100644 --- a/pocket_friends/__main__.py +++ b/pocket_friends/__main__.py @@ -11,7 +11,6 @@ if __name__ == '__main__': enable_dev = False resolution = 240 - # enable dev mode if --dev argument is passed if len(sys.argv) > 0: for arg in sys.argv: if arg == '--delete-save': @@ -19,8 +18,10 @@ if __name__ == '__main__': os.remove(save_dir + '/save.json') if '--res=' in arg: resolution = int(arg.split('=')[1]) + if arg == '--dev': + enable_dev = True - game.start_game(resolution) + game.start_game(resolution=resolution, dev=enable_dev) pygame.quit() sys.exit() diff --git a/pocket_friends/_development/_dev.py b/pocket_friends/_development/_dev.py index e66511e..72b83af 100644 --- a/pocket_friends/_development/_dev.py +++ b/pocket_friends/_development/_dev.py @@ -12,6 +12,7 @@ def shutdown_system(): os.system('sudo shutdown now') def update(): + pygame.quit() os.system('bash ~/update.sh') sys.exit(1) diff --git a/pocket_friends/surfaces/error_screen.py b/pocket_friends/_development/surfaces/_error_screen.py similarity index 100% rename from pocket_friends/surfaces/error_screen.py rename to pocket_friends/_development/surfaces/_error_screen.py diff --git a/pocket_friends/game.py b/pocket_friends/game.py index 9ae2f9b..6354e3b 100644 --- a/pocket_friends/game.py +++ b/pocket_friends/game.py @@ -6,22 +6,20 @@ import importlib valid_surfaces = [ '_dev_menu', '_black_screen', + '_error_screen', 'title', 'egg_select', 'selection_info', - 'error_screen' ] # Add all the surface modules to a dictionary for easy switching surface_modules = {} for module in valid_surfaces: - print(module[0]) if module[0] == '_': surface_modules[module] = importlib.import_module('pocket_friends._development.surfaces.{0}'.format(module)) else: surface_modules[module] = importlib.import_module('pocket_friends.surfaces.{0}'.format(module)) -starting_surface = 'title' -print(surface_modules) + # FPS for the game to run at. game_fps = 16 # The internal resolution of the game @@ -35,12 +33,13 @@ resources_dir = script_dir + '/resources' os.environ['SDL_FBDEV'] = '/dev/fb1' -def start_game(resolution=240): +def start_game(resolution, dev): """ Starts the game. Args: resolution (int, optional): Resolution to display the game at. Defaults to 240. + dev (bool): Boolean to enable the developer menu at start or not """ pygame.init() @@ -48,6 +47,11 @@ def start_game(resolution=240): # Hide the cursor for the Pi display. pygame.mouse.set_visible(False) + if dev: + starting_surface = '_dev_menu' + else: + starting_surface = 'title' + window = pygame.display.set_mode((resolution, resolution)) surface = surface_modules.get(starting_surface).Surface((game_res, game_res), resources_dir, game_fps) @@ -67,15 +71,21 @@ def start_game(resolution=240): frame = pygame.transform.scale(surface, (resolution, resolution)) window.blit(frame, frame.get_rect()) + # When the current surface is not running, check to make sure that the next surface will be valid if not surface.running: + # Force the dev menu to appear if the flag has been passed if surface.dev_override: next_surface = '_dev_menu' else: next_surface = surface.next_surface - additional_args = surface.additional_args + + # Send to the error screen if the given surface isn't a valid one if next_surface not in valid_surfaces: - print(next_surface) - next_surface = 'error_screen' + next_surface = '_error_screen' + + # Get the additional args to pass on from the ending surface to the next one + additional_args = surface.additional_args + # Create the new surface and pass through the additional argss surface = surface_modules.get(next_surface).Surface((game_res, game_res), resources_dir, game_fps, **additional_args) pygame.display.flip()