added basic running from old game.py

This commit is contained in:
Nicholas Dyer 2023-05-11 11:51:51 -04:00
parent 7073bbb73c
commit d66d56630d
2 changed files with 124 additions and 0 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,57 @@
import pygame
import os
from pathlib import Path
import pocket_friends
# FPS for the entire game to run at.
game_fps = 16
# The resolution the game is rendered at.
game_res = 80
script_dir = os.path.dirname(os.path.abspath(__file__))
save_dir = os.path.join(Path.home(), '.pocket_friends')
# Gets the directory of the script for importing and the save directory
def game():
"""
Starts the game.
"""
pygame.init()
# The game 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.
screen_size = 320
pygame.mouse.set_visible(False)
pygame.display.set_caption('Pocket Friends {0}'.format(pocket_friends.__version__))
window = pygame.display.set_mode((screen_size, screen_size))
surface = pygame.Surface((game_res, game_res))
# Add an icon to the pygame window.
icon = pygame.image.load(script_dir + '/resources/images/icon/icon.png').convert_alpha()
pygame.display.set_icon(icon)
clock = pygame.time.Clock()
# Font used for small text in the game. Bigger text is usually image files.
small_font = pygame.font.Font(script_dir + '/resources/fonts/5Pts5.ttf', 10)
# Default game state when the game first starts.
game_state = 'title'
running = True
# A group of all the sprites on screen. Used to update all sprites at onc
all_sprites = pygame.sprite.Group()
# Time since last input. Used to help regulate double presses of buttons.
last_input_tick = 0
def main():
"""
Calls the game() function to start the game.
"""
game()
pygame.quit()