Ejemplo n.º 1
0
class Game:
    BLACK = (0, 0, 0)

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

        self.maze = Maze(self.screen,
                         mazefile='map.txt',
                         brickfile='square',
                         portalfile='square',
                         shieldfile='shield',
                         pointfile='point',
                         dotfile='dot',
                         powerpillfile='powerpill')

        self.pacman = Pacman(self.ai_settings, self.screen, self.maze)
        self.ghosts = Ghosts(self.ai_settings, self.screen, self.maze,
                             self.pacman)
        self.displayname = Button(self.screen, "PacmanPortal")
        self.play_button = Button(self.screen, "PLAY GAME")
        self.highscore_button = Button(self.screen, "HIGH SCORES")
        self.blinkyname = Button(self.screen, "BLiNKY")
        self.pinkyname = Button(self.screen, "PiNKY")
        self.clydename = Button(self.screen, "CLYDE")
        self.inkeyname = Button(self.screen, "iNKEY")
        self.back = Button(self.screen, "Back")
        self.sb = Scoreboard(self.ai_settings, self.screen, self.maze,
                             self.pacman)
        self.score = Button(self.screen, "hello")
        self.blues = 0

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

    def play(self):
        # self.displayname = Button(self.screen, "Portal Pacman")
        self.displayname.rect.centery -= 300
        self.displayname.msg_image_rect.center = self.displayname.rect.center
        # self.highscore_button = Button(self.screen, "Portal Pacman")
        self.highscore_button.rect.centery += 330
        self.highscore_button.msg_image_rect.center = self.highscore_button.rect.center
        # self.play_button = Button(self.screen, "Portal Pacman")
        self.play_button.rect.centery += 280
        self.play_button.msg_image_rect.center = self.play_button.rect.center

        hs_file = open("score.txt", "r")
        self.ai_settings.hs = int(hs_file.read())
        hs_file.close()
        msgg = 'The high score is ' + str(self.ai_settings.hs)
        self.score = Button(self.screen, msgg)
        self.score.rect.centery -= 150
        self.score.msg_image_rect.center = self.score.rect.center
        self.ghosts.intro()
        self.pacman.intro()
        self.blues = pygame.mixer.Sound('sounds/blueghosts.wav')

        eloop = EventLoop(finished=False)
        while not eloop.finished:
            eloop.check_events(self.ai_settings, self.pacman, self.maze,
                               self.play_button, self.highscore_button,
                               self.back, self.ghosts, self.blues, self.sb)
            if self.ai_settings.score > self.ai_settings.hs and self.ai_settings.game_active:
                self.ai_settings.hs = self.ai_settings.score
                msgg = 'The high score is ' + str(self.ai_settings.hs)
                score = Button(self.screen, msgg)
                score.rect.centery += 150
                score.msg_image_rect.center = score.rect.center
            if len(self.maze.dots) == 1:
                self.maze = Maze(self.screen,
                                 mazefile='map.txt',
                                 brickfile='square',
                                 portalfile='square',
                                 shieldfile='shield',
                                 pointfile='point',
                                 dotfile='dot',
                                 powerpillfile='powerpill')
                self.pacman.reset()
                self.ghosts.reset()
                self.ai_settings.level += 1
            if not self.ai_settings.game_active:
                pygame.mouse.set_visible(True)
            self.update_screen()

    def update_screen(self):
        self.pacman.update()
        self.ghosts.update()
        self.screen.fill(Game.BLACK)
        self.maze.blitme()
        self.pacman.blitme()
        self.ghosts.blitme()
        self.sb.prep_score()
        self.sb.prep_high_score()
        # self.sb.prep_pacmans()
        self.sb.show_score()

        if not self.ai_settings.game_active:
            self.screen.fill(self.ai_settings.bg_color)
            self.displayname.draw_button()
            self.highscore_button.draw_button()
            self.play_button.draw_button()
            self.ghosts.introupdate()
            self.ghosts.blitme()
            self.pacman.introupdate()
            self.pacman.blitme()

        if self.ai_settings.shs:
            self.screen.fill(self.ai_settings.bg_color)
            self.back.draw_button()
            self.score.draw_button()

        pygame.display.flip()
