pocket-friends/pocket_friends/game_files/game.py

82 lines
2.4 KiB
Python
Raw Normal View History

2023-05-11 11:51:51 -04:00
import pygame
import os
from pathlib import Path
import pocket_friends
import importlib
valid_surfaces = [
'title',
'egg_select'
]
surface_modules = {}
for module in valid_surfaces:
surface_modules[module] = importlib.import_module('pocket_friends.game_files.surfaces.{0}'.format(module))
print('imported ' + module)
2023-05-11 11:51:51 -04:00
# 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')
2023-05-11 12:20:07 -04:00
resources_dir = script_dir + '/surfaces/resources'
starting_surface = 'title'
2023-05-11 11:51:51 -04:00
# 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 = surface_modules.get(starting_surface).Surface((game_res, game_res), resources_dir, game_fps)
2023-05-11 11:51:51 -04:00
# Add an icon to the pygame window.
2023-05-11 12:20:07 -04:00
icon = pygame.image.load(script_dir + '/icon/icon.png').convert_alpha()
2023-05-11 11:51:51 -04:00
pygame.display.set_icon(icon)
clock = pygame.time.Clock()
# Default game state when the game first starts.
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
2023-05-11 12:20:07 -04:00
while running:
clock.tick(game_fps)
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
if next_surface not in valid_surfaces:
raise Exception('Given surface is not listed in valid surfaces!')
surface = surface_modules.get(next_surface).Surface((game_res, game_res), resources_dir, game_fps)
print(surface.name)
2023-05-11 12:20:07 -04:00
pygame.display.flip()
2023-05-11 11:51:51 -04:00
def main():
"""
Calls the game() function to start the game.
"""
game()
pygame.quit()