Exemplo n.º 1
0
class LevelTransition:
    """Displays a level transition"""
    TRANSITION_CHANNEL = 4

    def __init__(self, screen, score_controller, transition_time=5000):
        self.screen = screen
        self.score_controller = score_controller
        self.sound = SoundManager(['pacman_beginning.wav'],
                                  keys=['transition'],
                                  channel=LevelTransition.TRANSITION_CHANNEL,
                                  volume=0.6)
        self.font = pygame.font.SysFont(None, 32)
        self.ready_msg = self.font.render('Get Ready!', True,
                                          ScoreBoard.SCORE_WHITE)
        self.ready_msg_rect = self.ready_msg.get_rect()
        ready_pos = screen.get_width() // 2, int(screen.get_height() * 0.65)
        self.ready_msg_rect.centerx, self.ready_msg_rect.centery = ready_pos
        self.level_msg = None
        self.level_msg_rect = None
        self.transition_time = transition_time  # total time to wait until the transition ends
        self.transition_begin = None
        self.transition_show = False

    def prep_level_msg(self):
        """Prepare a message for the current level number"""
        text = 'level ' + str(self.score_controller.level)
        self.level_msg = self.font.render(text, True, ScoreBoard.SCORE_WHITE)
        self.level_msg_rect = self.level_msg.get_rect()
        level_pos = self.screen.get_width() // 2, self.screen.get_height() // 2
        self.level_msg_rect.centerx, self.level_msg_rect.centery = level_pos

    def set_show_transition(self):
        """Begin the sequence for displaying the transition"""
        self.prep_level_msg()
        self.transition_begin = pygame.time.get_ticks()
        self.transition_show = True
        self.sound.play('transition')

    def draw(self):
        """Display the level transition to the screen"""
        if abs(self.transition_begin -
               pygame.time.get_ticks()) > self.transition_time:
            self.transition_show = False
        else:
            self.screen.fill((0, 0, 0))
            self.screen.blit(self.level_msg, self.level_msg_rect)
            if abs(self.transition_begin -
                   pygame.time.get_ticks()) >= self.transition_time // 2:
                self.screen.blit(self.ready_msg, self.ready_msg_rect)
Exemplo n.º 2
0
 def __init__(self, screen, user, maze):
     self.screen = screen
     self.maze = maze
     self.user = user
     self.sound_manager = SoundManager(
         sound_files=['portal-open.wav', 'portal-travel.wav'],
         keys=['open', 'travel'],
         channel=PortalController.PORTAL_AUDIO_CHANNEL)
     self.blue_portal = pygame.sprite.GroupSingle(
     )  # portals as GroupSingle, which only allows one per group
     self.blue_projectile = None
     self.orange_portal = pygame.sprite.GroupSingle()
     self.orange_projectile = None
     # converter for projectile direction to portal direction
     self.portal_directions = {'l': 'r', 'r': 'l', 'u': 'd', 'd': 'u'}
