restructured directories to make more sense of modules and sub-modules

This commit is contained in:
Nicholas Dyer 2023-05-11 17:59:06 -04:00
parent a8674f073e
commit 327f6cadee
61 changed files with 97 additions and 110 deletions

View File

@ -1 +1,4 @@
"""Pocket Friends is a game where you raise your own little pocket friend! These pocket friends, called bloops,
are great little companions to have! You can feed them, play with them, and watch them grow up!"""
__version__ = 'dev_0.0.3'

View File

@ -5,8 +5,7 @@ import os
import pygame
import sys
from pathlib import Path
from pocket_friends.game_files.game import main as game_main
#from pocket_friends.development.dev_menu import main as dev_menu_main
import pocket_friends.game_files.game as game
if __name__ == '__main__':
enable_dev = False
@ -14,19 +13,11 @@ if __name__ == '__main__':
# enable dev mode if --dev argument is passed
if len(sys.argv) > 0:
for args in sys.argv:
#if args == '--dev': [reimplement later]
# enable_dev = True
if args == '--delete-save':
save_dir = os.path.join(Path.home(), '.pocket_friends')
os.remove(save_dir + '/save.json')
# Dev menu disabled for now, will reimplement later
#if not enable_dev:
# game_main()
#else:
# dev_menu_main()
game_main()
game.main()
pygame.quit()
sys.exit()

View File

