84 lines
2.4 KiB
Python
84 lines
2.4 KiB
Python
import pygame
|
|
import os
|
|
from pathlib import Path
|
|
import pocket_friends
|
|
|
|
import importlib
|
|
|
|
from pocket_friends.game_files.io import gpio_handler
|
|
|
|
valid_surfaces = [
|
|
'title',
|
|
'egg_select',
|
|
'selection_info',
|
|
'error_screen'
|
|
]
|
|
surface_modules = {}
|
|
|
|
for module in valid_surfaces:
|
|
surface_modules[module] = importlib.import_module('pocket_friends.game_files.surfaces.{0}'.format(module))
|
|
|
|
# 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')
|
|
resources_dir = script_dir + '/resources'
|
|
starting_surface = 'title'
|
|
|
|
# Makes Pygame draw on the display of the RPi.
|
|
os.environ["SDL_FBDEV"] = "/dev/fb1"
|
|
|
|
|
|
def game(windowed=False):
|
|
"""
|
|
Starts the game.
|
|
"""
|
|
|
|
pygame.init()
|
|
|
|
# Hide the cursor for the Pi display.
|
|
pygame.mouse.set_visible(False)
|
|
|
|
# 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 = 240
|
|
|
|
window = pygame.display.set_mode((screen_size, screen_size))
|
|
surface = surface_modules.get(starting_surface).Surface((game_res, game_res), resources_dir, game_fps)
|
|
|
|
# Only really useful for PCs. Does nothing on the Raspberry Pi.
|
|
pygame.display.set_caption('Pocket Friends {0}'.format(pocket_friends.__version__))
|
|
|
|
# Add an icon to the pygame window.
|
|
icon = pygame.image.load(resources_dir + '/icon/icon.png').convert_alpha()
|
|
pygame.display.set_icon(icon)
|
|
|
|
clock = pygame.time.Clock()
|
|
|
|
running = True
|
|
|
|
while running:
|
|
surface.update()
|
|
frame = pygame.transform.scale(surface, (screen_size, screen_size))
|
|
window.blit(frame, frame.get_rect())
|
|
|
|
if not surface.running:
|
|
next_surface = surface.next_surface
|
|
additional_args = surface.additional_args
|
|
if next_surface not in valid_surfaces:
|
|
next_surface = 'error_screen'
|
|
surface = surface_modules.get(next_surface).Surface((game_res, game_res), resources_dir,
|
|
game_fps, **additional_args)
|
|
pygame.display.flip()
|
|
|
|
|
|
def main(windowed=False):
|
|
"""
|
|
Calls the game() function to start the game.
|
|
"""
|
|
game(windowed)
|
|
|
|
pygame.quit()
|