Compare commits

..

No commits in common. "327f6cadee079e63e747ed28cfe0540749641b7f" and "eff7f2c893a70bd13c7a73a40edfef48485ca70a" have entirely different histories.

61 changed files with 110 additions and 108 deletions

View File

@ -1,4 +1 @@
"""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' __version__ = 'dev_0.0.3'

View File

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

View File

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

View File

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

View File

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

@ -1,2 +0,0 @@
"""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

@ -1,62 +0,0 @@
"""
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

@ -0,0 +1,71 @@
"""
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

@ -1,26 +1,18 @@
import pygame import pygame
import importlib.util import importlib.util
import pocket_friends.game_files.io.gpio_handler as gpio_handler from pocket_friends.game_files.io_helpers.gpio_handler import Constants, GPIOHandler
class InputHandler: class InputHandler:
"""
Class that is implemented into surfaces in order to control the
pressing of buttons on both the real hardware and on a keyboard.
"""
def __init__(self, pygame_clock): def __init__(self, pygame_clock):
self.clock = pygame_clock self.clock = pygame_clock
# If not on actual hardware, fake the GPIO in order to get it working correctly.
try: try:
importlib.util.find_spec('RPi.GPIO') importlib.util.find_spec('RPi.GPIO')
import RPi.GPIO as GPIO import RPi.GPIO as GPIO
self.on_hardware = True self.on_hardware = True
except ImportError: except ImportError:
import pocket_friends.game_files.io.fake_gpio as GPIO import pocket_friends.game_files.io_helpers.fake_gpio as GPIO
self.on_hardware = False self.on_hardware = False
self.last_input_tick = 0 self.last_input_tick = 0
def create_event(self, pressed_button): def create_event(self, pressed_button):
@ -38,11 +30,11 @@ class InputHandler:
""" """
Handles getting GPIO button presses and making a pygame event when a press is detected. Handles getting GPIO button presses and making a pygame event when a press is detected.
""" """
for pressed_button in gpio_handler.BUTTONS: for pressed_button in Constants.buttons:
code = gpio_handler.BUTTONS.get(pressed_button) code = Constants.buttons.get(pressed_button)
# Check if a button has been pressed. If it has, create a pygame event for it. # Check if a button has been pressed. If it has, create a pygame event for it.
if gpio_handler.get_press(code): if GPIOHandler.get_press(code):
self.create_event(code) self.create_event(code)
def keyboard_handler(self): def keyboard_handler(self):
@ -56,26 +48,23 @@ class InputHandler:
running = False running = False
if keyboard_event.type == pygame.KEYDOWN: if keyboard_event.type == pygame.KEYDOWN:
if keyboard_event.key == pygame.K_a: if keyboard_event.key == pygame.K_a:
self.create_event(gpio_handler.BUTTONS.get('a')) self.create_event(Constants.buttons.get('a'))
if keyboard_event.key == pygame.K_b: if keyboard_event.key == pygame.K_b:
self.create_event(gpio_handler.BUTTONS.get('b')) self.create_event(Constants.buttons.get('b'))
if keyboard_event.key == pygame.K_PERIOD: if keyboard_event.key == pygame.K_PERIOD:
self.create_event(gpio_handler.BUTTONS.get('j_i')) self.create_event(Constants.buttons.get('j_i'))
if keyboard_event.key == pygame.K_RIGHT: if keyboard_event.key == pygame.K_RIGHT:
self.create_event(gpio_handler.BUTTONS.get('j_r')) self.create_event(Constants.buttons.get('j_r'))
if keyboard_event.key == pygame.K_LEFT: if keyboard_event.key == pygame.K_LEFT:
self.create_event(gpio_handler.BUTTONS.get('j_l')) self.create_event(Constants.buttons.get('j_l'))
if keyboard_event.key == pygame.K_DOWN: if keyboard_event.key == pygame.K_DOWN:
self.create_event(gpio_handler.BUTTONS.get('j_d')) self.create_event(Constants.buttons.get('j_d'))
if keyboard_event.key == pygame.K_UP: if keyboard_event.key == pygame.K_UP:
self.create_event(gpio_handler.BUTTONS.get('j_u')) self.create_event(Constants.buttons.get('j_u'))
if keyboard_event.key == pygame.K_ESCAPE: if keyboard_event.key == pygame.K_ESCAPE:
running = False running = False
def update(self): def update(self):
"""
Run the input handler and check for inputs.
"""
if self.on_hardware: if self.on_hardware:
self.handle_gpio() self.handle_gpio()
else: else:

View File

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

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