@ -2,7 +2,7 @@
Module to test the GPIO input on the Raspberry Pi.
"""
from collections import deque
from pocket_friends.game_files.io_helpers.gpio_handler import Constants, GPIOHandler
from pocket_friends.game_files.io.gpio_handler import Constants, GPIOHandler
def button_test():

View File

@ -8,13 +8,13 @@ import pygame
import time
from .button_test import button_test
from .menus import Menu
from pocket_friends.game_files.io_helpers.gpio_handler import GPIOHandler, Constants
from pocket_friends.game_files.io.gpio_handler import GPIOHandler, Constants
try:
importlib.util.find_spec('RPi.GPIO')
import RPi.GPIO as GPIO
except ImportError:
import pocket_friends.game_files.io_helpers.fake_gpio as GPIO
import pocket_friends.game_files.io.fake_gpio as GPIO
# Global variable to keep track of the current menu.
menu = 'main'

View File

@ -0,0 +1,2 @@
"""Sub-package for handling all I/O operations including keyboard input (GPIO input when connected to a Raspberry Pi)
and save data reading and writing."""

View File

@ -0,0 +1,62 @@
"""
Handle inputs from the GPIO pins on the Raspberry Pi and converting them to events to be used in other places (pygame, etc.)
"""
import importlib.util
# If the RPi.GPIO module is not found (aka the program is not running on a Pi), import the fake
# GPIO module instead to prevent a crash.
try:
importlib.util.find_spec('RPi.GPIO')
import RPi.GPIO as GPIO
except ImportError:
import pocket_friends.game_files.io.fake_gpio as GPIO
# Dictionary of all the buttons used and what their corresponding GPIO codes are
BUTTONS = {
'a': 31, # A button
'b': 29, # B button
'j_i': 7, # Joystick in
'j_u': 11, # Joystick up
'j_d': 15, # Joystick down
'j_l': 13, # Joystick left
'j_r': 16 # Joystick right
}
def setup():
"""
Primes the GPIO pins for reading the inputs of the buttons.
"""
GPIO.setmode(GPIO.BOARD)
GPIO.setup(BUTTONS.get('a'), GPIO.IN)
GPIO.setup(BUTTONS.get('b'), GPIO.IN)
GPIO.setup(BUTTONS.get('j_i'), GPIO.IN)
GPIO.setup(BUTTONS.get('j_u'), GPIO.IN)
GPIO.setup(BUTTONS.get('j_d'), GPIO.IN)
GPIO.setup(BUTTONS.get('j_l'), GPIO.IN)
GPIO.setup(BUTTONS.get('j_r'), GPIO.IN)
GPIO.add_event_detect(BUTTONS.get('a'), GPIO.FALLING)
GPIO.add_event_detect(BUTTONS.get('b'), GPIO.FALLING)
GPIO.add_event_detect(BUTTONS.get('j_i'), GPIO.FALLING)
GPIO.add_event_detect(BUTTONS.get('j_u'), GPIO.FALLING)
GPIO.add_event_detect(BUTTONS.get('j_d'), GPIO.FALLING)
GPIO.add_event_detect(BUTTONS.get('j_l'), GPIO.FALLING)
GPIO.add_event_detect(BUTTONS.get('j_r'), GPIO.FALLING)
def teardown():
"""
Cleans up the GPIO handler.
"""
GPIO.cleanup()
def get_press(button):
"""
Returns true if a button has changed from not pressed to pressed.
:param button: button to be detected
:return: True if the button is has been pressed, False otherwise
"""
return GPIO.event_detected(button)

View File

@ -1,6 +1,6 @@
import pygame
import importlib.util
from pocket_friends.game_files.io_helpers.gpio_handler import Constants, GPIOHandler
import pocket_friends.game_files.io.gpio_handler as gpio_handler
class InputHandler:
@ -18,7 +18,7 @@ class InputHandler:
import RPi.GPIO as GPIO
self.on_hardware = True
except ImportError:
import pocket_friends.game_files.io_helpers.fake_gpio as GPIO
import pocket_friends.game_files.io.fake_gpio as GPIO
self.on_hardware = False
self.last_input_tick = 0
@ -38,11 +38,11 @@ class InputHandler:
"""
Handles getting GPIO button presses and making a pygame event when a press is detected.
"""
for pressed_button in Constants.buttons:
code = Constants.buttons.get(pressed_button)
for pressed_button in gpio_handler.BUTTONS:
code = gpio_handler.BUTTONS.get(pressed_button)
# Check if a button has been pressed. If it has, create a pygame event for it.
if GPIOHandler.get_press(code):
if gpio_handler.get_press(code):
self.create_event(code)
def keyboard_handler(self):
@ -56,19 +56,19 @@ class InputHandler:
running = False
if keyboard_event.type == pygame.KEYDOWN:
if keyboard_event.key == pygame.K_a:
self.create_event(Constants.buttons.get('a'))
self.create_event(gpio_handler.BUTTONS.get('a'))
if keyboard_event.key == pygame.K_b:
self.create_event(Constants.buttons.get('b'))
self.create_event(gpio_handler.BUTTONS.get('b'))
if keyboard_event.key == pygame.K_PERIOD:
self.create_event(Constants.buttons.get('j_i'))
self.create_event(gpio_handler.BUTTONS.get('j_i'))
if keyboard_event.key == pygame.K_RIGHT:
self.create_event(Constants.buttons.get('j_r'))
self.create_event(gpio_handler.BUTTONS.get('j_r'))
if keyboard_event.key == pygame.K_LEFT:
self.create_event(Constants.buttons.get('j_l'))
self.create_event(gpio_handler.BUTTONS.get('j_l'))
if keyboard_event.key == pygame.K_DOWN:
self.create_event(Constants.buttons.get('j_d'))
self.create_event(gpio_handler.BUTTONS.get('j_d'))
if keyboard_event.key == pygame.K_UP:
self.create_event(Constants.buttons.get('j_u'))
self.create_event(gpio_handler.BUTTONS.get('j_u'))
if keyboard_event.key == pygame.K_ESCAPE:
running = False

View File

@ -1,71 +0,0 @@
"""
Module that helps with the handling of taking inputs from the GPIO pins on the Raspberry
Pi and converting them to events to be used in other places (pygame, etc.)
"""
import importlib.util
try:
importlib.util.find_spec('RPi.GPIO')
import RPi.GPIO as GPIO
except ImportError:
import pocket_friends.game_files.io_helpers.fake_gpio as GPIO
class Constants:
"""
Contains the constants used by the HAT to read in buttons
"""
buttons = {
'a': 31, # A button
'b': 29, # B button
'j_i': 7, # Joystick in
'j_u': 11, # Joystick up
'j_d': 15, # Joystick down
'j_l': 13, # Joystick left
'j_r': 16 # Joystick right
}
class GPIOHandler:
"""
Class to handle the GPIO inputs from the buttons.
"""
@staticmethod
def setup():
"""
Primes the GPIO pins for reading the inputs of the buttons.
"""
GPIO.setmode(GPIO.BOARD)
GPIO.setup(Constants.buttons.get('a'), GPIO.IN)
GPIO.setup(Constants.buttons.get('b'), GPIO.IN)
GPIO.setup(Constants.buttons.get('j_i'), GPIO.IN)
GPIO.setup(Constants.buttons.get('j_u'), GPIO.IN)
GPIO.setup(Constants.buttons.get('j_d'), GPIO.IN)
GPIO.setup(Constants.buttons.get('j_l'), GPIO.IN)
GPIO.setup(Constants.buttons.get('j_r'), GPIO.IN)
GPIO.add_event_detect(Constants.buttons.get('a'), GPIO.FALLING)
GPIO.add_event_detect(Constants.buttons.get('b'), GPIO.FALLING)
GPIO.add_event_detect(Constants.buttons.get('j_i'), GPIO.FALLING)
GPIO.add_event_detect(Constants.buttons.get('j_u'), GPIO.FALLING)
GPIO.add_event_detect(Constants.buttons.get('j_d'), GPIO.FALLING)
GPIO.add_event_detect(Constants.buttons.get('j_l'), GPIO.FALLING)
GPIO.add_event_detect(Constants.buttons.get('j_r'), GPIO.FALLING)
@staticmethod
def teardown():
"""
Cleans up the GPIO handler.
"""
GPIO.cleanup()
@staticmethod
def get_press(button):
"""
Returns true if a button has moved from not pressed to pressed.
:param button: button to be detected
:return: True if the button is has been pressed, False otherwise
"""
return GPIO.event_detected(button)

View File

Before

Width:  |  Height:  |  Size: 3.7 KiB

After

Width:  |  Height:  |  Size: 3.7 KiB

View File

Before

Width:  |  Height:  |  Size: 8.3 KiB

After

Width:  |  Height:  |  Size: 8.3 KiB

View File

Before

Width:  |  Height:  |  Size: 1.1 KiB

After

Width:  |  Height:  |  Size: 1.1 KiB

View File

Before

Width:  |  Height:  |  Size: 2.6 KiB

After

Width:  |  Height:  |  Size: 2.6 KiB

View File

Before

Width:  |  Height:  |  Size: 6.9 KiB

After

Width:  |  Height:  |  Size: 6.9 KiB

View File

Before

Width:  |  Height:  |  Size: 864 B

After

Width:  |  Height:  |  Size: 864 B

View File

Before

Width:  |  Height:  |  Size: 21 KiB

After

Width:  |  Height:  |  Size: 21 KiB

View File

Before

Width:  |  Height:  |  Size: 246 B

After

Width:  |  Height:  |  Size: 246 B

View File

Before

Width:  |  Height:  |  Size: 218 B

After

Width:  |  Height:  |  Size: 218 B

View File

Before

Width:  |  Height:  |  Size: 188 B

After

Width:  |  Height:  |  Size: 188 B

View File

Before

Width:  |  Height:  |  Size: 178 B

After

Width:  |  Height:  |  Size: 178 B

View File

Before

Width:  |  Height:  |  Size: 217 B

After

Width:  |  Height:  |  Size: 217 B

View File

Before

Width:  |  Height:  |  Size: 5.5 KiB

After

Width:  |  Height:  |  Size: 5.5 KiB

View File

Before

Width:  |  Height:  |  Size: 258 B

After

Width:  |  Height:  |  Size: 258 B

View File

Before

Width:  |  Height:  |  Size: 213 B

After

Width:  |  Height:  |  Size: 213 B

View File

Before

Width:  |  Height:  |  Size: 213 B

After

Width:  |  Height:  |  Size: 213 B

View File

Before

Width:  |  Height:  |  Size: 7.1 KiB

After

Width:  |  Height:  |  Size: 7.1 KiB

View File

Before

Width:  |  Height:  |  Size: 20 KiB

After

Width:  |  Height:  |  Size: 20 KiB

View File

@ -1,7 +1,7 @@
import pygame
from . import sprites
from pocket_friends.game_files.io_helpers.gpio_handler import Constants
from ..io_helpers.input_handler import InputHandler
import pocket_friends.game_files.io.gpio_handler as gpio_handler
from ..io.input_handler import InputHandler
class Surface(pygame.Surface):
@ -133,15 +133,15 @@ class Surface(pygame.Surface):
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == Constants.buttons.get('j_r'):
if event.key == gpio_handler.BUTTONS.get('j_r'):
self.sel_right()
if event.key == Constants.buttons.get('j_l'):
if event.key == gpio_handler.BUTTONS.get('j_l'):
self.sel_left()
if event.key == Constants.buttons.get('j_d'):
if event.key == gpio_handler.BUTTONS.get('j_d'):
self.sel_down()
if event.key == Constants.buttons.get('j_u'):
if event.key == gpio_handler.BUTTONS.get('j_u'):
self.sel_up()
if event.key == Constants.buttons.get('a'):
if event.key == gpio_handler.BUTTONS.get('a'):
self.additional_args = {'selected_egg': self.selected_color}
self.next_surface = 'selection_info'
self.running = False

View File

@ -1,8 +1,8 @@
import pygame
from . import sprites
from pocket_friends.game_files.io_helpers.gpio_handler import Constants
import pocket_friends.game_files.io.gpio_handler as gpio_handler
from .sprites import SelectionEgg
from ..io_helpers.input_handler import InputHandler
from ..io.input_handler import InputHandler
class Surface(pygame.Surface):
@ -46,15 +46,15 @@ class Surface(pygame.Surface):
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == Constants.buttons.get('j_d'):
if event.key == gpio_handler.BUTTONS.get('j_d'):
# Scroll down on the info screen.
self.info_text.scroll_down()
if event.key == Constants.buttons.get('j_u'):
if event.key == gpio_handler.BUTTONS.get('j_u'):
# Scroll up on the info screen.
self.info_text.scroll_up()
if event.key == Constants.buttons.get('a'):
if event.key == gpio_handler.BUTTONS.get('a'):
pass
if event.key == Constants.buttons.get('b'):
if event.key == gpio_handler.BUTTONS.get('b'):
self.running = False
self.additional_args = {'selected_color': self.selected_egg}
self.next_surface = 'egg_select'