pocket-friends/pocket_friends/game.py

75 lines
2.3 KiB
Python

import pygame
import os
import pocket_friends
import importlib
valid_surfaces = [
'title',
'egg_select',
'selection_info',
'error_screen'
]
# Add all the surface modules to a dictionary for easy switching
surface_modules = {'dev_menu': importlib.import_module('pocket_friends.development.{0}'.format('dev_menu'))}
for module in valid_surfaces:
surface_modules[module] = importlib.import_module('pocket_friends.surfaces.{0}'.format(module))
starting_surface = 'title'
# FPS for the game to run at.
game_fps = 16
# The internal resolution of the game
game_res = 80
# Get the path for where all game resources are (images, fonts, sounds, etc.)
script_dir = os.path.dirname(os.path.abspath(__file__))
resources_dir = script_dir + '/resources'
# Makes Pygame draw on the display of the RPi.
os.environ['SDL_FBDEV'] = '/dev/fb1'
def start_game(resolution=240):
"""
Starts the game.
Args:
resolution (int, optional): Resolution to display the game at. Defaults to 240.
"""
pygame.init()
# Hide the cursor for the Pi display.
pygame.mouse.set_visible(False)
window = pygame.display.set_mode((resolution, resolution))
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)
running = True
while running:
surface.update()
# The game is only 80x80px, however it is upscaled to whatever the running resolution is.
frame = pygame.transform.scale(surface, (resolution, resolution))
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 = 'dev_menu'
surface = surface_modules.get(next_surface).Surface((game_res, game_res), resources_dir,
game_fps, **additional_args)
pygame.display.flip()
pygame.quit()