rearragned project, removed unneeded game_files folder
This commit is contained in:
2
pocket_friends/io/__init__.py
Normal file
2
pocket_friends/io/__init__.py
Normal file
@@ -0,0 +1,2 @@
|
||||
"""Subpackage for handling all I/O operations including keyboard input (GPIO input when connected to a Raspberry Pi)
|
||||
and save data reading and writing."""
|
55
pocket_friends/io/data.py
Normal file
55
pocket_friends/io/data.py
Normal file
@@ -0,0 +1,55 @@
|
||||
import pocket_friends
|
||||
import json
|
||||
|
||||
|
||||
class SaveData:
|
||||
"""
|
||||
Class used to read and write save data for the game
|
||||
|
||||
Attributes:
|
||||
attributes (dict): Dictionary containing all the attributes to read and write from a save file.
|
||||
"""
|
||||
def __init__(self):
|
||||
"""
|
||||
Constructs the object with all starting values.
|
||||
"""
|
||||
# Attributes that are saved to a file to recover upon startup.
|
||||
self.attributes = {
|
||||
'version': pocket_friends.__version__,
|
||||
'time_elapsed': 0,
|
||||
'bloop': '',
|
||||
'age': 0,
|
||||
'health': 0,
|
||||
'hunger': 0,
|
||||
'happiness': 0,
|
||||
'care_counter': 0,
|
||||
'missed_care': 0,
|
||||
'adult': 0,
|
||||
'evolution_stage': '',
|
||||
}
|
||||
|
||||
# Frame counter
|
||||
self.frames_passed = 0
|
||||
|
||||
def write_save(self):
|
||||
"""
|
||||
Writes attributes of the save object to "save.json" file.
|
||||
"""
|
||||
with open(save_dir + '/save.json', 'w') as save_file:
|
||||
json.dump(self.attributes, save_file)
|
||||
save_file.close()
|
||||
|
||||
def read_save(self):
|
||||
"""
|
||||
Reads from "save.json" and inserts into the save object's attributes dictionary. Creates file if it does not
|
||||
exist.
|
||||
"""
|
||||
# Open up the save file and read it into self.attributes.
|
||||
try:
|
||||
with open(save_dir + '/save.json', 'r') as save_file:
|
||||
self.attributes = json.load(save_file)
|
||||
save_file.close()
|
||||
|
||||
# If there is no save file, write one with the defaults.
|
||||
except FileNotFoundError:
|
||||
self.write_save()
|
72
pocket_friends/io/input_handler.py
Normal file
72
pocket_friends/io/input_handler.py
Normal file
@@ -0,0 +1,72 @@
|
||||
import pygame
|
||||
|
||||
|
||||
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.
|
||||
|
||||
Attributes:
|
||||
clock (pygame.time.Clock): Pygame clock used for input time calculations.
|
||||
last_input_tick (int): The tick that the last input was registered on.
|
||||
|
||||
"""
|
||||
|
||||
def __init__(self, pygame_clock, tick_check=True):
|
||||
"""
|
||||
Create a InputHandler object using a given Pygame clock.
|
||||
|
||||
Args:
|
||||
pygame_clock (:obj:`pygame.time.Clock`): A pygame clock to use as the clock for input time calculations.
|
||||
tick_check (bool, optional): Bool to ignore inputs that happen to quickly after another. Defaults to True.
|
||||
|
||||
"""
|
||||
self.clock = pygame_clock
|
||||
self.tick_check = tick_check
|
||||
self.last_input_tick = 0
|
||||
|
||||
def create_event(self, pressed_button):
|
||||
"""
|
||||
Create a pygame event given a GPIO code and post it to the pygame event handler.
|
||||
Args:
|
||||
pressed_button (int): The GPIO code to be registered and pressed.
|
||||
|
||||
"""
|
||||
# Register a button click so long as the last button click happened no less than two frames ago
|
||||
if self.tick_check:
|
||||
if pygame.time.get_ticks() - self.last_input_tick > self.clock.get_time() * 2:
|
||||
pygame.event.post(pygame.event.Event(pygame.KEYDOWN, {'key': pressed_button}))
|
||||
pygame.event.post(pygame.event.Event(pygame.KEYUP, {'key': pressed_button}))
|
||||
else:
|
||||
pygame.event.post(pygame.event.Event(pygame.KEYDOWN, {'key': pressed_button}))
|
||||
pygame.event.post(pygame.event.Event(pygame.KEYUP, {'key': pressed_button}))
|
||||
self.last_input_tick = pygame.time.get_ticks()
|
||||
|
||||
def handle_keyboard(self):
|
||||
"""Handle keyboard presses and generate corresponding GPIO codes to create events."""
|
||||
|
||||
# Checks if a corresponding keyboard key has been pressed. If it has, emulate a button press.
|
||||
for keyboard_event in pygame.event.get():
|
||||
if keyboard_event.type == pygame.QUIT:
|
||||
running = False
|
||||
if keyboard_event.type == pygame.KEYDOWN:
|
||||
if keyboard_event.key == pygame.K_a:
|
||||
self.create_event(pygame.K_a)
|
||||
if keyboard_event.key == pygame.K_b:
|
||||
self.create_event(pygame.K_b)
|
||||
if keyboard_event.key == pygame.K_KP_ENTER:
|
||||
self.create_event(pygame.K_KP_ENTER)
|
||||
if keyboard_event.key == pygame.K_RIGHT:
|
||||
self.create_event(pygame.K_RIGHT)
|
||||
if keyboard_event.key == pygame.K_LEFT:
|
||||
self.create_event(pygame.K_LEFT)
|
||||
if keyboard_event.key == pygame.K_DOWN:
|
||||
self.create_event(pygame.K_DOWN)
|
||||
if keyboard_event.key == pygame.K_UP:
|
||||
self.create_event(pygame.K_UP)
|
||||
if keyboard_event.key == pygame.K_ESCAPE:
|
||||
running = False
|
||||
|
||||
def update(self):
|
||||
"""Run either the GPIO handler or the keyboard handler to check for input and create events."""
|
||||
self.handle_keyboard()
|
Reference in New Issue
Block a user