This repository has been archived on 2026-02-27. You can view files and clone it, but cannot push or open issues or pull requests.
Files
PainJamLauncher/__main__.py

100 lines
2.6 KiB
Python

import importlib.util
import argparse
import logging
import tomllib
import pygame
import attr
import sys
import os
# TODO: Auto install game 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()
# 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('-fps', '--fpsCap', type=int, default=60)
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()