diff --git a/__main__.py b/__main__.py index e59efdd..eeb290e 100644 --- a/__main__.py +++ b/__main__.py @@ -8,12 +8,11 @@ import sys import os # TODO: Auto install game requirements +# TODO: If a game is specified, only install those requirements 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() @@ -24,6 +23,7 @@ parser.add_argument('-sw', '--width', type=int, default=1280) parser.add_argument('-sh', '--height', type=int, default=720) parser.add_argument('-fps', '--fpsCap', type=int, default=60) parser.add_argument('-f', '--fullscreen', action='store_true') +parser.add_argument('gameID', nargs='?', type=str, default=None) args = parser.parse_args() @attr.s @@ -54,46 +54,77 @@ for path in os.listdir('../'): 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']) +# I'm sorry, I prefer object-oriented programming. So sue me. +class Launcher: + def __init__(self): + # The primary display. + self.screen = pygame.display.set_mode((args.width, args.height)) + # The surface that the games render to. + self.gameSurface = pygame.Surface(self.screen.size) + # The current Game object. None means no game is running + self.currentGame = None + self.clock = pygame.time.Clock() + # Is the launcher running (including if a game is running). + self.running = True -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) + pygame.display.set_caption('Pain Jam Launcher') - if currentGame != None: - currentGame.onEvent(event) + def loadGame(self, game): + # Set the current working directory + # to the base path of the game. + os.chdir(game.path) + # Load the games game.py file. + 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) + # Initialise the games Game class, then set the + # current game to that. + self.currentGame = foo.Game(self.gameSurface) + # Set the window title to the games name + pygame.display.set_caption(game.name) - if currentGame != None: - currentGame.update() - screen.blit(currentGame.surf, (0, 0)) + def update(self): + for event in pygame.event.get(): + if self.gameRunning: + self.currentGame.onEvent(event) - pygame.display.update() + # CTRL+ESC + if event.type == pygame.QUIT or (event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE and event.mod == 64): + print(event) + # Close the game. + # TODO: Fade out game? + self.currentGame.close() + self.currentGame = None + os.chdir(originalCWD) + pygame.display.set_caption('Pain Jam Launcher') + else: + if event.type == pygame.QUIT: + # Exit the launcher. + self.running = False + elif event.type == pygame.KEYDOWN: + if event.key == pygame.K_1: + self.loadGame(games['russianRoulette']) + + if self.gameRunning: + self.currentGame.update() + self.screen.blit(self.currentGame.surf, (0, 0)) + else: + self.screen.fill((0, 0, 0)) + + pygame.display.update() + + self.clock.tick(args.fpsCap) + + @property + def gameRunning(self): + return self.currentGame != None + +launcher = Launcher() +if args.gameID != None: + launcher.loadGame(games[args.gameID]) +while launcher.running: + launcher.update()