Ejemplo n.º 2
0
class Pacman(Sprite):

    def __init__(self, ai_settings, screen, maze):
        """Initialize the ship and set its starting position."""
        super(Pacman, self).__init__()
        self.screen = screen
        self.ai_settings = ai_settings
        self.maze = maze
        self.size = 30
        self.ghosts = Ghosts(self.ai_settings, self.screen, self.maze, self)
        # Get ship from sprite sheet and load its image and rect.
        self.ss = spritesheet.Spritesheet('images/pacman.png')
        self.s1 = spritesheet.Spritesheet('images/pacmandeath.png')
        self.image = self.ss.image_at((0, 0, 16, 16), -1)
        self.image = pygame.transform.scale(self.image, (self.size, self.size))
        self.rect = self.image.get_rect()
        # Initilize all pacman assets
        self.image4 = self.s1.image_at((0, 0, 16, 16), -1)
        self.image4 = pygame.transform.scale(self.image4, (self.size, self.size))
        self.image5 = self.s1.image_at((16, 0, 16, 16), -1)
        self.image5 = pygame.transform.scale(self.image5, (self.size, self.size))
        self.imageright1 = self.ss.image_at((0, 0, 16, 16), -1)
        self.imageright1 = pygame.transform.scale(self.imageright1, (self.size, self.size))
        self.imageright2 = self.ss.image_at((16, 0, 16, 16), -1)
        self.imageright2 = pygame.transform.scale(self.imageright2, (self.size, self.size))
        self.imageleft1 = self.ss.image_at((0, 16, 16, 16), -1)
        self.imageleft1 = pygame.transform.scale(self.imageleft1, (self.size, self.size))
        self.imageleft2 = self.ss.image_at((16, 16, 16, 16), -1)
        self.imageleft2 = pygame.transform.scale(self.imageleft2, (self.size, self.size))
        self.imageup1 = self.ss.image_at((0, 32, 16, 16), -1)
        self.imageup1 = pygame.transform.scale(self.imageup1, (self.size, self.size))
        self.imageup2 = self.ss.image_at((16, 32, 16, 16), -1)
        self.imageup2 = pygame.transform.scale(self.imageup2, (self.size, self.size))
        self.imagedown1 = self.ss.image_at((0, 48, 16, 16), -1)
        self.imagedown1 = pygame.transform.scale(self.imagedown1, (self.size, self.size))
        self.imagedown2 = self.ss.image_at((16, 48, 16, 16), -1)
        self.imagedown2 = pygame.transform.scale(self.imagedown2, (self.size, self.size))

        self.image3 = self.ss.image_at((32, 0, 16, 16), -1)
        self.image3 = pygame.transform.scale(self.image3, (self.size, self.size))
        self.screen = screen
        self.screen_rect = screen.get_rect()
        self.rect.center = self.screen_rect.center
        self.waka = pygame.mixer.Sound('sounds/waka.wav')
        self.intromus = pygame.mixer.Sound('sounds/intro.wav')
        self.dsound = pygame.mixer.Sound('sounds/death.wav')
        self.esound = pygame.mixer.Sound('sounds/eatghost.wav')
        self.dead = 0
        # Load the ship image and get its rect.
        # self.image = pygame.image.load('images/ship.bmp')
        # self.rect = self.image.get_rect()

        # Start each new ship at the bottom center of the screen.
        # self.rect.centerx = self.screen_rect.centerx
        # self.rect.bottom = self.screen_rect.bottom

        # Store a decimal value for the ship's center
        self.centerx = float(self.rect.centerx)
        self.centery = float(self.rect.centery)

        # Movement flags
        self.moving_right = False
        self.moving_left = False
        self.moving_up = False
        self.moving_down = False
        self.tick = 0
        self.i = 0

    def update(self):
        """Update the ship's posisiton based on the movement flags."""
        # Update the ship's center value, not the rect.
        # Update rect object from self.center.
        if self.dead:
            self.dsound.play()
            self.image = self.image3
            self.screen.fill(self.ai_settings.bg_color)
            self.blitme()
            self.maze.blitme()
            pygame.display.flip()
            sleep(.1)
            self.image = self.imageright2
            self.screen.fill(self.ai_settings.bg_color)
            self.blitme()
            self.maze.blitme()
            pygame.display.flip()
            sleep(.1)
            self.image = self.image3
            self.screen.fill(self.ai_settings.bg_color)
            self.blitme()
            self.maze.blitme()
            pygame.display.flip()
            sleep(.1)
            self.image = self.image4
            self.screen.fill(self.ai_settings.bg_color)
            self.blitme()
            self.maze.blitme()
            pygame.display.flip()
            sleep(.1)
            self.image = self.image5
            self.screen.fill(self.ai_settings.bg_color)
            self.blitme()
            self.maze.blitme()
            pygame.display.flip()
            sleep(.1)
            self.dead = 0
            self.reset()
            self.ghosts.reset()
        else:
            if self.moving_right:
                if self.tick == 0:
                    self.image = self.imageright1
                if self.tick == 20:
                    self.image = self.imageright2
                if self.tick == 40:
                    self.image = self.image3
                self.tick += 1
                if self.tick == 60:
                    self.tick = 0
                self.centerx += self.ai_settings.pac_speed_factor
                # self.mr()

            if self.moving_left:
                if self.tick == 0:
                    self.image = self.imageleft1
                if self.tick == 20:
                    self.image = self.imageleft2
                if self.tick == 40:
                    self.image = self.image3
                self.tick += 1
                if self.tick == 60:
                    self.tick = 0
                self.centerx -= self.ai_settings.pac_speed_factor

            if self.moving_up:
                if self.tick == 0:
                    self.image = self.imageup1
                if self.tick == 20:
                    self.image = self.imageup2
                if self.tick == 40:
                    self.image = self.image3
                self.tick += 1
                if self.tick == 60:
                    self.tick = 0
                self.centery -= self.ai_settings.pac_speed_factor

            if self.moving_down:
                if self.tick == 0:
                    self.image = self.imagedown1
                if self.tick == 20:
                    self.image = self.imagedown2
                if self.tick == 40:
                    self.image = self.image3
                self.tick += 1
                if self.tick == 60:
                    self.tick = 0
                self.centery += self.ai_settings.pac_speed_factor

        self.rect.centerx = self.centerx
        self.rect.centery = self.centery

    def reset(self):
        self.rect.center = self.screen_rect.center
        self.centerx = float(self.rect.centerx)
        self.centery = float(self.rect.centery)

    def intro(self):
        self.centerx = 40
        self.centery = 312
        # self.rect.left = self.screen_rect.right

    def introupdate(self):
        if self.ai_settings.intro:
            if self.ai_settings.powerpill:
                self.moving_right = True
                self.centerx += 0.5
                if pygame.Rect.colliderect(self.rect, self.ghosts.powerpill_rect) or self.rect.centerx > 680:
                    self.ghosts.powerpill_rect.centerx = -100
                    self.ai_settings.powerpill = False
            if not self.ai_settings.powerpill:
                self.moving_left = True
                self.moving_right = False
                self.centerx -= 0.5
                if self.rect.centerx < 0:
                    self.ai_settings.introduction = 1

    def blitme(self):
        """Draw the ship at its current location."""
        self.screen.blit(self.image, self.rect)

    def mr(self):
        self.centerx += self.ai_settings.pac_speed_factor

    def ml(self):
        self.centerx -= self.ai_settings.pac_speed_factor

    def mu(self):
        self.centery -= self.ai_settings.pac_speed_factor

    def md(self):
        self.centery += self.ai_settings.pac_speed_factor

    def die(self): pass