Пример #1
0
class TextGame:
    __slots__ = ('game',)
    def __init__(self):
        self.game = GameLogic()

    def start(self):
        while not self.game.is_finished():
            self.game.do_turn()
            self.game.print_events()

        if self.game.countries.get_alive_count() == 1:
            alive = self.game.countries.get_survivor()
            print(alive, "is the last one standing.")

        else:
            print("There were no survivors.")

        print("Hit enter to exit.")
        input()
Пример #2
0
class PyGame:
    BATCH = False
    FPS = 60
    TURN_LENGTH = 1

    def __init__(self, window: pygame.Surface):
        self.end_game = None
        self.fullscreen = False
        self.window = window

        self.game = GameLogic()
        self.clock = pygame.time.Clock()
        self.active_weapons = ActiveWeapons()
        self.explosions = Explosions()
        self.lasers = Lasers()
        self.particles = Particles()
        self.shake = Shake()
        self.timer = time.time()

        self.countries = Countries(self.game.countries.countries, SIZE)

    def start(self):
        global SIZE

        running = True
        self.turn_surface = TITLE_FONT.render("ROUND " + str(self.game.turn),
                                              True, GUI_COLOUR)

        while running:
            for event in pygame.event.get():
                if PyGame.quit_game(event):
                    running = False
                    pygame.quit()
                    return 0

                elif PyGame.press_f11(event):
                    self.toggle_fullscreen()

                elif event.type == pygame.VIDEORESIZE:
                    SIZE = event.size
                    pygame.display.set_mode(event.size, DEFAULT_FLAG)
                    self.countries.resize(*event.size)

            # Refresh screen
            self.window.fill(BLACK)
            self.explosions.draw(self.window)
            self.window.blit(self.turn_surface, (10, 10))

            self.countries.draw(self.window)
            self.lasers.draw(self.window, self.FPS)
            self.particles.draw(self.window, self.FPS)
            self.active_weapons.draw(self.window)

            if time.time() - self.timer > self.TURN_LENGTH * self.game.turn:
                if not self.game.is_finished():
                    self.game.do_turn()
                    self.game.print_events()
                    self.animate_turn()
                    self.turn_surface = TITLE_FONT.render(
                        f"Round {self.game.turn}", True, GUI_COLOUR)

                elif not self.end_game:
                    self.end_game = time.time()

                    if self.BATCH:
                        self.end_game += 5

            if self.shake.is_active():
                self.shake.animate(self.window)

            pygame.display.update()
            self.clock.tick(self.FPS)

            if self.end_game and self.end_game < time.time():
                break

        if self.game.countries.get_alive_count():
            alive = self.game.countries.get_survivor()
            print(f"{alive} is the last one standing.")

        else:
            print("There were no survivors.")

        if not self.BATCH:
            self._finish_game()

    def animate_turn(self):
        for event in self.game.events:
            if "Attack" in event:
                if event["Attack"]["Success"]:
                    start = self.countries.get_pos(event["Attack"]["Source"])
                    end_pos = self.countries.get_pos(event["Attack"]["Target"])

                    if event["Attack"]["Weapon"] == Weapons.LASER:
                        self.lasers.add(start, end_pos, self.TURN_LENGTH)
                    else:
                        self.active_weapons.add(start, end_pos, event,
                                                self.TURN_LENGTH)

            elif "Death" in event:
                end_pos = self.countries.get_pos(event["Death"]["Target"])
                self.particles.add(end_pos)

            elif "Hit" in event:
                if event["Hit"]["Weapon"] == Weapons.NUKE:
                    self.shake.start(40)

                pos = self.countries.get_pos(event["Hit"]["Target"])
                self.explosions.add(pos, event["Hit"]["Weapon"])

    def _finish_game(self):
        running = True
        while running:
            for event in pygame.event.get():
                if PyGame.quit_game(event):
                    running = False
                    pygame.quit()
                    return 0

                elif PyGame.press_f11(event):
                    self.toggle_fullscreen()

            self.clock.tick(self.FPS)

    @staticmethod
    def press_f11(event):
        return event.type == pygame.KEYUP and event.key == pygame.K_F11

    @staticmethod
    def quit_game(event):
        return (event.type == pygame.QUIT or
                (event.type == pygame.KEYUP and event.key == pygame.K_ESCAPE))

    def toggle_fullscreen(self):
        self.fullscreen = not self.fullscreen

        if self.fullscreen:
            flag = DEFAULT_FLAG | pygame.FULLSCREEN

        pygame.display.set_mode(SIZE, flag)
Пример #3
0
class PyGame:
    BATCH = False
    FPS = 60
    WIDTH = 800
    HEIGHT = 600
    TURN_LENGTH = 1

    def __init__(self, window: pygame.Surface):
        self.game = GameLogic()
        self.window = window
        self.clock = pygame.time.Clock()
        self.active_weapons = ActiveWeapons()
        self.explosions = Explosions()
        self.particles = Particles()
        self.timer = time.time()
        self.end_game = None

        self.countries = Countries(self.game.countries.countries, self.WIDTH,
                                   self.HEIGHT)

    def start(self):
        running = True
        self.turn_surface = TITLE_FONT.render("Round " + str(self.game.turn),
                                              True, GREY)

        while running:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    running = False
                    pygame.quit()
                    return 0

            # Refresh screen
            self.window.fill(BLACK)
            self.window.blit(self.turn_surface, (0, 0))

            self.countries.draw(self.window)
            self.explosions.draw(self.window)
            self.particles.draw(self.window, self.FPS)

            explosions = self.active_weapons.draw(self.window)
            for e in explosions:
                self.explosions.add(*e)

            if time.time() - self.timer > self.TURN_LENGTH * self.game.turn:
                if not self.game.is_finished():
                    self.game.do_turn()
                    self.game.print_events()
                    self.animate_turn()
                    self.turn_surface = TITLE_FONT.render(
                        "Round " + str(self.game.turn), True, GREY)

                elif not self.end_game:
                    self.end_game = time.time()
                    self.end_game += int(not self.BATCH)

            pygame.display.update()
            self.clock.tick(self.FPS)

            if self.end_game and self.end_game < time.time():
                break

        if self.game.countries.get_alive_count() == 1:
            alive = self.game.countries.get_survivor()
            print(alive, "is the last one standing.")

        else:
            print("There were no survivors.")

        if not self.BATCH:
            self._finish_game()

    def animate_turn(self):
        for event in self.game.events:
            if "Source" in event and "Target" in event and event["Success"]:
                start = self.countries.countries[event["Source"]].inner.center
                end = self.countries.countries[event["Target"]].inner.center

                self.active_weapons.add(start, end, event, self.TURN_LENGTH)

            elif "Death" in event:
                pos = self.countries.get_pos(event["Death"]["Target"])
                self.particles.add(pos)

    def _finish_game(self):
        running = True
        while running:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    running = False
                    pygame.quit()

                    return 0

            self.clock.tick(self.FPS)