renamed classes to io_helpers

This commit is contained in:
2023-05-11 14:05:53 -04:00
parent 4e14204392
commit 535aa993a5
4 changed files with 1 additions and 1 deletions

View File

@@ -0,0 +1,67 @@
import pocket_friends
import json
class DataHandler:
"""
Class that handles the hardware attributes and save files.
"""
def __init__(self):
# 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 class 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 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()
def update(self):
"""
Run the game logic.
"""
self.frames_passed += 1
# Run logic of the game every second.
if self.frames_passed >= game_fps:
# Add one to the age of the bloop.
self.attributes['age'] += 1
# Save the data when the age of the bloop is a multiple of 10.
if self.attributes['age'] % 10 == 0:
self.write_save()
# Reset frame counter
self.frames_passed = 0

View File

@@ -0,0 +1,71 @@
import pygame
import importlib.util
from pocket_friends.hardware.gpio_handler import Constants, GPIOHandler
class InputHandler:
def __init__(self, pygame_clock):
self.clock = pygame_clock
try:
importlib.util.find_spec('RPi.GPIO')
import RPi.GPIO as GPIO
self.on_hardware = True
except ImportError:
import pocket_friends.development.FakeGPIO as GPIO
self.on_hardware = False
self.last_input_tick = 0
def create_event(self, pressed_button):
"""
Creates a pygame event with a given keyboard code
:param pressed_button:
"""
# Register a button click so long as the last button click happened no less than two frames ago
if pygame.time.get_ticks() - self.last_input_tick > self.clock.get_time() * 2 or not self.on_hardware:
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_gpio(self):
"""
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)
# Check if a button has been pressed. If it has, create a pygame event for it.
if GPIOHandler.get_press(code):
self.create_event(code)
def keyboard_handler(self):
"""
Simulates key presses to GPIO button presses. Also handles quitting the game.
"""
# 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(Constants.buttons.get('a'))
if keyboard_event.key == pygame.K_b:
self.create_event(Constants.buttons.get('b'))
if keyboard_event.key == pygame.K_PERIOD:
self.create_event(Constants.buttons.get('j_i'))
if keyboard_event.key == pygame.K_RIGHT:
self.create_event(Constants.buttons.get('j_r'))
if keyboard_event.key == pygame.K_LEFT:
self.create_event(Constants.buttons.get('j_l'))
if keyboard_event.key == pygame.K_DOWN:
self.create_event(Constants.buttons.get('j_d'))
if keyboard_event.key == pygame.K_UP:
self.create_event(Constants.buttons.get('j_u'))
if keyboard_event.key == pygame.K_ESCAPE:
running = False
def update(self):
if self.on_hardware:
self.handle_gpio()
else:
self.keyboard_handler()