From c3b09543f50b49d6b91dfc93e6d84166cd4535f3 Mon Sep 17 00:00:00 2001 From: Olivier Gayot Date: Thu, 30 Dec 2021 15:25:05 +0100 Subject: Move __main__.py inside the package Signed-off-by: Olivier Gayot --- cameltris/__main__.py | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100755 cameltris/__main__.py (limited to 'cameltris/__main__.py') diff --git a/cameltris/__main__.py b/cameltris/__main__.py new file mode 100755 index 0000000..a3333c5 --- /dev/null +++ b/cameltris/__main__.py @@ -0,0 +1,61 @@ +#!/usr/bin/env python3 + +""" Entry point to play the game """ + +import argparse + +import pygame + +from .controller import Controller, KeyboardController, JoystickController +from .screens.base_screen import Screen +from .screens.in_game import InGame as InGameScreen, Player +from .screens.pause import Pause as PauseScreen +from .misc import Pause, UnPause + + +def main(args: dict): + """ Main function to be called with option arguments already parsed """ + pygame.init() + + screen = pygame.display.set_mode((801, 1000)) + + controller: Controller + if args["joystick_id"] is not None: + joystick = pygame.joystick.Joystick(args["joystick_id"]) + joystick.init() + controller = JoystickController(joystick) + else: + controller = KeyboardController(pygame.key) + + # Just one player + players = [Player(controller, args["starting_level"])] + + for player in players: + player.refresh_piece_preview_canvas() + + clock = pygame.time.Clock() + + current_screen: Screen + current_screen = ingame_screen = InGameScreen(players, screen) + + while True: + try: + current_screen.oneframe() + except Pause: + current_screen = PauseScreen(screen) + except UnPause: + current_screen = ingame_screen + + current_screen.refresh() + pygame.display.update() + + clock.tick(60) + + +if __name__ == "__main__": + PARSER = argparse.ArgumentParser() + + PARSER.add_argument("--starting-level", type=int, choices=list(range(1, 30)), default=1) + PARSER.add_argument("--joystick", type=int, dest="joystick_id", metavar="Joystick ID") + + main(vars(PARSER.parse_args())) -- cgit v1.2.3