removed windowed flag, added resolution flag

This commit is contained in:
Nicholas Dyer 2023-05-13 10:30:27 -04:00
parent 38a5812839
commit 35ac1cff51
2 changed files with 28 additions and 26 deletions

View File

@ -9,7 +9,7 @@ import pocket_friends.game_files.game as game
if __name__ == '__main__': if __name__ == '__main__':
enable_dev = False enable_dev = False
windowed = False resolution = 240
# enable dev mode if --dev argument is passed # enable dev mode if --dev argument is passed
if len(sys.argv) > 0: if len(sys.argv) > 0:
@ -17,10 +17,10 @@ if __name__ == '__main__':
if arg == '--delete-save': if arg == '--delete-save':
save_dir = os.path.join(Path.home(), '.pocket_friends') save_dir = os.path.join(Path.home(), '.pocket_friends')
os.remove(save_dir + '/save.json') os.remove(save_dir + '/save.json')
if arg == '--windowed': if '--res=' in arg:
windowed = True resolution = int(arg.split('=')[1])
game.main(windowed) game.main(resolution)
pygame.quit() pygame.quit()
sys.exit() sys.exit()

View File

@ -2,10 +2,8 @@ import pygame
import os import os
from pathlib import Path from pathlib import Path
import pocket_friends import pocket_friends
import importlib import importlib
from pocket_friends.game_files.io import gpio_handler
valid_surfaces = [ valid_surfaces = [
'title', 'title',
@ -13,27 +11,32 @@ valid_surfaces = [
'selection_info', 'selection_info',
'error_screen' 'error_screen'
] ]
surface_modules = {}
# Add all the surface modules to a dictionary for easy switching
surface_modules = {}
for module in valid_surfaces: for module in valid_surfaces:
surface_modules[module] = importlib.import_module('pocket_friends.game_files.surfaces.{0}'.format(module)) 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' 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. # Makes Pygame draw on the display of the RPi.
os.environ["SDL_FBDEV"] = "/dev/fb1" os.environ["SDL_FBDEV"] = "/dev/fb1"
def game(windowed=False): def game(resolution=240):
""" """
Starts the game. Starts the game.
Args:
resolution (int, optional): Resolution to display the game at. Defaults to 240.
""" """
pygame.init() pygame.init()
@ -41,11 +44,7 @@ def game(windowed=False):
# Hide the cursor for the Pi display. # Hide the cursor for the Pi display.
pygame.mouse.set_visible(False) pygame.mouse.set_visible(False)
# The game is normally rendered at 80 pixels and upscaled from there. If changing displays, change the window = pygame.display.set_mode((resolution, resolution))
# 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) 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. # Only really useful for PCs. Does nothing on the Raspberry Pi.
@ -55,13 +54,13 @@ def game(windowed=False):
icon = pygame.image.load(resources_dir + '/icon/icon.png').convert_alpha() icon = pygame.image.load(resources_dir + '/icon/icon.png').convert_alpha()
pygame.display.set_icon(icon) pygame.display.set_icon(icon)
clock = pygame.time.Clock()
running = True running = True
while running: while running:
surface.update() surface.update()
frame = pygame.transform.scale(surface, (screen_size, screen_size))
# 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()) window.blit(frame, frame.get_rect())
if not surface.running: if not surface.running:
@ -74,10 +73,13 @@ def game(windowed=False):
pygame.display.flip() pygame.display.flip()
def main(windowed=False): def main(resolution=240):
""" """
Calls the game() function to start the game. Calls the game() function to start the game.
Args:
resolution (int, optional): Resolution to display the game at. Defaults to 240.
""" """
game(windowed) game(resolution)
pygame.quit() pygame.quit()