From ec63226505a02745525cad3b6f860b9f15d575cd Mon Sep 17 00:00:00 2001 From: Olivier Gayot Date: Fri, 12 Nov 2021 13:47:06 +0100 Subject: Place main code into main function and document it Signed-off-by: Olivier Gayot --- __main__.py | 66 ++++++++++++++++++++++++++++++++----------------------------- 1 file changed, 35 insertions(+), 31 deletions(-) diff --git a/__main__.py b/__main__.py index e823054..98872c0 100755 --- a/__main__.py +++ b/__main__.py @@ -1,5 +1,7 @@ #!/usr/bin/env python3 +""" Entry point to play the game """ + import argparse import pygame @@ -11,47 +13,49 @@ from cameltris.screens.Pause import Pause as PauseScreen from cameltris.misc import Pause, UnPause -PARSER = argparse.ArgumentParser() +def main(args: dict): + """ Main function to be called with option arguments already parsed """ + pygame.init() -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") + screen = pygame.display.set_mode((801, 1000)) -ARGS = vars(PARSER.parse_args()) + 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) -print(ARGS) + # Just one player + players = [Player(controller, args["starting_level"])] -pygame.init() + for player in players: + player.refresh_piece_preview_canvas() -screen = pygame.display.set_mode((801, 1000)) + clock = pygame.time.Clock() -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) + current_screen: Screen + current_screen = ingame_screen = InGameScreen(players, screen) -# Just one player -players = [Player(controller, ARGS["starting_level"])] + while True: + try: + current_screen.oneframe() + except Pause: + current_screen = PauseScreen(screen) + except UnPause: + current_screen = ingame_screen -for player in players: - player.refresh_piece_preview_canvas() + current_screen.refresh() + pygame.display.update() -clock = pygame.time.Clock() + clock.tick(60) -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 +if __name__ == "__main__": + PARSER = argparse.ArgumentParser() - current_screen.refresh() - pygame.display.update() + 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") - clock.tick(60) + main(vars(PARSER.parse_args())) -- cgit v1.2.3