Exemplo n.º 1
0
class Game:
    bg_color = (0, 0, 0)

    def __init__(self):
        pygame.init()
        mixer.init()
        ai_settings = Settings()
        self.ai_settings = ai_settings
        self.screen = pygame.display.set_mode(
            (self.ai_settings.screen_width, self.ai_settings.screen_height))
        pygame.display.set_caption("Pacman Portal")

        self.menu = Menu(self.screen, 'Pacman  Portal', 'HIGH SCORE:')
        self.maze = Maze(self.screen, 'images/maze.txt', 'brick', 'dot',
                         'powerpill')
        self.pacman = Pacman(self.screen, self.ai_settings)
        self.pinky = Pinky(self.screen, self.ai_settings)
        self.inky = Inky(self.screen, self.ai_settings)
        self.blinky = Blinky(self.screen, self.ai_settings)
        self.clyde = Clyde(self.screen, self.ai_settings)
        self.sb = Scoreboard(self.ai_settings, self.screen)
        self.load_data()
        self.ghosts = Group()
        self.ghosts.add(self.pinky)
        self.ghosts.add(self.inky)
        self.ghosts.add(self.blinky)
        self.ghosts.add(self.clyde)

    def load_data(self):
        self.dir = path.dirname(__file__)
        with open(path.join(self.dir, self.ai_settings.hs_file), 'r') as f:
            try:
                self.ai_settings.high_score = int(f.read())
            except:
                self.ai_settings.high_score = 0

    def play(self):
        eloop = EventLoop(self.ai_settings.finished)
        self.load_data()

        while not eloop.finished:
            eloop.check_events(self.ai_settings, self.menu, self.pacman)
            self.pacman.check_dot_collision(self.ai_settings, self.maze.dots,
                                            self.maze.pills, self.sb)
            self.sb.check_high_score(self.sb)
            self.update_screen()
            self.pacman.check_wall_collision(self.maze.bricks)
            self.pinky.check_wall_collision(self.maze.bricks)
            self.inky.check_wall_collision(self.maze.bricks)
            self.blinky.check_wall_collision(self.maze.bricks)
            self.clyde.check_wall_collision(self.maze.bricks)
            self.pacman.update()
            self.ghosts.update()
            eloop.update_collisions(self.ai_settings, self.pacman, self.ghosts,
                                    self.maze)

    def update_screen(self):
        self.screen.fill(Game.bg_color)

        if not self.ai_settings.finished:
            self.menu.draw_menu()
            self.sb.check_high_score(self.sb)
            self.sb.prep_high_score()
            self.sb.display_high_score()

        else:
            self.maze.blitme()
            self.pacman.blitme()
            self.ghosts.draw(self.screen)
            self.sb.show_score()

        pygame.display.flip()
Exemplo n.º 2
0
class Game:
    BLACK = (0, 0, 0)

    def __init__(self):
        pygame.init()
        mixer.init()
        ai_settings = Settings()
        self.ai_settings = ai_settings
        self.screen = pygame.display.set_mode(
            (self.ai_settings.screen_width, self.ai_settings.screen_height))
        pygame.display.set_caption("PacMan X Portal")
        self.dir = False
        self.menu = Menu(self.screen, 'PAC-MAN X PORTAL', 'HIGHSCORE:')
        self.sb = Scoreboard(self.ai_settings, self.screen)
        self.load_data()

        self.maze = Maze(self.screen, 'images/pacmanportalmaze_dijkstra.txt',
                         'brick', 'dot', 'powerpill')
        # self.maze = Maze(self.screen, 'images/maze.txt', 'brick', 'dot', 'powerpill')
        self.pacman = Pacman(self.screen, self.ai_settings)

        self.ghosts = Group()

        self.ghost = Ghost(self.screen, ai_settings)

        self.ghosts.add(self.ghost)

    def __str__(self):
        return 'Game(Pacman Portal), maze=' + str(self.maze) + ')'

    def load_data(self):
        self.dir = path.dirname(__file__)
        with open(path.join(self.dir, self.ai_settings.hs_file), 'r') as f:
            try:
                self.ai_settings.high_score = int(f.read())
            except:
                self.ai_settings.high_score = 0

    def play(self):
        eloop = EventLoop(self.ai_settings.finished)

        while not eloop.finished:
            eloop.check_events(self.ai_settings, self.menu, self.pacman)
            self.pacman.check_dot_collision(self.ai_settings, self.maze.dots,
                                            self.maze.pills, self.sb)
            self.sb.check_high_score(self.sb)
            self.update_screen()
            self.pacman.check_wall_collision(self.maze.bricks)
            self.pacman.update()
            self.ghost.update_ghost(self.pacman, self.maze.nodes)

    def update_screen(self):
        self.screen.fill(Game.BLACK)

        if not self.ai_settings.finished:
            self.menu.draw_menu()
            self.sb.check_high_score(self.sb)
            self.sb.prep_high_score()
            self.sb.display_high_score()
        else:
            self.maze.blitme()
            self.pacman.blitme()
            self.ghost.blitme()
            self.sb.show_score()

        pygame.display.flip()