2021-05-29 17:18:37 -04:00
|
|
|
"""
|
2021-06-16 17:44:27 -04:00
|
|
|
Main file for the entire game. Controls everything except for GPIO input.
|
2021-05-29 17:18:37 -04:00
|
|
|
"""
|
|
|
|
from collections import deque
|
|
|
|
import importlib.util
|
|
|
|
import json
|
|
|
|
import os
|
2021-05-29 22:40:41 -04:00
|
|
|
from pathlib import Path
|
|
|
|
import pocket_friends
|
2021-05-29 17:18:37 -04:00
|
|
|
import pygame
|
|
|
|
from pygame.locals import *
|
2021-05-29 21:42:33 -04:00
|
|
|
from ..hardware.gpio_handler import Constants, GPIOHandler
|
2021-05-29 17:18:37 -04:00
|
|
|
|
2021-05-29 22:30:12 -04:00
|
|
|
# FPS for the entire game to run at.
|
2021-05-29 17:18:37 -04:00
|
|
|
game_fps = 16
|
2021-06-05 18:27:53 -04:00
|
|
|
# The resolution the game is rendered at.
|
|
|
|
game_res = 80
|
2021-05-29 22:30:12 -04:00
|
|
|
|
|
|
|
# Gets the directory of the script for importing and the save directory
|
2021-05-30 00:34:07 -04:00
|
|
|
script_dir = os.path.dirname(os.path.abspath(__file__))
|
2021-05-29 22:30:12 -04:00
|
|
|
save_dir = os.path.join(Path.home(), '.pocket_friends')
|
|
|
|
|
|
|
|
# Tries to make the save directory. Does nothing if it already exists.
|
|
|
|
try:
|
|
|
|
os.mkdir(save_dir)
|
|
|
|
except FileExistsError:
|
|
|
|
pass
|
2021-05-29 17:18:37 -04:00
|
|
|
|
|
|
|
|
2021-06-12 22:16:54 -04:00
|
|
|
class SpriteSheet:
|
|
|
|
"""
|
|
|
|
Imports a sprite sheet as separate pygame images given an image file and a json file.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, sprite_sheet, texture_json):
|
|
|
|
# Load in whole sprite sheet as one image.
|
|
|
|
self.sprite_sheet = pygame.image.load(sprite_sheet).convert_alpha()
|
|
|
|
self.images = []
|
|
|
|
|
|
|
|
# Get the sprite sheet json file.
|
|
|
|
with open(texture_json, 'r') as json_file:
|
|
|
|
self.img_attrib = json.load(json_file)
|
|
|
|
json_file.close()
|
|
|
|
|
2021-06-16 03:40:02 -04:00
|
|
|
# Count for how many images have been added in the image list
|
2021-06-12 22:16:54 -04:00
|
|
|
image_count = 0
|
|
|
|
|
|
|
|
# Get the sprite size as a tuple
|
|
|
|
sprite_size = self.img_attrib['width'], self.img_attrib['height']
|
|
|
|
|
|
|
|
# Iterate through every image location on the sprite sheet given the sprite size
|
|
|
|
for i in range(self.sprite_sheet.get_size()[1] // sprite_size[1]):
|
|
|
|
i *= sprite_size[1]
|
|
|
|
for j in range(self.sprite_sheet.get_size()[0] // sprite_size[0]):
|
|
|
|
j *= sprite_size[0]
|
|
|
|
|
|
|
|
# Create a new transparent surface
|
|
|
|
sprite = pygame.Surface(sprite_size, SRCALPHA)
|
|
|
|
# Blit the sprite onto the image
|
|
|
|
sprite.blit(self.sprite_sheet, (0, 0), (j, i, sprite_size[0], sprite_size[1]))
|
|
|
|
# Add the image to the list of images
|
|
|
|
self.images.append(sprite)
|
|
|
|
|
|
|
|
image_count += 1
|
|
|
|
|
|
|
|
# Break the loop if the specified number of frames has been reached.
|
|
|
|
if image_count >= self.img_attrib['frames']:
|
|
|
|
break
|
|
|
|
if image_count >= self.img_attrib['frames']:
|
|
|
|
break
|
|
|
|
|
|
|
|
|
2021-06-12 23:10:59 -04:00
|
|
|
class DataHandler:
|
2021-05-29 17:18:37 -04:00
|
|
|
"""
|
2021-05-29 20:08:15 -04:00
|
|
|
Class that handles the hardware attributes and save files.
|
2021-05-29 17:18:37 -04:00
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
# Attributes that are saved to a file to recover upon startup.
|
|
|
|
self.attributes = {
|
2021-06-05 18:32:51 -04:00
|
|
|
'version': pocket_friends.__version__,
|
2021-05-29 17:18:37 -04:00
|
|
|
'time_elapsed': 0,
|
2021-06-05 18:32:51 -04:00
|
|
|
'bloop': '',
|
2021-05-29 17:18:37 -04:00
|
|
|
'age': 0,
|
|
|
|
'health': 0,
|
|
|
|
'hunger': 0,
|
|
|
|
'happiness': 0,
|
2021-06-12 23:10:59 -04:00
|
|
|
'care_counter': 0,
|
2021-06-05 18:32:51 -04:00
|
|
|
'missed_care': 0,
|
|
|
|
'adult': 0,
|
2021-06-16 03:19:48 -04:00
|
|
|
'evolution_stage': '',
|
2021-05-29 17:18:37 -04:00
|
|
|
}
|
2021-06-16 03:40:02 -04:00
|
|
|
|
|
|
|
# Frame counter
|
2021-06-12 23:10:59 -04:00
|
|
|
self.frames_passed = 0
|
2021-05-29 17:18:37 -04:00
|
|
|
|
|
|
|
def write_save(self):
|
|
|
|
"""
|
|
|
|
Writes attributes of class to "save.json" file.
|
|
|
|
"""
|
2021-05-29 22:30:12 -04:00
|
|
|
with open(save_dir + '/save.json', 'w') as save_file:
|
2021-05-29 17:18:37 -04:00
|
|
|
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:
|
2021-05-29 22:30:12 -04:00
|
|
|
with open(save_dir + '/save.json', 'r') as save_file:
|
2021-05-29 17:18:37 -04:00
|
|
|
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()
|
|
|
|
|
2021-06-12 23:10:59 -04:00
|
|
|
def update(self):
|
|
|
|
"""
|
|
|
|
Run the game logic.
|
|
|
|
"""
|
|
|
|
self.frames_passed += 1
|
2021-06-16 03:40:02 -04:00
|
|
|
# Run logic of the game every second.
|
2021-06-12 23:10:59 -04:00
|
|
|
if self.frames_passed >= game_fps:
|
2021-06-16 03:40:02 -04:00
|
|
|
|
|
|
|
# Add one to the age of the bloop.
|
2021-06-12 23:10:59 -04:00
|
|
|
self.attributes['age'] += 1
|
|
|
|
|
2021-06-16 03:40:02 -04:00
|
|
|
# Save the data when the age of the bloop is a multiple of 10.
|
2021-06-12 23:10:59 -04:00
|
|
|
if self.attributes['age'] % 10 == 0:
|
|
|
|
self.write_save()
|
2021-06-16 03:40:02 -04:00
|
|
|
|
|
|
|
# Reset frame counter
|
2021-06-12 23:10:59 -04:00
|
|
|
self.frames_passed = 0
|
|
|
|
|
2021-05-29 17:18:37 -04:00
|
|
|
|
|
|
|
class PlaygroundFriend(pygame.sprite.Sprite):
|
|
|
|
"""
|
|
|
|
Class for the sprite of the creature on the main playground.
|
|
|
|
"""
|
|
|
|
|
2021-06-12 23:10:59 -04:00
|
|
|
def __init__(self, data_handler):
|
2021-05-29 17:18:37 -04:00
|
|
|
pygame.sprite.Sprite.__init__(self)
|
|
|
|
|
2021-06-05 19:12:02 -04:00
|
|
|
# All attributes of the bloops
|
2021-06-12 23:10:59 -04:00
|
|
|
self.bloop = data_handler.attributes['bloop']
|
|
|
|
self.adult = data_handler.attributes['adult']
|
|
|
|
self.evolution_stage = data_handler.attributes['evolution_stage']
|
2021-06-05 19:12:02 -04:00
|
|
|
self.direction = 0
|
|
|
|
|
2021-06-16 03:19:48 -04:00
|
|
|
if self.evolution_stage == 'adult':
|
|
|
|
image = self.evolution_stage + self.adult
|
2021-06-05 19:12:02 -04:00
|
|
|
else:
|
2021-06-16 03:19:48 -04:00
|
|
|
image = self.evolution_stage
|
2021-06-05 19:12:02 -04:00
|
|
|
|
2021-06-16 03:19:48 -04:00
|
|
|
# Draw the correct bloop depending on the stage
|
|
|
|
sprite_sheet = SpriteSheet(script_dir + '/resources/images/bloops/{0}/{1}.png'.format(self.bloop, image),
|
|
|
|
script_dir + '/resources/images/bloops/{0}/{1}.json'.format(self.bloop, image))
|
|
|
|
|
|
|
|
# Load the images from the sprite sheet
|
2021-06-12 22:17:53 -04:00
|
|
|
self.images = sprite_sheet.images
|
2021-06-05 19:12:02 -04:00
|
|
|
|
2021-06-16 03:19:48 -04:00
|
|
|
# Put the egg in the middle of the screen.
|
2021-06-05 19:12:02 -04:00
|
|
|
self.rect = self.images[0].get_rect()
|
|
|
|
self.rect.x = (game_res / 2) - (self.rect.width / 2)
|
|
|
|
self.rect.y = (game_res / 2) - (self.rect.height / 2)
|
2021-06-16 03:19:48 -04:00
|
|
|
|
|
|
|
# Start animation at the beginning of the sprite sheet.
|
2021-06-05 19:12:02 -04:00
|
|
|
self.index = 0
|
|
|
|
self.image = self.images[self.index]
|
|
|
|
|
2021-06-16 03:40:02 -04:00
|
|
|
self.movement_frames = game_fps / 2 # How many frames pass before the bloop moves
|
2021-06-05 19:12:02 -04:00
|
|
|
self.current_frame = 0
|
|
|
|
|
2021-06-16 03:43:51 -04:00
|
|
|
def pet(self):
|
|
|
|
"""
|
|
|
|
Pet the bloop!
|
|
|
|
"""
|
|
|
|
pass
|
|
|
|
|
2021-06-05 22:10:47 -04:00
|
|
|
def update(self):
|
2021-06-05 19:12:02 -04:00
|
|
|
"""
|
|
|
|
Takes the images loaded and animates it, spacing it out equally for the framerate.
|
|
|
|
"""
|
|
|
|
|
2021-06-16 03:40:02 -04:00
|
|
|
margins = 9 # Margins for how far the bloop can move from the left and the right of the screen
|
|
|
|
movement_amount = 2 # Pixels that the bloop moves in one movement
|
2021-06-05 19:12:02 -04:00
|
|
|
|
|
|
|
self.current_frame += 1
|
2021-06-16 03:40:02 -04:00
|
|
|
|
|
|
|
# Check to see if the number of movement frames has passed
|
|
|
|
if self.current_frame >= self.movement_frames:
|
2021-06-05 19:12:02 -04:00
|
|
|
self.current_frame = 0
|
|
|
|
|
2021-06-16 03:40:02 -04:00
|
|
|
# Move only if the bloop is not in the egg stage
|
|
|
|
if self.evolution_stage != 'egg':
|
|
|
|
|
|
|
|
# Change direction if the bloop has reached either edge of the screen
|
2021-06-05 19:12:02 -04:00
|
|
|
if self.rect.x < margins:
|
|
|
|
self.direction = 1
|
|
|
|
elif self.rect.x > game_res - margins - self.rect.width:
|
|
|
|
self.direction = 0
|
|
|
|
|
2021-06-16 03:40:02 -04:00
|
|
|
# Move according to the direction.
|
2021-06-05 19:12:02 -04:00
|
|
|
if self.direction == 0:
|
|
|
|
self.rect.x -= movement_amount
|
|
|
|
else:
|
|
|
|
self.rect.x += movement_amount
|
|
|
|
|
2021-06-16 03:40:02 -04:00
|
|
|
# Animate the bloop
|
|
|
|
self.index = (self.index + 1) % len(self.images)
|
|
|
|
self.image = self.images[self.index]
|
|
|
|
|
2021-05-29 17:18:37 -04:00
|
|
|
|
|
|
|
class SelectionEgg(pygame.sprite.Sprite):
|
|
|
|
"""
|
|
|
|
Class for the eggs on the egg selection screen.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, egg_color):
|
|
|
|
pygame.sprite.Sprite.__init__(self)
|
|
|
|
|
2021-06-04 00:33:02 -04:00
|
|
|
self.egg_color = egg_color
|
|
|
|
|
2021-06-04 00:15:01 -04:00
|
|
|
# Loads the JSON file of the egg to read in data.
|
2021-06-05 18:05:27 -04:00
|
|
|
with open(script_dir + '/resources/data/bloop_info/{0}.json'.format(egg_color), 'r') as save_file:
|
2021-06-04 00:15:01 -04:00
|
|
|
json_file = json.load(save_file)
|
|
|
|
save_file.close()
|
2021-05-29 17:18:37 -04:00
|
|
|
|
2021-06-04 00:15:01 -04:00
|
|
|
# Gets the description off the egg from the JSON file.
|
|
|
|
self.description = json_file.get('description')
|
2021-06-13 14:51:18 -04:00
|
|
|
self.contentness = json_file.get('contentness')
|
|
|
|
self.metabolism = json_file.get('metabolism')
|
2021-06-04 00:15:01 -04:00
|
|
|
|
2021-05-29 17:18:37 -04:00
|
|
|
# Load the egg from the given color and get the bounding rectangle for the image.
|
2021-06-12 22:17:53 -04:00
|
|
|
sprite_sheet = SpriteSheet(script_dir + '/resources/images/bloops/{0}/egg.png'.format(self.egg_color),
|
|
|
|
script_dir + '/resources/images/bloops/{0}/egg.json'.format(self.egg_color))
|
|
|
|
self.images = sprite_sheet.images
|
2021-05-29 17:18:37 -04:00
|
|
|
|
2021-06-05 19:12:02 -04:00
|
|
|
# Get the rectangle from the first image in the list
|
2021-05-29 17:18:37 -04:00
|
|
|
self.rect = self.images[0].get_rect()
|
|
|
|
self.index = 0
|
|
|
|
self.image = self.images[self.index]
|
|
|
|
|
|
|
|
def update(self):
|
2021-06-05 18:27:53 -04:00
|
|
|
"""
|
|
|
|
Updates the sprite object.
|
|
|
|
"""
|
2021-06-16 03:40:02 -04:00
|
|
|
# Animate the sprite
|
|
|
|
self.index = (self.index + 1) % len(self.images)
|
|
|
|
self.image = self.images[self.index]
|
2021-05-29 17:18:37 -04:00
|
|
|
|
|
|
|
|
2021-06-13 14:51:18 -04:00
|
|
|
class EggInfo:
|
|
|
|
"""
|
|
|
|
Class to draw the contentness and metabolism value off the egg on the info screen.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, contentness, metabolism, location):
|
|
|
|
self.contentness = contentness
|
|
|
|
self.metabolism = metabolism
|
|
|
|
self.x = location[0]
|
|
|
|
self.y = location[1]
|
|
|
|
|
|
|
|
# Create a new surface to blit onto the other surface
|
|
|
|
self.surface = pygame.Surface((44, 15), SRCALPHA)
|
|
|
|
|
|
|
|
# Blit the two indicator icons on screen
|
|
|
|
smiley = pygame.image.load(script_dir + '/resources/images/gui/smiley.png').convert_alpha()
|
|
|
|
self.surface.blit(smiley, (0, 0))
|
|
|
|
apple = pygame.image.load(script_dir + '/resources/images/gui/apple.png').convert_alpha()
|
|
|
|
self.surface.blit(apple, (1, 9))
|
|
|
|
|
|
|
|
# Draw 5 stars. If the value of the contentness is less than the current star, make it a blank star.
|
|
|
|
for i in range(5):
|
|
|
|
if i < self.contentness:
|
|
|
|
star = pygame.image.load(script_dir + '/resources/images/gui/star.png').convert_alpha()
|
|
|
|
else:
|
|
|
|
star = pygame.image.load(script_dir + '/resources/images/gui/blank_star.png').convert_alpha()
|
|
|
|
self.surface.blit(star, (11 + (i * 6), 1))
|
|
|
|
|
|
|
|
# Draw 5 stars. If the value of the metabolism is less than the current star, make it a blank star.
|
|
|
|
for i in range(5):
|
|
|
|
if i < self.metabolism:
|
|
|
|
star = pygame.image.load(script_dir + '/resources/images/gui/star.png').convert_alpha()
|
|
|
|
else:
|
|
|
|
star = pygame.image.load(script_dir + '/resources/images/gui/blank_star.png').convert_alpha()
|
|
|
|
self.surface.blit(star, (11 + (i * 6), 10))
|
|
|
|
|
|
|
|
def draw(self, surface):
|
|
|
|
"""
|
|
|
|
Draw the info icons on a given surface.
|
|
|
|
:param surface: the surface to draw the icons on.
|
|
|
|
"""
|
2021-06-16 03:40:02 -04:00
|
|
|
# Blit the info onto the given surface.
|
2021-06-13 14:51:18 -04:00
|
|
|
surface.blit(self.surface, (self.x, self.y))
|
|
|
|
|
|
|
|
|
2021-06-04 23:21:54 -04:00
|
|
|
class InfoText:
|
|
|
|
"""
|
|
|
|
Class for drawing large amounts of text on the screen at a time
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, font, text='Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam commodo tempor '
|
|
|
|
'aliquet. Suspendisse placerat accumsan neque, nec volutpat nunc porta ut.'):
|
|
|
|
|
|
|
|
self.font = font
|
|
|
|
self.text = [] # Text broken up into a list according to how it will fit on screen.
|
|
|
|
self.max_lines = 6 # Max number of lines to be shown on screen at a time.
|
|
|
|
self.offset = 0
|
2021-06-04 23:59:10 -04:00
|
|
|
|
|
|
|
# Arrow icons to indicate scrolling
|
|
|
|
self.up_arrow = pygame.image.load(script_dir + '/resources/images/gui/up_arrow.png').convert_alpha()
|
|
|
|
self.down_arrow = pygame.image.load(script_dir + '/resources/images/gui/down_arrow.png').convert_alpha()
|
|
|
|
|
2021-06-04 23:21:54 -04:00
|
|
|
raw_text = text # Copy the text to a different variable to be cut up.
|
|
|
|
|
2021-06-05 18:27:53 -04:00
|
|
|
margins = 4.5
|
|
|
|
max_line_width = game_res - (margins * 2) # The maximum pixel width that drawn text can be.
|
2021-06-04 23:21:54 -04:00
|
|
|
cut_chars = '.,! ' # Characters that will be considered "cuts" aka when a line break can occur.
|
|
|
|
|
|
|
|
# Prevents freezing if the end of the string does not end in a cut character
|
|
|
|
# Will fix eventually more elegantly
|
|
|
|
if raw_text[-1:] not in cut_chars:
|
|
|
|
raw_text += ' '
|
|
|
|
|
|
|
|
# Calculating line breaks.
|
|
|
|
while len(raw_text) > 0:
|
|
|
|
index = 0
|
|
|
|
test_text = '' # Chunk of text to pseudo-render and test the width of.
|
|
|
|
|
|
|
|
# Loops until the testing text has reached the size limit.
|
|
|
|
while True:
|
|
|
|
|
|
|
|
# Break if the current index is larger than the remaining text.
|
|
|
|
if index + 1 > len(raw_text):
|
|
|
|
index -= 1
|
|
|
|
break
|
|
|
|
|
|
|
|
# Add one character to the testing text from the raw text.
|
|
|
|
test_text += raw_text[index]
|
|
|
|
# Get the width of the pseudo-rendered text.
|
|
|
|
text_width = font.size(test_text)[0]
|
|
|
|
|
|
|
|
# Break if the text is larger than the defined max width.
|
|
|
|
if text_width > max_line_width:
|
|
|
|
break
|
|
|
|
index += 1
|
|
|
|
pass
|
|
|
|
|
|
|
|
# Gets the chunk of text to be added to the list.
|
|
|
|
text_chunk = raw_text[0:index + 1]
|
|
|
|
# Determines if the chunk of text has any break characters.
|
|
|
|
has_breaks = any(cut_chars in text_chunk for cut_chars in cut_chars)
|
|
|
|
|
|
|
|
# If the text has break characters, start with the last character and go backwards until
|
|
|
|
# one has been found, decreasing the index each time.
|
|
|
|
if has_breaks:
|
|
|
|
while raw_text[index] not in cut_chars:
|
|
|
|
index -= 1
|
|
|
|
text_chunk = raw_text[0:index + 1]
|
|
|
|
# If there are no break characters in the chunk, simply decrease the index by one and insert
|
|
|
|
# a dash at the end of the line to indicate the word continues.
|
|
|
|
else:
|
|
|
|
index -= 1
|
|
|
|
text_chunk = raw_text[0:index + 1]
|
|
|
|
text_chunk += '-'
|
|
|
|
|
|
|
|
# Append the text chunk to the list of text to draw.
|
|
|
|
self.text.append(text_chunk)
|
|
|
|
|
|
|
|
# Cut the text to repeat the process with the new cut string.
|
|
|
|
raw_text = raw_text[index + 1:]
|
|
|
|
|
|
|
|
def draw(self, surface):
|
|
|
|
"""
|
|
|
|
Draws the text on a given surface.
|
|
|
|
:param surface: The surface for the text to be drawn on.
|
|
|
|
"""
|
2021-06-05 18:27:53 -04:00
|
|
|
# Constants to help draw the text
|
|
|
|
line_separation = 7
|
|
|
|
left_margin = 3
|
|
|
|
top_margin = 25
|
|
|
|
bottom_margin = 10
|
2021-06-04 23:21:54 -04:00
|
|
|
|
2021-06-05 18:27:53 -04:00
|
|
|
# Draw the lines on the screen
|
2021-06-04 23:21:54 -04:00
|
|
|
for i in range(min(len(self.text), self.max_lines)):
|
|
|
|
text = self.font.render(self.text[i + self.offset], False, (64, 64, 64))
|
2021-06-05 18:27:53 -04:00
|
|
|
surface.blit(text, (left_margin, top_margin + (i * line_separation)))
|
2021-06-04 23:59:10 -04:00
|
|
|
|
|
|
|
# Draw the arrows if there is more text than is on screen.
|
|
|
|
if self.offset != 0:
|
2021-06-05 18:27:53 -04:00
|
|
|
surface.blit(self.up_arrow, ((game_res / 2) - (self.up_arrow.get_rect().width / 2), top_margin - 3))
|
2021-06-04 23:59:10 -04:00
|
|
|
if len(self.text) - (self.offset + 1) >= self.max_lines:
|
2021-06-05 18:27:53 -04:00
|
|
|
surface.blit(self.down_arrow,
|
|
|
|
((game_res / 2) - (self.down_arrow.get_rect().width / 2), game_res - bottom_margin))
|
2021-06-04 23:21:54 -04:00
|
|
|
|
2021-06-04 23:26:56 -04:00
|
|
|
def scroll_down(self):
|
|
|
|
"""
|
|
|
|
Scrolls the text on the screen down.
|
|
|
|
"""
|
|
|
|
# Ensures that the offset cannot be too big as to try to render non-existent lines.
|
|
|
|
if len(self.text) - (self.offset + 1) >= self.max_lines:
|
|
|
|
self.offset += 1
|
|
|
|
|
|
|
|
def scroll_up(self):
|
|
|
|
"""
|
|
|
|
Scrolls the text on the screen up.
|
|
|
|
"""
|
|
|
|
if self.offset > 0: # Ensures a non-zero offset is not possible.
|
|
|
|
self.offset -= 1
|
|
|
|
|
2021-06-04 23:21:54 -04:00
|
|
|
|
2021-06-16 03:35:21 -04:00
|
|
|
class MenuIcon(pygame.sprite.Sprite):
|
|
|
|
"""
|
|
|
|
Sprite for an icon on the main popup menu.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, icon):
|
|
|
|
pygame.sprite.Sprite.__init__(self)
|
|
|
|
self.icon = icon
|
|
|
|
|
|
|
|
# Load the sprite sheet from the icon name
|
|
|
|
sprite_sheet = SpriteSheet(script_dir + '/resources/images/gui/popup_menu/{0}.png'.format(self.icon),
|
|
|
|
script_dir + '/resources/images/gui/popup_menu/{0}.json'.format(self.icon))
|
|
|
|
self.images = sprite_sheet.images
|
|
|
|
|
|
|
|
# Get the rectangle from the first image in the list
|
|
|
|
self.rect = self.images[0].get_rect()
|
|
|
|
self.image = self.images[0]
|
|
|
|
|
|
|
|
def select(self):
|
|
|
|
"""
|
|
|
|
Change the icon sprite to the selected icon.
|
|
|
|
"""
|
|
|
|
self.image = self.images[1]
|
|
|
|
|
|
|
|
def deselect(self):
|
|
|
|
"""
|
|
|
|
Change the icon sprite to the not selected icon.
|
|
|
|
"""
|
|
|
|
self.image = self.images[0]
|
|
|
|
|
|
|
|
|
|
|
|
class PopupMenu:
|
|
|
|
"""
|
|
|
|
Class to create a popup menu that can be hidden and shown at will
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, position):
|
|
|
|
# Background frame of the popup menu
|
|
|
|
self.frame = pygame.image.load(script_dir + '/resources/images/gui/popup_menu/frame.png').convert_alpha()
|
|
|
|
|
|
|
|
self.draw_menu = False # Whether or not to draw the popup menu
|
|
|
|
self.menu_sprites = pygame.sprite.Group() # Sprite group for the icons
|
|
|
|
self.selected = 0 # The currently selected icon
|
|
|
|
|
|
|
|
# The names of the icons to be drawn
|
|
|
|
icon_names = ['apple', 'dumbbell', 'stats', 'controller', 'bed']
|
|
|
|
|
|
|
|
self.icons = []
|
|
|
|
# Create an icon sprite for each name in the list and add it to the icon list
|
|
|
|
for i in icon_names:
|
|
|
|
self.icons.append(MenuIcon(i))
|
|
|
|
|
|
|
|
# Add each sprite in the icon list to the sprite group
|
|
|
|
for i in range(len(self.icons)):
|
|
|
|
icon = self.icons[i]
|
|
|
|
if i == self.selected: # Make the default selected icon glow
|
|
|
|
icon.select()
|
|
|
|
|
|
|
|
# Calculate the position of the icon on screen
|
2021-06-19 23:31:19 -04:00
|
|
|
icon.rect.x = 10 + (i * 15) - (icon.image.get_width() / 2)
|
2021-06-16 03:35:21 -04:00
|
|
|
icon.rect.y = position[1] + self.frame.get_height() / 2 - icon.image.get_height() / 2
|
|
|
|
|
|
|
|
# Add the icon to the sprite group.
|
|
|
|
self.menu_sprites.add(icon)
|
|
|
|
|
|
|
|
def toggle(self):
|
|
|
|
"""
|
|
|
|
Toggles the menu on or off.
|
|
|
|
"""
|
|
|
|
self.draw_menu = not self.draw_menu
|
|
|
|
|
|
|
|
def next(self):
|
|
|
|
"""
|
|
|
|
Changes the selection to the next icon (to the right.)
|
|
|
|
"""
|
|
|
|
if self.draw_menu: # Only change if the menu is on screen
|
|
|
|
|
|
|
|
self.icons[self.selected].deselect() # Deselect the current icon
|
|
|
|
self.selected += 1 # Change selection to the next icon
|
|
|
|
if self.selected >= len(self.icons): # Wrap around if new value is invalid
|
|
|
|
self.selected = 0
|
|
|
|
self.icons[self.selected].select() # Select the newly selected icon
|
|
|
|
|
|
|
|
def prev(self):
|
|
|
|
"""
|
|
|
|
Changes the selection to the previous icon (to the left.)
|
|
|
|
"""
|
|
|
|
if self.draw_menu: # Only change if the menu is on screen
|
|
|
|
|
|
|
|
self.icons[self.selected].deselect() # Deselect the current icon
|
|
|
|
self.selected -= 1 # Change selection to the previous icon
|
|
|
|
if self.selected < 0: # Wrap around if new value is invalid
|
|
|
|
self.selected = len(self.icons) - 1
|
|
|
|
self.icons[self.selected].select() # Select the newly selected icon
|
|
|
|
|
|
|
|
def draw(self, surface):
|
|
|
|
"""
|
|
|
|
Draw the menu onto a given surface
|
|
|
|
:param surface: the surface to draw the menu on.
|
|
|
|
"""
|
|
|
|
# Draw the menu only if it is toggled on.
|
|
|
|
if self.draw_menu:
|
|
|
|
surface.blit(self.frame, (3, 3))
|
|
|
|
self.menu_sprites.draw(surface)
|
|
|
|
|
|
|
|
|
2021-05-29 17:18:37 -04:00
|
|
|
# Makes Pygame draw on the display of the RPi.
|
|
|
|
os.environ["SDL_FBDEV"] = "/dev/fb1"
|
|
|
|
|
|
|
|
# Useful for debugging on the PC. Imports a fake RPi.GPIO library if one is not found (which it can't
|
|
|
|
# be on a PC, RPi.GPIO cannot be installed outside of a Raspberry Pi.
|
|
|
|
try:
|
|
|
|
importlib.util.find_spec('RPi.GPIO')
|
|
|
|
import RPi.GPIO as GPIO
|
2021-06-16 17:44:27 -04:00
|
|
|
|
2021-06-15 18:55:24 -04:00
|
|
|
on_hardware = True
|
2021-05-29 17:18:37 -04:00
|
|
|
except ImportError:
|
2021-05-29 18:46:24 -04:00
|
|
|
import pocket_friends.development.FakeGPIO as GPIO
|
2021-06-16 17:44:27 -04:00
|
|
|
|
2021-06-15 18:55:24 -04:00
|
|
|
on_hardware = False
|
2021-05-29 17:18:37 -04:00
|
|
|
|
|
|
|
|
|
|
|
def game():
|
|
|
|
"""
|
2021-06-16 17:44:27 -04:00
|
|
|
Starts the game.
|
2021-05-29 17:18:37 -04:00
|
|
|
"""
|
|
|
|
pygame.init()
|
|
|
|
|
|
|
|
# Hide the cursor for the Pi display.
|
|
|
|
pygame.mouse.set_visible(False)
|
|
|
|
|
2021-06-16 17:44:27 -04:00
|
|
|
# The game is normally rendered at 80 pixels and upscaled from there. If changing displays, change the
|
2021-05-29 17:18:37 -04:00
|
|
|
# screen_size to reflect what the resolution of the new display is.
|
2021-06-19 23:32:01 -04:00
|
|
|
screen_size = 320
|
2021-05-29 17:18:37 -04:00
|
|
|
|
|
|
|
window = pygame.display.set_mode((screen_size, screen_size))
|
2021-06-05 18:27:53 -04:00
|
|
|
surface = pygame.Surface((game_res, game_res))
|
2021-05-29 17:18:37 -04:00
|
|
|
|
|
|
|
# Only really useful for PCs. Does nothing on the Raspberry Pi.
|
2021-05-29 22:40:41 -04:00
|
|
|
pygame.display.set_caption('Pocket Friends {0}'.format(pocket_friends.__version__))
|
2021-05-29 17:18:37 -04:00
|
|
|
|
2021-05-30 00:16:57 -04:00
|
|
|
# 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)
|
|
|
|
|
2021-05-29 17:18:37 -04:00
|
|
|
clock = pygame.time.Clock()
|
|
|
|
|
2021-06-16 17:44:27 -04:00
|
|
|
# Font used for small text in the game. Bigger text is usually image files.
|
2021-05-29 22:17:22 -04:00
|
|
|
small_font = pygame.font.Font(script_dir + '/resources/fonts/5Pts5.ttf', 10)
|
2021-05-29 17:18:37 -04:00
|
|
|
|
2021-06-16 17:44:27 -04:00
|
|
|
# Default game state when the game first starts.
|
2021-05-29 17:18:37 -04:00
|
|
|
game_state = 'title'
|
|
|
|
running = True
|
2021-06-12 23:10:59 -04:00
|
|
|
data_handler = DataHandler()
|
2021-05-29 17:18:37 -04:00
|
|
|
|
|
|
|
# A group of all the sprites on screen. Used to update all sprites at onc
|
|
|
|
all_sprites = pygame.sprite.Group()
|
|
|
|
|
|
|
|
# Start the GPIO handler to take in buttons from the RPi HAT.
|
|
|
|
GPIOHandler.setup()
|
|
|
|
|
2021-06-16 17:44:27 -04:00
|
|
|
# Dev code used to exit the game. Default Down, Down, Up, Up, Down, Down, Up, Up, A, A, B
|
2021-05-29 17:18:37 -04:00
|
|
|
dev_code = deque()
|
|
|
|
for button in [Constants.buttons.get('j_d'), Constants.buttons.get('j_d'), Constants.buttons.get('j_u'),
|
|
|
|
Constants.buttons.get('j_u'), Constants.buttons.get('j_d'), Constants.buttons.get('j_d'),
|
|
|
|
Constants.buttons.get('j_u'), Constants.buttons.get('j_u'), Constants.buttons.get('a'),
|
|
|
|
Constants.buttons.get('a'), Constants.buttons.get('b')]:
|
|
|
|
dev_code.append(button)
|
|
|
|
|
|
|
|
# Log of the inputs.
|
|
|
|
input_log = deque()
|
|
|
|
|
|
|
|
# Time since last input. Used to help regulate double presses of buttons.
|
|
|
|
last_input_tick = 0
|
|
|
|
|
|
|
|
def draw():
|
|
|
|
"""
|
|
|
|
Draws the main pygame display.
|
|
|
|
"""
|
|
|
|
|
|
|
|
# Draws all the sprites on screen and scales the screen to the correct size from the rendered size.
|
|
|
|
all_sprites.update()
|
|
|
|
all_sprites.draw(surface)
|
|
|
|
frame = pygame.transform.scale(surface, (screen_size, screen_size))
|
|
|
|
window.blit(frame, frame.get_rect())
|
|
|
|
|
|
|
|
# Update the entire display.
|
|
|
|
pygame.display.flip()
|
|
|
|
|
|
|
|
def draw_bg():
|
|
|
|
"""
|
2021-06-16 17:44:27 -04:00
|
|
|
Draws the main game background image onto a given surface.
|
2021-05-29 17:18:37 -04:00
|
|
|
"""
|
2021-05-29 22:17:22 -04:00
|
|
|
bg_image = pygame.image.load(script_dir + '/resources/images/bg.png').convert()
|
2021-05-29 17:18:37 -04:00
|
|
|
surface.blit(bg_image, (0, 0))
|
|
|
|
|
|
|
|
def log_button(pressed_button):
|
|
|
|
"""
|
|
|
|
Logs the button presses to register the dev code.
|
|
|
|
:param pressed_button: The button code to be logged
|
|
|
|
"""
|
|
|
|
input_log.append(pressed_button)
|
|
|
|
if len(input_log) > len(dev_code):
|
|
|
|
input_log.popleft()
|
|
|
|
|
|
|
|
def create_event(pressed_button):
|
|
|
|
"""
|
|
|
|
Creates a pygame event with a given keyboard code
|
|
|
|
:param pressed_button:
|
|
|
|
"""
|
|
|
|
nonlocal last_input_tick
|
|
|
|
# Register a button click so long as the last button click happened no less than two frames ago
|
2021-06-15 18:55:24 -04:00
|
|
|
if pygame.time.get_ticks() - last_input_tick > clock.get_time() * 2 or not on_hardware:
|
2021-05-29 17:18:37 -04:00
|
|
|
pygame.event.post(pygame.event.Event(KEYDOWN, {'key': pressed_button}))
|
|
|
|
pygame.event.post(pygame.event.Event(KEYUP, {'key': pressed_button}))
|
|
|
|
log_button(pressed_button)
|
|
|
|
last_input_tick = pygame.time.get_ticks()
|
|
|
|
|
|
|
|
def check_dev_code():
|
|
|
|
"""
|
|
|
|
Checks if the dev code has been entered. If it has, stop the program.
|
|
|
|
"""
|
|
|
|
nonlocal running
|
|
|
|
|
|
|
|
if dev_code == input_log:
|
|
|
|
running = False
|
|
|
|
|
|
|
|
def handle_gpio():
|
|
|
|
"""
|
|
|
|
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):
|
|
|
|
create_event(code)
|
|
|
|
|
|
|
|
def keyboard_handler():
|
|
|
|
"""
|
2021-06-16 17:44:27 -04:00
|
|
|
Simulates key presses to GPIO button presses. Also handles quitting the game.
|
2021-05-29 17:18:37 -04:00
|
|
|
"""
|
|
|
|
nonlocal running
|
|
|
|
|
|
|
|
# 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:
|
|
|
|
create_event(Constants.buttons.get('a'))
|
|
|
|
if keyboard_event.key == pygame.K_b:
|
|
|
|
create_event(Constants.buttons.get('b'))
|
|
|
|
if keyboard_event.key == pygame.K_PERIOD:
|
|
|
|
create_event(Constants.buttons.get('j_i'))
|
|
|
|
if keyboard_event.key == pygame.K_RIGHT:
|
|
|
|
create_event(Constants.buttons.get('j_r'))
|
|
|
|
if keyboard_event.key == pygame.K_LEFT:
|
|
|
|
create_event(Constants.buttons.get('j_l'))
|
|
|
|
if keyboard_event.key == pygame.K_DOWN:
|
|
|
|
create_event(Constants.buttons.get('j_d'))
|
|
|
|
if keyboard_event.key == pygame.K_UP:
|
|
|
|
create_event(Constants.buttons.get('j_u'))
|
|
|
|
if keyboard_event.key == pygame.K_ESCAPE:
|
|
|
|
running = False
|
|
|
|
|
|
|
|
def pre_handler():
|
|
|
|
"""
|
2021-06-16 17:44:27 -04:00
|
|
|
Runs at the beginning of each loop, handles drawing the background, controlling game speed, and
|
2021-05-29 17:18:37 -04:00
|
|
|
controlling the GPIO button inputs and keyboard handler
|
|
|
|
"""
|
2021-06-16 17:44:27 -04:00
|
|
|
# Regulate the speed of the game.
|
2021-05-29 17:18:37 -04:00
|
|
|
clock.tick(game_fps)
|
|
|
|
|
|
|
|
# Handle all inputs for both debugging and real GPIO button presses.
|
|
|
|
keyboard_handler()
|
|
|
|
handle_gpio()
|
|
|
|
check_dev_code()
|
|
|
|
|
|
|
|
# Draw the background.
|
|
|
|
draw_bg()
|
|
|
|
|
|
|
|
while running:
|
|
|
|
if game_state == 'title':
|
|
|
|
all_sprites.empty()
|
|
|
|
pre_handler()
|
|
|
|
|
|
|
|
# Draw the title image in the middle of the screen.
|
2021-05-29 22:17:22 -04:00
|
|
|
title_image = pygame.image.load(script_dir + '/resources/images/title.png').convert_alpha()
|
2021-05-29 17:18:37 -04:00
|
|
|
surface.blit(title_image, (0, 0))
|
|
|
|
draw()
|
|
|
|
|
2021-06-16 17:44:27 -04:00
|
|
|
# Show the title for 1 second then move on to the initialization phase of the game.
|
2021-05-29 17:18:37 -04:00
|
|
|
pygame.time.wait(1000)
|
|
|
|
game_state = 'init'
|
|
|
|
|
|
|
|
elif game_state == 'playground':
|
2021-06-05 19:12:22 -04:00
|
|
|
|
2021-06-16 03:43:51 -04:00
|
|
|
# Submenu used within the playground.
|
|
|
|
submenu = 'main'
|
2021-06-05 19:12:22 -04:00
|
|
|
|
|
|
|
while running and game_state == 'playground':
|
2021-06-16 03:43:51 -04:00
|
|
|
|
|
|
|
all_sprites.empty()
|
|
|
|
|
|
|
|
if submenu == 'main':
|
|
|
|
|
|
|
|
# Create the bloop and the menu
|
|
|
|
bloop = PlaygroundFriend(data_handler)
|
|
|
|
all_sprites.add(bloop)
|
|
|
|
popup_menu = PopupMenu((3, 3))
|
|
|
|
|
|
|
|
while running and game_state == 'playground' and submenu == 'main':
|
|
|
|
pre_handler()
|
|
|
|
data_handler.update()
|
|
|
|
|
|
|
|
for event in pygame.event.get():
|
|
|
|
if event.type == pygame.KEYDOWN:
|
|
|
|
if event.key == Constants.buttons.get('j_r'):
|
|
|
|
# Move selection to the next item
|
|
|
|
popup_menu.next()
|
|
|
|
if event.key == Constants.buttons.get('j_l'):
|
|
|
|
# Move selection to the previous item
|
|
|
|
popup_menu.prev()
|
|
|
|
if event.key == Constants.buttons.get('a'):
|
|
|
|
# Change submenu to the menu the icon points to
|
|
|
|
if popup_menu.draw_menu:
|
|
|
|
submenu = popup_menu.icons[popup_menu.selected].icon
|
2021-06-16 17:44:27 -04:00
|
|
|
else: # Pet the bloop otherwise
|
2021-06-16 03:43:51 -04:00
|
|
|
bloop.pet()
|
|
|
|
if event.key == Constants.buttons.get('b'):
|
|
|
|
# Toggle the popup menu on or off
|
|
|
|
popup_menu.toggle()
|
|
|
|
|
|
|
|
# Draw the popup menu if toggled on
|
|
|
|
popup_menu.draw(surface)
|
|
|
|
|
|
|
|
draw()
|
|
|
|
|
|
|
|
else: # Go to the error state if an invalid state is set.
|
|
|
|
game_state = None
|
2021-05-29 17:18:37 -04:00
|
|
|
|
|
|
|
elif game_state == 'init':
|
|
|
|
all_sprites.empty()
|
|
|
|
pre_handler()
|
|
|
|
draw()
|
|
|
|
|
|
|
|
# Read the save file.
|
2021-06-12 23:10:59 -04:00
|
|
|
data_handler.read_save()
|
2021-05-29 17:18:37 -04:00
|
|
|
|
2021-06-16 17:44:27 -04:00
|
|
|
# Determines if it is a new game or not by looking at the evolution stage. If it is -1, the egg has
|
|
|
|
# not been created yet, and the game sends you to the egg selection screen. If not, the game sends
|
2021-05-29 17:18:37 -04:00
|
|
|
# you to the playground.
|
2021-06-12 23:10:59 -04:00
|
|
|
if data_handler.attributes['bloop'] == '':
|
2021-05-29 17:18:37 -04:00
|
|
|
game_state = 'egg_select'
|
|
|
|
else:
|
|
|
|
game_state = 'playground'
|
|
|
|
|
|
|
|
elif game_state == 'egg_select':
|
|
|
|
|
|
|
|
# Submenu used within the egg selection menu.
|
|
|
|
submenu = 'main'
|
|
|
|
|
|
|
|
selected = 0
|
2021-06-04 00:33:02 -04:00
|
|
|
selected_color = ""
|
2021-05-29 17:18:37 -04:00
|
|
|
|
|
|
|
while running and game_state == 'egg_select':
|
2021-06-04 00:33:02 -04:00
|
|
|
|
2021-05-29 17:18:37 -04:00
|
|
|
all_sprites.empty()
|
2021-06-04 00:33:02 -04:00
|
|
|
|
2021-05-29 17:18:37 -04:00
|
|
|
if submenu == 'main':
|
|
|
|
|
|
|
|
# Creates and holds the egg objects in a list.
|
2021-06-05 19:12:22 -04:00
|
|
|
eggs = [SelectionEgg('dev_egg'), SelectionEgg('blue'), SelectionEgg('rainbow')]
|
2021-05-29 17:18:37 -04:00
|
|
|
|
|
|
|
# How many eggs per row should be displayed.
|
|
|
|
eggs_per_row = 3
|
|
|
|
distance_between_eggs = 36 / eggs_per_row
|
|
|
|
|
|
|
|
# Count the total rows.
|
|
|
|
total_rows = -(-len(eggs) // eggs_per_row)
|
|
|
|
distance_between_rows = 32 / eggs_per_row
|
|
|
|
|
|
|
|
# Determine the location of each egg.
|
|
|
|
for egg in eggs:
|
|
|
|
current_row = eggs.index(egg) // eggs_per_row
|
|
|
|
rows_after = total_rows - (current_row + 1)
|
|
|
|
egg_in_row = eggs.index(egg) % eggs_per_row
|
|
|
|
eggs_after = min(len(eggs) - (current_row * eggs_per_row), eggs_per_row) - (egg_in_row + 1)
|
|
|
|
|
|
|
|
x_offset = 32
|
|
|
|
y_offset = 30
|
|
|
|
|
|
|
|
# The x coordinate of an egg is determined by which egg in the row it is, and how many eggs
|
|
|
|
# are in that row. If there is only 1 egg in a row, it is in the middle of the screen. If
|
|
|
|
# there are two, they're on equal halves and so on.
|
|
|
|
x = x_offset - (eggs_after * distance_between_eggs) + (egg_in_row * distance_between_eggs)
|
|
|
|
y = y_offset - (rows_after * distance_between_rows) + (current_row * distance_between_rows)
|
|
|
|
|
|
|
|
egg.rect.x = x
|
|
|
|
egg.rect.y = y
|
|
|
|
|
|
|
|
# Add the egg to the sprite list.
|
|
|
|
all_sprites.add(egg)
|
|
|
|
|
|
|
|
def get_cursor_coords(selection):
|
|
|
|
"""
|
|
|
|
Gets the coordinates of an egg on the selection screen by index and returns it as a tuple
|
|
|
|
:param selection: index of the egg to be selected
|
|
|
|
:return: tuple of the coordinates of the selected egg
|
|
|
|
"""
|
|
|
|
cursor_x_offset = -2
|
|
|
|
cursor_y_offset = -2
|
|
|
|
|
|
|
|
return eggs[selection].rect.x + cursor_x_offset, eggs[selection].rect.y + cursor_y_offset
|
|
|
|
|
|
|
|
def sel_left():
|
|
|
|
"""
|
|
|
|
Select the egg to the left with constraints.
|
|
|
|
"""
|
|
|
|
nonlocal selected
|
|
|
|
|
|
|
|
if selected % eggs_per_row != 0:
|
|
|
|
selected -= 1
|
|
|
|
|
|
|
|
def sel_right():
|
|
|
|
"""
|
|
|
|
Select the egg to the right with constraints.
|
|
|
|
"""
|
|
|
|
nonlocal selected
|
|
|
|
|
|
|
|
row = selected // eggs_per_row
|
|
|
|
eggs_in_row = min(len(eggs) - (row * eggs_per_row), eggs_per_row)
|
|
|
|
|
|
|
|
if selected % eggs_per_row != eggs_in_row - 1:
|
|
|
|
selected += 1
|
|
|
|
|
|
|
|
def sel_up():
|
|
|
|
"""
|
|
|
|
Select the egg above with constraints.
|
|
|
|
"""
|
|
|
|
nonlocal selected
|
|
|
|
|
|
|
|
if selected // eggs_per_row != 0:
|
|
|
|
selected -= eggs_per_row
|
|
|
|
|
|
|
|
def sel_down():
|
|
|
|
"""
|
|
|
|
Select the egg below with constraints.
|
|
|
|
"""
|
|
|
|
nonlocal selected
|
|
|
|
|
|
|
|
if selected // eggs_per_row != total_rows - 1:
|
|
|
|
selected += eggs_per_row
|
|
|
|
|
|
|
|
while running and game_state == 'egg_select' and submenu == 'main':
|
|
|
|
|
|
|
|
pre_handler()
|
|
|
|
|
|
|
|
for event in pygame.event.get():
|
|
|
|
if event.type == pygame.KEYDOWN:
|
|
|
|
if event.key == Constants.buttons.get('j_r'):
|
|
|
|
sel_right()
|
|
|
|
if event.key == Constants.buttons.get('j_l'):
|
|
|
|
sel_left()
|
|
|
|
if event.key == Constants.buttons.get('j_d'):
|
|
|
|
sel_down()
|
|
|
|
if event.key == Constants.buttons.get('j_u'):
|
|
|
|
sel_up()
|
|
|
|
if event.key == Constants.buttons.get('a'):
|
|
|
|
# Advance to the egg info screen for the selected egg.
|
2021-06-05 18:32:51 -04:00
|
|
|
submenu = 'bloop_info'
|
2021-05-29 17:18:37 -04:00
|
|
|
|
|
|
|
# Draws the cursor on screen.
|
2021-06-16 03:38:47 -04:00
|
|
|
cursor = pygame.image.load(
|
|
|
|
script_dir + '/resources/images/gui/egg_selector.png').convert_alpha()
|
2021-05-29 17:18:37 -04:00
|
|
|
surface.blit(cursor, get_cursor_coords(selected))
|
|
|
|
|
2021-06-04 00:33:02 -04:00
|
|
|
selected_color = eggs[selected].egg_color
|
|
|
|
|
2021-05-29 17:18:37 -04:00
|
|
|
draw()
|
|
|
|
|
2021-06-05 18:32:51 -04:00
|
|
|
elif submenu == 'bloop_info':
|
2021-06-04 00:33:02 -04:00
|
|
|
|
|
|
|
# Draw the selected egg on screen
|
|
|
|
egg = SelectionEgg(selected_color)
|
2021-06-13 14:51:18 -04:00
|
|
|
egg.rect.x = 8
|
2021-06-04 00:33:02 -04:00
|
|
|
egg.rect.y = 3
|
|
|
|
all_sprites.add(egg)
|
|
|
|
|
2021-06-05 00:00:52 -04:00
|
|
|
# Info screen for the eggs.
|
2021-06-13 14:51:18 -04:00
|
|
|
info_text = InfoText(small_font, egg.description)
|
|
|
|
info_icons = EggInfo(egg.contentness, egg.metabolism, (32, 4))
|
2021-06-05 00:00:52 -04:00
|
|
|
|
2021-06-05 18:32:51 -04:00
|
|
|
while running and game_state == 'egg_select' and submenu == 'bloop_info':
|
2021-06-04 00:33:02 -04:00
|
|
|
|
2021-05-29 17:18:37 -04:00
|
|
|
pre_handler()
|
2021-06-04 00:33:02 -04:00
|
|
|
|
2021-05-29 17:18:37 -04:00
|
|
|
for event in pygame.event.get():
|
|
|
|
if event.type == pygame.KEYDOWN:
|
2021-06-05 00:00:52 -04:00
|
|
|
if event.key == Constants.buttons.get('j_d'):
|
|
|
|
# Scroll down on the info screen.
|
2021-06-13 14:51:18 -04:00
|
|
|
info_text.scroll_down()
|
2021-06-05 00:00:52 -04:00
|
|
|
if event.key == Constants.buttons.get('j_u'):
|
|
|
|
# Scroll up on the info screen.
|
2021-06-13 14:51:18 -04:00
|
|
|
info_text.scroll_up()
|
2021-05-29 17:18:37 -04:00
|
|
|
if event.key == Constants.buttons.get('a'):
|
2021-06-05 19:12:22 -04:00
|
|
|
# Write save file with new attributes
|
2021-06-12 23:10:59 -04:00
|
|
|
data_handler.attributes['bloop'] = egg.egg_color
|
|
|
|
data_handler.attributes['health'] = 10
|
|
|
|
data_handler.attributes['hunger'] = 10
|
|
|
|
data_handler.attributes['happiness'] = 10
|
2021-06-16 03:19:48 -04:00
|
|
|
data_handler.attributes['evolution_stage'] = 'egg'
|
2021-06-12 23:10:59 -04:00
|
|
|
data_handler.write_save()
|
2021-06-05 19:12:22 -04:00
|
|
|
|
|
|
|
# Go to playground
|
|
|
|
game_state = 'playground'
|
2021-05-29 17:18:37 -04:00
|
|
|
if event.key == Constants.buttons.get('b'):
|
|
|
|
# Go back to the egg selection screen.
|
|
|
|
submenu = 'main'
|
|
|
|
|
2021-06-05 00:00:52 -04:00
|
|
|
# Draw the info screen.
|
2021-06-13 14:51:18 -04:00
|
|
|
info_text.draw(surface)
|
|
|
|
info_icons.draw(surface)
|
2021-05-29 17:18:37 -04:00
|
|
|
|
|
|
|
draw()
|
|
|
|
|
|
|
|
else: # Go to the error state if an invalid state is set.
|
|
|
|
game_state = None
|
|
|
|
|
|
|
|
else:
|
2021-06-16 17:44:27 -04:00
|
|
|
# Error screen. This appears when an invalid game state has been selected.
|
2021-05-29 17:18:37 -04:00
|
|
|
|
|
|
|
all_sprites.empty()
|
2021-06-16 17:44:27 -04:00
|
|
|
frames_passed = 0 # Counter for frames, helps ensure the game isn't frozen.
|
2021-05-29 17:18:37 -04:00
|
|
|
|
|
|
|
while running and game_state != 'title':
|
|
|
|
|
|
|
|
pre_handler()
|
|
|
|
|
|
|
|
# Draw the error screen
|
2021-05-29 22:17:22 -04:00
|
|
|
error_screen = pygame.image.load(script_dir + '/resources/images/debug/invalid.png').convert_alpha()
|
2021-05-29 17:18:37 -04:00
|
|
|
surface.blit(error_screen, (0, -8))
|
|
|
|
|
|
|
|
# Counts the frames passed. Resets every second.
|
|
|
|
frames_passed += 1
|
|
|
|
if frames_passed >= game_fps:
|
|
|
|
frames_passed = 0
|
|
|
|
|
|
|
|
# Draws the frame counter.
|
|
|
|
frame_counter = small_font.render('frames: {0}'.format(frames_passed), False, (64, 64, 64))
|
2021-06-05 18:27:53 -04:00
|
|
|
surface.blit(frame_counter, (1, game_res - 10))
|
2021-05-29 17:18:37 -04:00
|
|
|
|
|
|
|
for event in pygame.event.get():
|
|
|
|
if event.type == pygame.KEYDOWN:
|
|
|
|
if event.key == Constants.buttons.get('b'):
|
|
|
|
# Reset back to the title screen.
|
|
|
|
game_state = 'title'
|
|
|
|
|
|
|
|
draw()
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
"""
|
2021-06-16 17:44:27 -04:00
|
|
|
Calls the game() function to start the game.
|
2021-05-29 17:18:37 -04:00
|
|
|
"""
|
|
|
|
game()
|
|
|
|
|
|
|
|
GPIOHandler.teardown()
|
|
|
|
pygame.quit()
|