It's not done, but it "works"

This commit is contained in:
2025-06-22 17:37:06 +01:00
parent 1cbf9ef2af
commit 48bd8126a5

View File

@ -8,12 +8,11 @@ import sys
import os import os
# TODO: Auto install game requirements # TODO: Auto install game requirements
# TODO: If a game is specified, only install those requirements
logging.basicConfig(level=logging.DEBUG) logging.basicConfig(level=logging.DEBUG)
log = logging.getLogger('PainJamLauncher') log = logging.getLogger('PainJamLauncher')
print(sys.executable)
# Store the current working directory # Store the current working directory
# This is used to revert the cd change # This is used to revert the cd change
originalCWD = os.getcwd() 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('-sh', '--height', type=int, default=720)
parser.add_argument('-fps', '--fpsCap', type=int, default=60) parser.add_argument('-fps', '--fpsCap', type=int, default=60)
parser.add_argument('-f', '--fullscreen', action='store_true') parser.add_argument('-f', '--fullscreen', action='store_true')
parser.add_argument('gameID', nargs='?', type=str, default=None)
args = parser.parse_args() args = parser.parse_args()
@attr.s @attr.s
@ -54,46 +54,77 @@ for path in os.listdir('../'):
log.debug(f'Loaded game {gameID}') log.debug(f'Loaded game {gameID}')
screen = pygame.display.set_mode((args.width, args.height))
gameSurface = pygame.Surface(screen.size)
run = True 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() 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: pygame.display.set_caption('Pain Jam Launcher')
for event in pygame.event.get():
if event.type == pygame.QUIT: def loadGame(self, game):
# If there's no game running # Set the current working directory
if currentGame == None: # to the base path of the game.
# Exit the launcher os.chdir(game.path)
run = False # Load the games game.py file.
# Otherwise 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)
def update(self):
for event in pygame.event.get():
if self.gameRunning:
self.currentGame.onEvent(event)
# 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: else:
# Close the game if event.type == pygame.QUIT:
# TODO: Fade out game? # Exit the launcher.
currentGame.close() self.running = False
currentgame = None elif event.type == pygame.KEYDOWN:
os.chdir(originalCWD) if event.key == pygame.K_1:
self.loadGame(games['russianRoulette'])
if currentGame != None: if self.gameRunning:
currentGame.onEvent(event) self.currentGame.update()
self.screen.blit(self.currentGame.surf, (0, 0))
else:
self.screen.fill((0, 0, 0))
if currentGame != None: pygame.display.update()
currentGame.update()
screen.blit(currentGame.surf, (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()