Exemplo n.º 3
0
 def __init__(self):
     pygame.init()
     pygame.mixer.music.load('sounds/bg-music.wav')
     self.screen = pygame.display.set_mode(
         (800, 600)
     )
     pygame.display.set_caption('PacMan Portal')
     self.clock = pygame.time.Clock()
     self.score_keeper = ScoreController(screen=self.screen,
                                         sb_pos=((self.screen.get_width() // 5),
                                                 (self.screen.get_height() * 0.965)),
                                         items_image='cherry.png',
                                         itc_pos=(int(self.screen.get_width() * 0.6),
                                                  self.screen.get_height() * 0.965))
     self.maze = Maze(screen=self.screen, maze_map_file='maze_map.txt')
     self.life_counter = PacManCounter(screen=self.screen, ct_pos=((self.screen.get_width() // 3),
                                                                   (self.screen.get_height() * 0.965)),
                                       images_size=(self.maze.block_size, self.maze.block_size))
     self.level_transition = LevelTransition(screen=self.screen, score_controller=self.score_keeper)
     self.game_over = True
     self.pause = False
     self.player = PacMan(screen=self.screen, maze=self.maze)
     self.ghosts = pygame.sprite.Group()
     self.ghost_sound_manager = SoundManager(sound_files=['ghost-blue.wav', 'pacman_eatghost.wav', 'ghost-std.wav'],
                                             keys=['blue', 'eaten', 'std'],
                                             channel=Ghost.GHOST_AUDIO_CHANNEL)
     self.ghost_active_interval = 2500
     self.ghosts_to_activate = None
     self.first_ghost = None
     self.other_ghosts = []
     self.spawn_ghosts()
     self.actions = {PacManPortalGame.START_EVENT: self.init_ghosts,
                     PacManPortalGame.REBUILD_EVENT: self.rebuild_maze,
                     PacManPortalGame.LEVEL_TRANSITION_EVENT: self.next_level}
Exemplo n.º 4
0
    def __init__(self, screen, maze):
        super().__init__()
        self.screen = screen
        self.radius = maze.block_size // 5
        self.maze = maze
        self.sound_manager = SoundManager(sound_files=['pacman_chomp.wav', 'pacman_eatfruit.wav',
                                                       'pacman_death.wav', 'pacman-portal.wav'],
                                          keys=['chomp', 'eatfruit', 'death', 'portal'],
                                          channel=PacMan.PAC_AUDIO_CHANNEL)
        self.horizontal_images = ImageManager('pacman-horiz.png', sheet=True, pos_offsets=[(0, 0, 32, 32),
                                                                                           (32, 0, 32, 32),
                                                                                           (0, 32, 32, 32),
                                                                                           (32, 32, 32, 32),
                                                                                           (0, 64, 32, 32)],
                                              resize=(self.maze.block_size, self.maze.block_size),
                                              reversible=True)
        self.vertical_images = ImageManager('pacman-vert.png', sheet=True, pos_offsets=[(0, 0, 32, 32),
                                                                                        (32, 0, 32, 32),
                                                                                        (0, 32, 32, 32),
                                                                                        (32, 32, 32, 32),
                                                                                        (0, 64, 32, 32)],
                                            resize=(self.maze.block_size, self.maze.block_size),
                                            reversible=True)
        self.death_images = ImageManager('pacman_death.png', sheet=True, pos_offsets=[(0, 0, 32, 32),
                                                                                      (32, 0, 32, 32),
                                                                                      (0, 32, 32, 32),
                                                                                      (32, 32, 32, 32),
                                                                                      (0, 64, 32, 32),
                                                                                      (32, 64, 32, 32)],
                                         resize=(self.maze.block_size, self.maze.block_size),
                                         animation_delay=150, repeat=False)
        self.flip_status = {'use_horiz': True, 'h_flip': False, 'v_flip': False}
        self.spawn_info = self.maze.player_spawn[1]
        self.tile = self.maze.player_spawn[0]
        self.direction = None
        self.moving = False
        self.speed = maze.block_size // 7
        self.image, self.rect = self.horizontal_images.get_image()
        self.rect.centerx, self.rect.centery = self.spawn_info   # screen coordinates for spawn
        self.dead = False
        self.portal_controller = PortalController(screen, self, maze)   # controller object for portal

        # Keyboard related events/actions/releases
        self.event_map = {pygame.KEYDOWN: self.perform_action, pygame.KEYUP: self.reset_direction}
        self.action_map = {pygame.K_UP: self.set_move_up, pygame.K_LEFT: self.set_move_left,
                           pygame.K_DOWN: self.set_move_down, pygame.K_RIGHT: self.set_move_right,
                           pygame.K_q: self.blue_portal, pygame.K_w: self.orange_portal}
Exemplo n.º 5
0
 def __init__(self, screen, score_controller, transition_time=5000):
     self.screen = screen
     self.score_controller = score_controller
     self.sound = SoundManager(['pacman_beginning.wav'],
                               keys=['transition'],
                               channel=LevelTransition.TRANSITION_CHANNEL,
                               volume=0.6)
     self.font = pygame.font.SysFont(None, 32)
     self.ready_msg = self.font.render('Get Ready!', True,
                                       ScoreBoard.SCORE_WHITE)
     self.ready_msg_rect = self.ready_msg.get_rect()
     ready_pos = screen.get_width() // 2, int(screen.get_height() * 0.65)
     self.ready_msg_rect.centerx, self.ready_msg_rect.centery = ready_pos
     self.level_msg = None
     self.level_msg_rect = None
     self.transition_time = transition_time  # total time to wait until the transition ends
     self.transition_begin = None
     self.transition_show = False
Exemplo n.º 6
0
class PacMan(pygame.sprite.Sprite):
    """Represents the player character 'PacMan' and its related logic/control"""
    PAC_YELLOW = (255, 255, 0)
    PAC_AUDIO_CHANNEL = 0

    def __init__(self, screen, maze):
        super().__init__()
        self.screen = screen
        self.radius = maze.block_size // 5
        self.maze = maze
        self.sound_manager = SoundManager(sound_files=['pacman_chomp.wav', 'pacman_eatfruit.wav',
                                                       'pacman_death.wav', 'pacman-portal.wav'],
                                          keys=['chomp', 'eatfruit', 'death', 'portal'],
                                          channel=PacMan.PAC_AUDIO_CHANNEL)
        self.horizontal_images = ImageManager('pacman-horiz.png', sheet=True, pos_offsets=[(0, 0, 32, 32),
                                                                                           (32, 0, 32, 32),
                                                                                           (0, 32, 32, 32),
                                                                                           (32, 32, 32, 32),
                                                                                           (0, 64, 32, 32)],
                                              resize=(self.maze.block_size, self.maze.block_size),
                                              reversible=True)
        self.vertical_images = ImageManager('pacman-vert.png', sheet=True, pos_offsets=[(0, 0, 32, 32),
                                                                                        (32, 0, 32, 32),
                                                                                        (0, 32, 32, 32),
                                                                                        (32, 32, 32, 32),
                                                                                        (0, 64, 32, 32)],
                                            resize=(self.maze.block_size, self.maze.block_size),
                                            reversible=True)
        self.death_images = ImageManager('pacman_death.png', sheet=True, pos_offsets=[(0, 0, 32, 32),
                                                                                      (32, 0, 32, 32),
                                                                                      (0, 32, 32, 32),
                                                                                      (32, 32, 32, 32),
                                                                                      (0, 64, 32, 32),
                                                                                      (32, 64, 32, 32)],
                                         resize=(self.maze.block_size, self.maze.block_size),
                                         animation_delay=150, repeat=False)
        self.flip_status = {'use_horiz': True, 'h_flip': False, 'v_flip': False}
        self.spawn_info = self.maze.player_spawn[1]
        self.tile = self.maze.player_spawn[0]
        self.direction = None
        self.moving = False
        self.speed = maze.block_size // 7
        self.image, self.rect = self.horizontal_images.get_image()
        self.rect.centerx, self.rect.centery = self.spawn_info   # screen coordinates for spawn
        self.dead = False
        self.portal_controller = PortalController(screen, self, maze)   # controller object for portal

        # Keyboard related events/actions/releases
        self.event_map = {pygame.KEYDOWN: self.perform_action, pygame.KEYUP: self.reset_direction}
        self.action_map = {pygame.K_UP: self.set_move_up, pygame.K_LEFT: self.set_move_left,
                           pygame.K_DOWN: self.set_move_down, pygame.K_RIGHT: self.set_move_right,
                           pygame.K_q: self.blue_portal, pygame.K_w: self.orange_portal}

    def clear_portals(self):
        """Remove all portals from play"""
        self.portal_controller.clear_portals()

    def blue_portal(self):
        """Create a blue portal from PacMan"""
        self.sound_manager.play('portal')
        self.portal_controller.fire_b_portal_projectile()

    def orange_portal(self):
        """Create an orange portal from PacMan"""
        self.sound_manager.play('portal')
        self.portal_controller.fire_o_portal_projectile()

    def set_death(self):
        """Set the death flag for PacMan and begin the death animation"""
        self.sound_manager.play('dead')
        self.dead = True
        self.image, _ = self.death_images.get_image()

    def revive(self):
        """Set dead to False and give PacMan a default image"""
        self.dead = False
        self.image, _ = self.horizontal_images.get_image()
        self.death_images.image_index = 0

    def reset_position(self):
        """Reset position back to pre-define spawn location"""
        self.rect.centerx, self.rect.centery = self.spawn_info  # screen coordinates for spawn

    def reset_direction(self, event):
        """Reset the movement direction if key-up on movement keys"""
        if event.key in (pygame.K_UP, pygame.K_DOWN, pygame.K_LEFT, pygame.K_RIGHT):
            self.moving = False

    def perform_action(self, event):
        """Change direction based on the event key"""
        if event.key in self.action_map:
            self.action_map[event.key]()

    def set_move_up(self):
        """Set move direction up"""
        if self.direction != 'u':
            self.direction = 'u'
            if self.flip_status['v_flip']:
                self.vertical_images.flip(False, True)
                self.flip_status['v_flip'] = False
            self.flip_status['use_horiz'] = False
        self.moving = True

    def set_move_left(self):
        """Set move direction left"""
        if self.direction != 'l':
            self.direction = 'l'
            if not self.flip_status['h_flip']:
                self.horizontal_images.flip()
                self.flip_status['h_flip'] = True
            self.flip_status['use_horiz'] = True
        self.moving = True

    def set_move_down(self):
        """Set move direction down"""
        if self.direction != 'd':
            self.direction = 'd'
            if not self.flip_status['v_flip']:
                self.vertical_images.flip(x_bool=False, y_bool=True)
                self.flip_status['v_flip'] = True
            self.flip_status['use_horiz'] = False
        self.moving = True

    def set_move_right(self):
        """Set move direction to right"""
        if self.direction != 'r':
            self.direction = 'r'
            if self.flip_status['h_flip']:
                self.horizontal_images.flip()
                self.flip_status['h_flip'] = False
            self.flip_status['use_horiz'] = True
        self.moving = True

    def get_nearest_col(self):
        """Get the current column location on the maze map"""
        return (self.rect.x - (self.screen.get_width() // 5)) // self.maze.block_size

    def get_nearest_row(self):
        """Get the current row location on the maze map"""
        return (self.rect.y - (self.screen.get_height() // 12)) // self.maze.block_size

    def is_blocked(self):
        """Check if PacMan is blocked by any maze barriers, return True if blocked, False if clear"""
        result = False
        if self.direction is not None and self.moving:
            original_pos = self.rect
            if self.direction == 'u':
                test = self.rect.move((0, -self.speed))
            elif self.direction == 'l':
                test = self.rect.move((-self.speed, 0))
            elif self.direction == 'd':
                test = self.rect.move((0, self.speed))
            else:
                test = self.rect.move((self.speed, 0))
            self.rect = test    # temporarily move self

            # if any collision, result = True
            if pygame.sprite.spritecollideany(self, self.maze.maze_blocks):
                result = True
            elif pygame.sprite.spritecollideany(self, self.maze.shield_blocks):
                result = True
            elif not self.portal_controller.portables_usable():
                result = self.portal_controller.collide_portals(self)
            self.rect = original_pos    # reset position
        return result

    def update(self):
        """Update PacMan's position in the maze if moving, and if not blocked"""
        if not self.dead:
            self.portal_controller.update()
            self.portal_controller.check_portals(self)
            if self.direction and self.moving:
                if self.flip_status['use_horiz']:
                    self.image = self.horizontal_images.next_image()
                else:
                    self.image = self.vertical_images.next_image()
                if not self.is_blocked():
                    if self.direction == 'u':
                        self.rect.centery -= self.speed
                    elif self.direction == 'l':
                        self.rect.centerx -= self.speed
                    elif self.direction == 'd':
                        self.rect.centery += self.speed
                    elif self.direction == 'r':
                        self.rect.centerx += self.speed
                self.tile = (self.get_nearest_row(), self.get_nearest_col())
        else:
            self.image = self.death_images.next_image()

    def blit(self):
        """Blit the PacMan sprite to the screen"""
        self.portal_controller.blit()
        self.screen.blit(self.image, self.rect)

    def eat(self):
        """Eat pellets from the maze and return the score accumulated"""
        score = 0
        fruit_count = 0
        power = None
        collision = pygame.sprite.spritecollideany(self, self.maze.pellets)
        if collision:
            collision.kill()
            score += 10
            self.sound_manager.play('chomp')
        collision = pygame.sprite.spritecollideany(self, self.maze.fruits)
        if collision:
            collision.kill()
            score += 20
            fruit_count += 1
            self.sound_manager.play('eatfruit')
        collision = pygame.sprite.spritecollideany(self, self.maze.power_pellets)
        if collision:
            collision.kill()
            score += 20
            power = True
            self.sound_manager.play('chomp')
        return score, fruit_count, power
Exemplo n.º 7
0
class PortalController:
    """Manages portals and their related functionality within the game"""
    PORTAL_AUDIO_CHANNEL = 3

    def __init__(self, screen, user, maze):
        self.screen = screen
        self.maze = maze
        self.user = user
        self.sound_manager = SoundManager(
            sound_files=['portal-open.wav', 'portal-travel.wav'],
            keys=['open', 'travel'],
            channel=PortalController.PORTAL_AUDIO_CHANNEL)
        self.blue_portal = pygame.sprite.GroupSingle(
        )  # portals as GroupSingle, which only allows one per group
        self.blue_projectile = None
        self.orange_portal = pygame.sprite.GroupSingle()
        self.orange_projectile = None
        # converter for projectile direction to portal direction
        self.portal_directions = {'l': 'r', 'r': 'l', 'u': 'd', 'd': 'u'}

    def clear_portals(self):
        """Remove all portals and projectiles"""
        self.blue_portal.empty()
        self.orange_portal.empty()
        self.blue_projectile = None
        self.orange_projectile = None

    def fire_b_portal_projectile(self):
        """Create a projectile for generating a blue portal"""
        if self.user.direction is not None:
            self.blue_projectile = PortalProjectile(
                screen=self.screen,
                source=self.user,
                direction=self.user.direction,
                p_type=Portal.P_TYPE_1)

    def fire_o_portal_projectile(self):
        """Create a projectile for generating an orange portal"""
        if self.user.direction is not None:
            self.orange_projectile = PortalProjectile(
                screen=self.screen,
                source=self.user,
                direction=self.user.direction,
                p_type=Portal.P_TYPE_2)

    def create_blue_portal(self, x, y, direction):
        """Create a blue portal, replacing the location it originally took up with a normal maze block"""
        if self.blue_portal:
            old_x, old_y = self.blue_portal.sprite.rect.x, self.blue_portal.sprite.rect.y
            self.maze.maze_blocks.add(
                Block(old_x, old_y, self.maze.block_size, self.maze.block_size,
                      self.maze.block_image))
        self.blue_portal.add(
            Portal(screen=self.screen,
                   x=x,
                   y=y,
                   direction=direction,
                   maze=self.maze,
                   p_type=Portal.P_TYPE_1))

    def create_orange_portal(self, x, y, direction):
        """Create a blue portal, replacing the location it originally took up with a normal maze block"""
        if self.orange_portal:
            old_x, old_y = self.orange_portal.sprite.rect.x, self.orange_portal.sprite.rect.y
            self.maze.maze_blocks.add(
                Block(old_x, old_y, self.maze.block_size, self.maze.block_size,
                      self.maze.block_image))
        self.orange_portal.add(
            Portal(screen=self.screen,
                   x=x,
                   y=y,
                   direction=direction,
                   maze=self.maze,
                   p_type=Portal.P_TYPE_2))

    def update(self):
        """Update the portal controller's display parts and tracking"""
        self.blue_portal.update()
        self.orange_portal.update()
        if self.blue_projectile:
            self.blue_projectile.update()  # update projectile
            # erase projectile if it hits a portal
            if pygame.sprite.spritecollideany(self.blue_projectile, self.blue_portal) or \
                    pygame.sprite.spritecollideany(self.blue_projectile, self.orange_portal):
                self.blue_projectile = None
                return
            collision = pygame.sprite.spritecollideany(self.blue_projectile,
                                                       self.maze.maze_blocks)
            if collision:  # if projectile hits a block, replace it with a blue portal
                x, y = collision.rect.x, collision.rect.y
                collision.kill()  # Replace the block with a portal
                direction = self.portal_directions[
                    self.blue_projectile.direction]
                self.blue_projectile = None  # remove the projectile
                self.create_blue_portal(x, y, direction)
                self.sound_manager.play('open')
            # remove if projectile off screen
            elif self.blue_projectile.is_off_screen():
                self.blue_projectile = None
        if self.orange_projectile:
            self.orange_projectile.update()
            # erase projectile if it hits a portal
            if pygame.sprite.spritecollideany(self.orange_projectile, self.blue_portal) or \
                    pygame.sprite.spritecollideany(self.orange_projectile, self.orange_portal):
                self.orange_projectile = None
                return
            collision = pygame.sprite.spritecollideany(self.orange_projectile,
                                                       self.maze.maze_blocks)
            if collision:  # if projectile hits a block, replace it with an orange portal
                x, y = collision.rect.x, collision.rect.y
                collision.kill()  # Replace the block with a portal
                direction = self.portal_directions[
                    self.orange_projectile.direction]
                self.orange_projectile = None  # remove the projectile
                self.create_orange_portal(x, y, direction)
                self.sound_manager.play('open')
            elif self.orange_projectile.is_off_screen():
                self.orange_projectile = None

    def portables_usable(self):
        """Return True if the portables are usable (i.e. there are two of them)"""
        return self.blue_portal and self.orange_portal

    def collide_portals(self, other):
        """Return True if the sprite is colliding with any portal"""
        return pygame.sprite.spritecollideany(other, self.blue_portal) or \
            pygame.sprite.spritecollideany(other, self.orange_portal)

    def check_portals(self, *args):
        """Check if other sprites have come into contact with the portals, and if so move them"""
        for arg in args:
            if pygame.sprite.spritecollideany(
                    arg, self.blue_portal) and self.orange_portal:
                # get i, j as it relates to the maze map
                i, j = self.orange_portal.sprite.get_nearest_row(
                ), self.orange_portal.sprite.get_nearest_col()
                # move i or j based on portal direction
                if self.orange_portal.sprite.direction == 'l':
                    j -= 1
                elif self.orange_portal.sprite.direction == 'r':
                    j += 1
                elif self.orange_portal.sprite.direction == 'u':
                    i -= 1
                else:
                    i += 1
                # convert i, j to x, y coordinates using same formula as maze class
                x, y = ((self.screen.get_width()) // 5 + (j * self.maze.block_size)), \
                       ((self.screen.get_height()) // 12 + (i * self.maze.block_size))
                arg.rect.x, arg.rect.y = x, y
                self.sound_manager.play('travel')
            elif pygame.sprite.spritecollideany(
                    arg, self.orange_portal) and self.blue_portal:
                # get i, j as it relates to the maze map
                i, j = self.blue_portal.sprite.get_nearest_row(
                ), self.blue_portal.sprite.get_nearest_col()
                # move i or j based on portal direction
                if self.blue_portal.sprite.direction == 'l':
                    j -= 1
                elif self.blue_portal.sprite.direction == 'r':
                    j += 1
                elif self.blue_portal.sprite.direction == 'u':
                    i -= 1
                else:
                    i += 1
                # convert i, j to x, y coordinates using same formula as maze class
                x, y = ((self.screen.get_width() // 5) + (j * self.maze.block_size)), \
                       ((self.screen.get_height() // 12) + (i * self.maze.block_size))
                arg.rect.x, arg.rect.y = x, y
                self.sound_manager.play('travel')

    def blit(self):
        """Blit the portal controller's display components to the screen"""
        if self.blue_projectile:
            self.blue_projectile.blit()
        if self.orange_projectile:
            self.orange_projectile.blit()
        if self.blue_portal:
            self.blue_portal.sprite.blit()
        if self.orange_portal:
            self.orange_portal.sprite.blit()