summaryrefslogtreecommitdiff
path: root/__main__.py
blob: d0aeb17ec98ddbf6c1dbfe1290683c0f1e103629 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/env python3

""" Entry point to play the game """

import argparse

import pygame

from cameltris.controller import Controller, KeyboardController, JoystickController
from cameltris.screens.base_screen import Screen
from cameltris.screens.in_game import InGame as InGameScreen, Player
from cameltris.screens.pause import Pause as PauseScreen
from cameltris.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()))