From 4e7e37177bca1d402432c3ea6c26f4f23db27895 Mon Sep 17 00:00:00 2001 From: Nick Dyer Date: Sat, 29 May 2021 20:08:15 -0400 Subject: [PATCH] changed project file structure to better fit python standards --- pocket_friends.py | 25 --------- pocket_friends/__init__.py | 2 +- pocket_friends/__main__.py | 31 +++++++++++ pocket_friends/development/FakeGPIO.py | 2 +- pocket_friends/development/button_test.py | 2 +- pocket_friends/development/dev_menu.py | 12 ++--- pocket_friends/game_files/__init__.py | 0 pocket_friends/{ => game_files}/game.py | 50 +++++++++--------- pocket_friends/hardware/__init__.py | 0 pocket_friends/{ => hardware}/gpio_handler.py | 0 .../resources}/fonts/5Pts5.ttf | Bin .../resources}/images/bg.png | Bin .../resources}/images/clock_selector.png | Bin .../resources}/images/debug/invalid.png | Bin .../images/egg_images/blue/blue.png | Bin .../images/egg_images/dev_egg/dev_egg_0.png | Bin .../images/egg_images/dev_egg/dev_egg_1.png | Bin .../images/egg_images/green/green.png | Bin .../images/egg_images/indigo/indigo.png | Bin .../images/egg_images/orange/orange.png | Bin .../images/egg_images/rainbow/rainbow.png | Bin .../resources}/images/egg_images/red/red.png | Bin .../images/egg_images/violet/violet.png | Bin .../images/egg_images/white/white.png | Bin .../images/egg_images/yellow/yellow.png | Bin .../resources}/images/icon.png | Bin .../resources}/images/promotional.png | Bin .../resources}/images/title.png | Bin 28 files changed, 65 insertions(+), 59 deletions(-) delete mode 100644 pocket_friends.py create mode 100644 pocket_friends/__main__.py create mode 100644 pocket_friends/game_files/__init__.py rename pocket_friends/{ => game_files}/game.py (89%) create mode 100644 pocket_friends/hardware/__init__.py rename pocket_friends/{ => hardware}/gpio_handler.py (100%) rename {resources => pocket_friends/resources}/fonts/5Pts5.ttf (100%) rename {resources => pocket_friends/resources}/images/bg.png (100%) rename {resources => pocket_friends/resources}/images/clock_selector.png (100%) rename {resources => pocket_friends/resources}/images/debug/invalid.png (100%) rename {resources => pocket_friends/resources}/images/egg_images/blue/blue.png (100%) rename {resources => pocket_friends/resources}/images/egg_images/dev_egg/dev_egg_0.png (100%) rename {resources => pocket_friends/resources}/images/egg_images/dev_egg/dev_egg_1.png (100%) rename {resources => pocket_friends/resources}/images/egg_images/green/green.png (100%) rename {resources => pocket_friends/resources}/images/egg_images/indigo/indigo.png (100%) rename {resources => pocket_friends/resources}/images/egg_images/orange/orange.png (100%) rename {resources => pocket_friends/resources}/images/egg_images/rainbow/rainbow.png (100%) rename {resources => pocket_friends/resources}/images/egg_images/red/red.png (100%) rename {resources => pocket_friends/resources}/images/egg_images/violet/violet.png (100%) rename {resources => pocket_friends/resources}/images/egg_images/white/white.png (100%) rename {resources => pocket_friends/resources}/images/egg_images/yellow/yellow.png (100%) rename {resources => pocket_friends/resources}/images/icon.png (100%) rename {resources => pocket_friends/resources}/images/promotional.png (100%) rename {resources => pocket_friends/resources}/images/title.png (100%) diff --git a/pocket_friends.py b/pocket_friends.py deleted file mode 100644 index 6b123d7..0000000 --- a/pocket_friends.py +++ /dev/null @@ -1,25 +0,0 @@ -""" -Launch script for Pocket Friends. -""" -import pygame -import sys -from pocket_friends.game import main as game_main -from pocket_friends.development.dev_menu import main as dev_menu_main - -enable_dev = False - -if __name__ == '__main__': - - # enable dev mode if --dev argument is passed - if len(sys.argv) > 0: - for args in sys.argv: - if args == '--dev': - enable_dev = True - - if not enable_dev: - game_main() - else: - dev_menu_main() - - pygame.quit() - sys.exit() diff --git a/pocket_friends/__init__.py b/pocket_friends/__init__.py index 975cbfa..498af1e 100644 --- a/pocket_friends/__init__.py +++ b/pocket_friends/__init__.py @@ -1 +1 @@ -"""Imports classes for running the game.""" +"""Imports classes for running the hardware.""" diff --git a/pocket_friends/__main__.py b/pocket_friends/__main__.py new file mode 100644 index 0000000..387d89b --- /dev/null +++ b/pocket_friends/__main__.py @@ -0,0 +1,31 @@ +""" +Launch script for Pocket Friends. +""" +import pygame +import sys +from pocket_friends.game_files.game import main as game_main +from pocket_friends.development.dev_menu import main as dev_menu_main + + +def main(): + enable_dev = False + + if __name__ == '__main__': + + # enable dev mode if --dev argument is passed + if len(sys.argv) > 0: + for args in sys.argv: + if args == '--dev': + enable_dev = True + + if not enable_dev: + game_main() + else: + dev_menu_main() + + pygame.quit() + sys.exit() + + +if __name__ == "__main__": + main() diff --git a/pocket_friends/development/FakeGPIO.py b/pocket_friends/development/FakeGPIO.py index c974005..23a82c6 100644 --- a/pocket_friends/development/FakeGPIO.py +++ b/pocket_friends/development/FakeGPIO.py @@ -1,6 +1,6 @@ """ Module used to fake the RPi.GPIO module so that -the game can be run without the actual hardware. +the hardware can be run without the actual hardware. """ # Constants used by RPi.GPIO diff --git a/pocket_friends/development/button_test.py b/pocket_friends/development/button_test.py index b89813b..159627b 100644 --- a/pocket_friends/development/button_test.py +++ b/pocket_friends/development/button_test.py @@ -2,7 +2,7 @@ Module to test the GPIO input on the Raspberry Pi. """ from collections import deque -from ..gpio_handler import Constants, GPIOHandler +from pocket_friends.hardware.gpio_handler import Constants, GPIOHandler def button_test(): diff --git a/pocket_friends/development/dev_menu.py b/pocket_friends/development/dev_menu.py index 3c9b35b..8f638d7 100644 --- a/pocket_friends/development/dev_menu.py +++ b/pocket_friends/development/dev_menu.py @@ -1,14 +1,14 @@ """ -Development menu for the game on Raspberry Pi. NOTE: THIS DOES NOTHING ON A COMPUTER! +Development menu for the hardware on Raspberry Pi. NOTE: THIS DOES NOTHING ON A COMPUTER! """ -import pocket_friends.game +import pocket_friends.game_files.game import importlib.util import os import pygame import time from .button_test import button_test from .menus import Menu -from ..gpio_handler import GPIOHandler, Constants +from pocket_friends.hardware.gpio_handler import GPIOHandler, Constants dev_version = '0.0.1' @@ -40,10 +40,10 @@ def clear_screen(): def start_game(): """ - Cleans the GPIO and starts the game. + Cleans the GPIO and starts the hardware. """ GPIOHandler.teardown() - pocket_friends.game.main() + pocket_friends.game_files.game.main() pygame.quit() GPIOHandler.setup() @@ -95,7 +95,7 @@ def main(): # The following defines all of the options in the various different menus. - main_menu = Menu('Pocket Friends Dev Menu {0}\nGame Version {1}'.format(dev_version, pocket_friends.game.version)) + main_menu = Menu('Pocket Friends Dev Menu {0}\nGame Version {1}'.format(dev_version, pocket_friends.game_files.game.version)) main_menu.add_option(Menu.Option('Start Game', start_game)) main_menu.add_option(Menu.Option('Button Test', run_button_test)) main_menu.add_option(Menu.Option('Restart Dev Menu', quit_with_error)) diff --git a/pocket_friends/game_files/__init__.py b/pocket_friends/game_files/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/pocket_friends/game.py b/pocket_friends/game_files/game.py similarity index 89% rename from pocket_friends/game.py rename to pocket_friends/game_files/game.py index ca1614b..a897ad7 100644 --- a/pocket_friends/game.py +++ b/pocket_friends/game_files/game.py @@ -1,5 +1,5 @@ """ -Main file for the entire game. Controls everything except for GPIO input. +Main file for the entire hardware. Controls everything except for GPIO input. """ from collections import deque import importlib.util @@ -7,7 +7,7 @@ import json import os import pygame from pygame.locals import * -from .gpio_handler import Constants, GPIOHandler +from pocket_friends.hardware.gpio_handler import Constants, GPIOHandler version = '0.0.1' game_fps = 16 @@ -15,7 +15,7 @@ game_fps = 16 class FileHandler: """ - Class that handles the game attributes and save files. + Class that handles the hardware attributes and save files. """ def __init__(self): @@ -69,7 +69,7 @@ class SelectionEgg(pygame.sprite.Sprite): def __init__(self, egg_color): pygame.sprite.Sprite.__init__(self) - image_directory = 'resources/images/egg_images/{0}'.format(egg_color) + image_directory = 'pocket_friends/resources/images/egg_images/{0}'.format(egg_color) # Load the egg from the given color and get the bounding rectangle for the image. self.images = [] @@ -113,14 +113,14 @@ except ImportError: def game(): """ - Starts the game. + Starts the hardware. """ pygame.init() # Hide the cursor for the Pi display. pygame.mouse.set_visible(False) - # The game is normally rendered at 80 pixels and upscaled from there. If changing displays, change the + # The hardware is normally rendered at 80 pixels and upscaled from there. If changing displays, change the # screen_size to reflect what the resolution of the new display is. rendered_size = 80 screen_size = 800 @@ -133,10 +133,10 @@ def game(): clock = pygame.time.Clock() - # Font used for small text in the game. Bigger text is usually image files. - small_font = pygame.font.Font('resources/fonts/5Pts5.ttf', 10) + # Font used for small text in the hardware. Bigger text is usually image files. + small_font = pygame.font.Font('pocket_friends/resources/fonts/5Pts5.ttf', 10) - # Default game state when the game first starts. + # Default hardware state when the hardware first starts. game_state = 'title' running = True file_handler = FileHandler() @@ -147,7 +147,7 @@ def game(): # Start the GPIO handler to take in buttons from the RPi HAT. GPIOHandler.setup() - # Dev code used to exit the game. Default Down, Down, Up, Up, Down, Down, Up, Up, A, A, B + # Dev code used to exit the hardware. Default Down, Down, Up, Up, Down, Down, Up, Up, A, A, B dev_code = deque() for button in [Constants.buttons.get('j_d'), Constants.buttons.get('j_d'), Constants.buttons.get('j_u'), Constants.buttons.get('j_u'), Constants.buttons.get('j_d'), Constants.buttons.get('j_d'), @@ -177,9 +177,9 @@ def game(): def draw_bg(): """ - Draws the main game background image onto a given surface. + Draws the main hardware background image onto a given surface. """ - bg_image = pygame.image.load('resources/images/bg.png').convert() + bg_image = pygame.image.load('pocket_friends/resources/images/bg.png').convert() surface.blit(bg_image, (0, 0)) def log_button(pressed_button): @@ -226,7 +226,7 @@ def game(): def keyboard_handler(): """ - Simulates key presses to GPIO button presses. Also handles quitting the game. + Simulates key presses to GPIO button presses. Also handles quitting the hardware. """ nonlocal running @@ -254,10 +254,10 @@ def game(): def pre_handler(): """ - Runs at the beginning of each loop, handles drawing the background, controlling game speed, and + Runs at the beginning of each loop, handles drawing the background, controlling hardware speed, and controlling the GPIO button inputs and keyboard handler """ - # Regulate the speed of the game. + # Regulate the speed of the hardware. clock.tick(game_fps) # Handle all inputs for both debugging and real GPIO button presses. @@ -274,11 +274,11 @@ def game(): pre_handler() # Draw the title image in the middle of the screen. - title_image = pygame.image.load('resources/images/title.png').convert_alpha() + title_image = pygame.image.load('pocket_friends/resources/images/title.png').convert_alpha() surface.blit(title_image, (0, 0)) draw() - # Show the title for 1 second then move on to the initialization phase of the game. + # Show the title for 1 second then move on to the initialization phase of the hardware. pygame.time.wait(1000) game_state = 'init' @@ -294,8 +294,8 @@ def game(): # Read the save file. file_handler.read_save() - # Determines if it is a new game or not by looking at the evolution stage. If it is -1, the egg has - # not been created yet, and the game sends you to the egg selection screen. If not, the game sends + # Determines if it is a new hardware or not by looking at the evolution stage. If it is -1, the egg has + # not been created yet, and the hardware sends you to the egg selection screen. If not, the hardware sends # you to the playground. if file_handler.attributes['evolution_stage'] == -1: game_state = 'egg_select' @@ -419,7 +419,7 @@ def game(): submenu = 'egg_info' # Draws the cursor on screen. - cursor = pygame.image.load('resources/images/clock_selector.png').convert_alpha() + cursor = pygame.image.load('pocket_friends/resources/images/clock_selector.png').convert_alpha() surface.blit(cursor, get_cursor_coords(selected)) draw() @@ -430,7 +430,7 @@ def game(): for event in pygame.event.get(): if event.type == pygame.KEYDOWN: if event.key == Constants.buttons.get('a'): - # Go to an invalid game state if continuing. + # Go to an invalid hardware state if continuing. game_state = None if event.key == Constants.buttons.get('b'): # Go back to the egg selection screen. @@ -446,17 +446,17 @@ def game(): game_state = None else: - # Error screen. This appears when an invalid game state has been selected. + # Error screen. This appears when an invalid hardware state has been selected. all_sprites.empty() - frames_passed = 0 # Counter for frames, helps ensure the game isnt frozen. + frames_passed = 0 # Counter for frames, helps ensure the hardware isnt frozen. while running and game_state != 'title': pre_handler() # Draw the error screen - error_screen = pygame.image.load('resources/images/debug/invalid.png').convert_alpha() + error_screen = pygame.image.load('pocket_friends/resources/images/debug/invalid.png').convert_alpha() surface.blit(error_screen, (0, -8)) # Counts the frames passed. Resets every second. @@ -479,7 +479,7 @@ def game(): def main(): """ - Calls the game() function to start the game. + Calls the hardware() function to start the hardware. """ game() diff --git a/pocket_friends/hardware/__init__.py b/pocket_friends/hardware/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/pocket_friends/gpio_handler.py b/pocket_friends/hardware/gpio_handler.py similarity index 100% rename from pocket_friends/gpio_handler.py rename to pocket_friends/hardware/gpio_handler.py diff --git a/resources/fonts/5Pts5.ttf b/pocket_friends/resources/fonts/5Pts5.ttf similarity index 100% rename from resources/fonts/5Pts5.ttf rename to pocket_friends/resources/fonts/5Pts5.ttf diff --git a/resources/images/bg.png b/pocket_friends/resources/images/bg.png similarity index 100% rename from resources/images/bg.png rename to pocket_friends/resources/images/bg.png diff --git a/resources/images/clock_selector.png b/pocket_friends/resources/images/clock_selector.png similarity index 100% rename from resources/images/clock_selector.png rename to pocket_friends/resources/images/clock_selector.png diff --git a/resources/images/debug/invalid.png b/pocket_friends/resources/images/debug/invalid.png similarity index 100% rename from resources/images/debug/invalid.png rename to pocket_friends/resources/images/debug/invalid.png diff --git a/resources/images/egg_images/blue/blue.png b/pocket_friends/resources/images/egg_images/blue/blue.png similarity index 100% rename from resources/images/egg_images/blue/blue.png rename to pocket_friends/resources/images/egg_images/blue/blue.png diff --git a/resources/images/egg_images/dev_egg/dev_egg_0.png b/pocket_friends/resources/images/egg_images/dev_egg/dev_egg_0.png similarity index 100% rename from resources/images/egg_images/dev_egg/dev_egg_0.png rename to pocket_friends/resources/images/egg_images/dev_egg/dev_egg_0.png diff --git a/resources/images/egg_images/dev_egg/dev_egg_1.png b/pocket_friends/resources/images/egg_images/dev_egg/dev_egg_1.png similarity index 100% rename from resources/images/egg_images/dev_egg/dev_egg_1.png rename to pocket_friends/resources/images/egg_images/dev_egg/dev_egg_1.png diff --git a/resources/images/egg_images/green/green.png b/pocket_friends/resources/images/egg_images/green/green.png similarity index 100% rename from resources/images/egg_images/green/green.png rename to pocket_friends/resources/images/egg_images/green/green.png diff --git a/resources/images/egg_images/indigo/indigo.png b/pocket_friends/resources/images/egg_images/indigo/indigo.png similarity index 100% rename from resources/images/egg_images/indigo/indigo.png rename to pocket_friends/resources/images/egg_images/indigo/indigo.png diff --git a/resources/images/egg_images/orange/orange.png b/pocket_friends/resources/images/egg_images/orange/orange.png similarity index 100% rename from resources/images/egg_images/orange/orange.png rename to pocket_friends/resources/images/egg_images/orange/orange.png diff --git a/resources/images/egg_images/rainbow/rainbow.png b/pocket_friends/resources/images/egg_images/rainbow/rainbow.png similarity index 100% rename from resources/images/egg_images/rainbow/rainbow.png rename to pocket_friends/resources/images/egg_images/rainbow/rainbow.png diff --git a/resources/images/egg_images/red/red.png b/pocket_friends/resources/images/egg_images/red/red.png similarity index 100% rename from resources/images/egg_images/red/red.png rename to pocket_friends/resources/images/egg_images/red/red.png diff --git a/resources/images/egg_images/violet/violet.png b/pocket_friends/resources/images/egg_images/violet/violet.png similarity index 100% rename from resources/images/egg_images/violet/violet.png rename to pocket_friends/resources/images/egg_images/violet/violet.png diff --git a/resources/images/egg_images/white/white.png b/pocket_friends/resources/images/egg_images/white/white.png similarity index 100% rename from resources/images/egg_images/white/white.png rename to pocket_friends/resources/images/egg_images/white/white.png diff --git a/resources/images/egg_images/yellow/yellow.png b/pocket_friends/resources/images/egg_images/yellow/yellow.png similarity index 100% rename from resources/images/egg_images/yellow/yellow.png rename to pocket_friends/resources/images/egg_images/yellow/yellow.png diff --git a/resources/images/icon.png b/pocket_friends/resources/images/icon.png similarity index 100% rename from resources/images/icon.png rename to pocket_friends/resources/images/icon.png diff --git a/resources/images/promotional.png b/pocket_friends/resources/images/promotional.png similarity index 100% rename from resources/images/promotional.png rename to pocket_friends/resources/images/promotional.png diff --git a/resources/images/title.png b/pocket_friends/resources/images/title.png similarity index 100% rename from resources/images/title.png rename to pocket_friends/resources/images/title.png