Example #1
0
    def run(cls):
        """Запуск игры"""
        while not cls.finished:
            cls.handleKeys()
            mx, my = pygame.mouse.get_pos()
            mouse_pos = vector(mx, my)
            mouse_pressed = pygame.mouse.get_pressed()[0]
            Menu.state = GameState.MENU
            if cls.state == GameState.MENU:
                Display.draw_start_img(
                    pygame.image.load("data/game_images/start1.png"),
                    vector(0, 0))
                Display.draw_start_img(
                    pygame.image.load("data/game_images/f12.png"),
                    vector(50, 0))
                Display.draw_start_img(
                    pygame.image.load("data/game_images/esc.png"),
                    vector(0, 50))
                Display.draw_start_img(
                    pygame.image.load("data/game_images/space.png"),
                    vector(50, 50))
                Menu.update(mouse_pos, mouse_pressed)
                if Menu.can_play:
                    cls.state = GameState.GAME
                if Menu.can_quit:
                    cls.finished = True
                Menu.display()
                if Menu.can_play:
                    Display.set_cursor()

            elif cls.state == GameState.END:
                Menu.update(mouse_pos, mouse_pressed)
                if not Map.end:
                    Map.update()
                if Menu.can_play:
                    cls.state = GameState.GAME
                    Display.end = False
                    Map.end = False
                if Menu.can_quit:
                    cls.finished = True
                Map.display()
                Menu.display()
                if Menu.can_play:
                    Display.set_cursor()
            elif cls.state == GameState.WIN:
                Menu.update(mouse_pos, mouse_pressed)
                if Menu.can_play:
                    cls.state = GameState.GAME
                    Display.end = False
                    Map.end = False
                if Menu.can_quit:
                    cls.finished = True
                Menu.display()
                if Menu.can_play:
                    Display.set_cursor()
            elif cls.state == GameState.GAME:
                Map.set_mouse(mouse_pos / Display.zoom_factor)
                Map.update()
                Map.display()
                if Map.is_player_alive():
                    if Map.end:
                        cls.state = GameState.WIN
                        Menu.apply_state(GameState.WIN)
                else:
                    cls.state = GameState.END
                    Menu.apply_state(GameState.END)
            Display.update_frame()
Example #2
0
    def run(cls) -> None:
        """Méthode principale permettant de faire tourner le jeu"""

        cls.finished = False

        cls.keytime = {}

        cls.state = GameState.MENU

        while not cls.finished:
            cls.ESC_MAX_FRAMECOUNT = Display.real_framerate * 0.25
            # On veut quitter après 0.25 secondes

            cls.handleKeys()

            mx, my = pygame.mouse.get_pos()
            mouse_pos = Vect2d(mx, my)
            # Position de la souris

            mouse_pressed = pygame.mouse.get_pressed()[0]
            # Clic ou non

            Menu.state = GameState.MENU

            if cls.state == GameState.MENU:
                # Affichage du menu

                Menu.update(mouse_pos, mouse_pressed)

                if Menu.can_play:
                    # Si l'on veut rejouer
                    cls.state = GameState.GAME

                if Menu.can_quit:
                    # Si l'on veut quitter
                    cls.finished = True

                Menu.display()

                if Menu.can_play:
                    # Si l'on veut rejouer on remet le curseur normal
                    Display.setCursorArrow()

            elif cls.state == GameState.END:
                # Partie perdue

                Menu.update(mouse_pos, mouse_pressed)

                if not Map.game_finished:
                    # On update la map tant que la partie n'est pas terminée pour de bon
                    # afin de faire jouer les IA
                    Map.update()

                if Menu.can_play:
                    cls.state = GameState.GAME

                if Menu.can_quit:
                    cls.finished = True

                Map.display()
                Menu.display()
                # Affichage du menu et de l'effet de transparence au dessus de la map

                if Menu.can_play:
                    Display.setCursorArrow()
            elif cls.state == GameState.WIN:
                # Victoire

                Menu.update(mouse_pos, mouse_pressed)

                if Menu.can_play:
                    cls.state = GameState.GAME

                if Menu.can_quit:
                    cls.finished = True

                Menu.display()

                if Menu.can_play:
                    Display.setCursorArrow()
            elif cls.state == GameState.GAME:
                # Jeu en cours

                Map.setMousePos(mouse_pos / Display.zoom_factor)
                # On envoie la position de la souris à la map

                Map.update()
                Map.display()

                if Map.isPlayerAlive():
                    if Map.game_finished:
                        cls.state = GameState.WIN
                        Menu.applyState(GameState.WIN)
                else:
                    cls.state = GameState.END
                    Menu.applyState(GameState.END)
            else:
                raise ValueError("État inconnu")

            alpha = cls.keytime.get(pygame.K_ESCAPE,
                                    0) / cls.ESC_MAX_FRAMECOUNT * 255

            Display.drawText("Quitter...",
                             Vect2d(50, 25),
                             color=(255, 0, 0, alpha),
                             size=16,
                             base_pos=Vect2d(0, 0))

            # Affichage du "Quitter" rouge en haut à gauche proportionnellement au temps d'appui de la touche ESC

            Display.updateFrame()