import importlib.util import argparse import logging import tomllib import pygame import attr import sys import os logging.basicConfig(level=logging.DEBUG) log = logging.getLogger('PainJamLauncher') print(sys.executable) # Store the current working directory # This is used to revert the cd change originalCWD = os.getcwd() # Parse command line arguments parser = argparse.ArgumentParser(prog='Pain Jam launcher') parser.add_argument('-sw', '--width', type=int, default=1280) parser.add_argument('-sh', '--height', type=int, default=720) parser.add_argument('-f', '--fullscreen', action='store_true') args = parser.parse_args() @attr.s class Game: id: str = attr.ib() name: str = attr.ib() description: str = attr.ib() path: str = attr.ib() games = {} # Scan for games for path in os.listdir('../'): gamePath = os.path.join('../', path) # Skip anything that isn't a directory if not os.path.isdir(gamePath): continue # Get the games toml path tomlPath = os.path.join(gamePath, 'game.toml') # Skip all directories that don't have a game.toml file if not os.path.isfile(tomlPath): continue with open(tomlPath, 'r') as f: toml = tomllib.loads(f.read()) gameID = toml['id'] game = Game(gameID, toml['name'], toml['description'], gamePath) games.update({gameID: game}) log.debug(f'Loaded game {gameID}') screen = pygame.display.set_mode((args.width, args.height)) gameSurface = pygame.Surface(screen.size) run = True currentGame = None def loadGame(game): # Set the current working directory # to the base path of the game os.chdir(game.path) spec = importlib.util.spec_from_file_location(game.id, os.path.join(game.path, 'game.py')) foo = importlib.util.module_from_spec(spec) spec.loader.exec_module(foo) return foo.Game(gameSurface) pygame.init() currentGame = loadGame(games['russianRoulette']) while run: for event in pygame.event.get(): if event.type == pygame.QUIT: # If there's no game running if currentGame == None: # Exit the launcher run = False # Otherwise else: # Close the game # TODO: Fade out game? currentGame.close() currentgame = None os.chdir(originalCWD) if currentGame != None: currentGame.onEvent(event) if currentGame != None: currentGame.update() screen.blit(currentGame.surf, (0, 0)) pygame.display.update()