Ejemplo n.º 1
0
    def __menu(self):
        print("entering __menu")

        menu = Menu(self.graphics, ("play", "how to play", "exit"))

        state_to_return = "EXIT"
        goon = True
        while goon:
            # events
            for event in pygame.event.get():
                if event.type == QUIT:
                    goon = False

                menu.event(event)

            # updates
            menu.update()

            choice = menu.get_clicked_button_txt()

            if choice == "play":
                state_to_return = "GAME"
                goon = False

            if choice == "how to play":
                state_to_return = "HOWTOPLAY"
                goon = False

            #if choice == "credits":
            #    state_to_return = "CREDITS"
            #    goon = False

            if choice == "exit":
                state_to_return = "EXIT"
                goon = False

            # draws
            finalrender = pygame.Surface((c.WINDOW_WIDTH, c.WINDOW_HEIGHT))

            finalrender.blit(menu.draw(), (0, 0))

            self.screen.blit(
                pygame.transform.scale(finalrender,
                                       (c.WINDOW_WIDTH, c.WINDOW_HEIGHT)),
                (0, 0))

            pygame.display.flip()

            self.clock.tick(30)

        print("exiting __menu")

        return state_to_return
Ejemplo n.º 2
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()
Ejemplo n.º 3
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()