Beispiel #1
0
class Intro:
    def __init__(self, screen, settings):
        pygame.font.init()
        self.screen = screen
        self.settings = settings
        self.settings.mode = "Intro"
        self.inkey = Ghost(self.screen, 0, 150,
                           'images/ghost/inkey/inkey1.png', "Inkey")
        self.pinky = Ghost(self.screen, 0, 250,
                           'images/ghost/pinky/pinky1.png', "Pinky")
        self.blinky = Ghost(self.screen, 0, 350,
                            'images/ghost/blinky/blinky1.png', "Blinky")
        self.clyde = Ghost(self.screen, 0, 450,
                           'images/ghost/clyde/clyde1.png', "Clyde")

    def blitme(self):
        if pygame.time.get_ticks() > self.settings.max_intro_lifetime:
            self.settings.mode = "Menu"
        if pygame.time.get_ticks() > .75 * self.settings.max_intro_lifetime:
            self.inkey.cardinal = "Stopped"
            self.pinky.cardinal = "Stopped"
            self.blinky.cardinal = "Stopped"
            self.clyde.cardinal = "Stopped"
        self.inkey.blitme(self.settings)
        self.pinky.blitme(self.settings)
        self.blinky.blitme(self.settings)
        self.clyde.blitme(self.settings)
Beispiel #2
0
class Game:
    BLACK = (0, 0, 0)

    def __init__(self):
        pygame.init()
        self.settings = Settings()
        self.screen = pygame.display.set_mode((self.settings.screen_height, self.settings.screen_width))
        pygame.display.set_caption("Pacman Portal")
        self.play_button = Button(self.settings, self.screen, "PLAY GAME", (self.settings.screen_width * 2.75) / 7,(self.settings.screen_height * 5.25) / 7)
        self.high_scores_button = Button(self.settings, self.screen, "HIGH SCORES", (self.settings.screen_width * 2.65) / 7, (self.settings.screen_height * 6) / 7)
        self.stats = Stats(self.settings)
        self.scoreboard = Scoreboard(self.settings, self.screen, self.stats)


        self.maze = Maze(self.settings, self.screen, mazefile="maze.txt", brickfile="square", portalfile="portal", shieldfile="shield", pointfile="powerpill")
        self.pacman_images = ["images/pacman_move_0.png", "images/pacman_move_1.png", "images/pacman_move_2.png"]
        self.pacman = Pacman(self.settings, self.screen, self.pacman_images[0])
        self.pacman_index = 0

        self.inkey_images = [
                                {
                                    "Up": [
                                        "blue_ghost_up_1",
                                        "blue_ghost_up_2"
                                    ]
                                },
                                {
                                    "Down": [
                                        "blue_ghost_down_1",
                                        "blue_ghost_down_2"
                                    ]
                                },
                                {
                                    "Left": [
                                        "blue_ghost_left_1",
                                        "blue_ghost_left_2"
                                    ]
                                },
                                {
                                    "Right": [
                                        "blue_ghost_right_1",
                                        "blue_ghost_right_2"
                                    ]
                                }
                            ]
        self.inkey = Ghost(self.settings, self.screen, self.inkey_images, self.maze.ghost_location[0])

        self.clyde_images = [
                                {
                                    "Up": [
                                        "yellow_ghost_up_1",
                                        "yellow_ghost_up_2"
                                    ]
                                },
                                {
                                    "Down": [
                                        "yellow_ghost_down_1",
                                        "yellow_ghost_down_2"
                                    ]
                                },
                                {
                                    "Left": [
                                        "yellow_ghost_left_1",
                                        "yellow_ghost_left_2"
                                    ]
                                },
                                {
                                    "Right": [
                                        "yellow_ghost_right_1",
                                        "yellow_ghost_right_2"
                                    ]
                                }
                            ]
        self.clyde = Ghost(self.settings, self.screen, self.clyde_images, self.maze.ghost_location[1])

        self.pinky_images = [
                                {
                                    "Up": [
                                        "pink_ghost_up_1",
                                        "pink_ghost_up_2"
                                    ]
                                },
                                {
                                    "Down": [
                                        "pink_ghost_down_1",
                                        "pink_ghost_down_2"
                                    ]
                                },
                                {
                                    "Left": [
                                        "pink_ghost_left_1",
                                        "pink_ghost_left_2"
                                    ]
                                },
                                {
                                    "Right": [
                                        "pink_ghost_right_1",
                                        "pink_ghost_right_2"
                                    ]
                                }
                            ]
        self.pinky = Ghost(self.settings, self.screen, self.pinky_images, self.maze.ghost_location[2])

        self.blinky_images = [
                                {
                                    "Up": [
                                        "red_ghost_up_1",
                                        "red_ghost_up_2"
                                    ]
                                },
                                {
                                    "Down": [
                                        "red_ghost_down_1",
                                        "red_ghost_down_2"
                                    ]
                                },
                                {
                                    "Left": [
                                        "red_ghost_left_1",
                                        "red_ghost_left_2"
                                    ]
                                },
                                {
                                    "Right": [
                                        "red_ghost_right_1",
                                        "red_ghost_right_2"
                                    ]
                                }
                            ]
        self.blinky = Ghost(self.settings, self.screen, self.blinky_images, self.maze.ghost_location[3])

        self.menu = Menu(self.settings, self.screen)

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

    def play(self):
        eloop = EventLoop(self.settings, self.pacman, finished=False)

        while not self.stats.game_active:
            if self.settings.high_score_screen:
                self.menu.high_scores()
            else:
                self.menu.start_menu(self.play_button, self.high_scores_button)
            self.check_events()
            pygame.display.update()

        while not eloop.finished:
            self.check_events()
            eloop.check_events()
            self.check_collision()
            self.update_screen()

    def update_screen(self):
        self.screen.fill(Game.BLACK)
        self.maze.blitme()
        self.pacman.update(self.pacman_images, self.pacman_index, self.pacman.direction)
        self.blit_characters()
        self.scoreboard.show_score()
        pygame.display.flip()

    def blit_characters(self):
        self.pacman.blitme()
        self.inkey.blitme()
        self.clyde.blitme()
        self.pinky.blitme()
        self.blinky.blitme()

    def check_events(self):
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
            elif event.type == pygame.KEYDOWN:
                self.check_keydown_event(event)
            elif event.type == pygame.KEYUP:
                self.check_keyup_events(event)
            elif event.type == pygame.MOUSEBUTTONDOWN:
                mouse_x, mouse_y = pygame.mouse.get_pos()
                self.check_play_button(self.settings, self.play_button, mouse_x, mouse_y)
                self.check_high_scores(self.settings, self.high_scores_button, mouse_x, mouse_y)

    def check_keydown_event(self, event):
        if event.key == pygame.K_SPACE:
            self.settings.high_score_screen = False
            self.menu.reset_animation()
        if event.key == pygame.K_q:
            sys.exit()
        if event.key == pygame.K_LEFT and self.pacman.move_left:
            self.pacman.x_dir = -1
            self.pacman.y_dir = 0
            self.pacman.direction = "Left"
        if event.key == pygame.K_RIGHT and self.pacman.move_right:
            self.pacman.x_dir = 1
            self.pacman.y_dir = 0
            self.pacman.direction = "Right"
        if event.key == pygame.K_UP and self.pacman.move_up:
            self.pacman.y_dir = -1
            self.pacman.x_dir = 0
            self.pacman.direction = "Up"
        if event.key == pygame.K_DOWN and self.pacman.move_down:
            self.pacman.y_dir = 1
            self.pacman.x_dir = 0
            self.pacman.direction = "Down"

    def check_keyup_events(self, event):
        if event.key == pygame.K_RIGHT or event.key == pygame.K_LEFT:
            self.pacman.x_dir = 0
        if event.key == pygame.K_UP or event.key == pygame.K_DOWN:
            self.pacman.y_dir = 0
        # self.pacman.direction = None

    def check_collision(self):
        self.check_dot_collisions()
        self.check_power_pill_collisions()
        # self.check_brick_collisions()

    def check_dot_collisions(self):
        for dot in self.maze.dots:
            if self.pacman.rect.colliderect(dot):
                self.maze.dots.remove(dot)
                self.stats.score += self.settings.dot_value

    def check_power_pill_collisions(self):
        for pill in self.maze.pills:
            if self.pacman.rect.colliderect(pill):
                self.maze.pills.remove(pill)
                self.stats.score += self.settings.power_pill_value

    # def check_brick_collisions(self):
    #     for brick in self.maze.bricks:
    #         if self.pacman.collider.collidepoint(brick.left[0], brick.left[1]):
    #             if self.pacman.collider.left <= brick.right and brick.bottom >= self.pacman.collider_y >= brick.top:
    #                 self.pacman.move_left = False
    #                 print("cant move left")
    #             else:
    #                 self.pacman.move_left = True
    #             if self.pacman.collider.right >= brick.left and brick.bottom >= self.pacman.collider_y >= brick.top:
    #                 self.pacman.move_right = False
    #                 print("cant move right")
    #             else:
    #                 self.pacman.move_right = True
    #             if self.pacman.collider.top <= brick.bottom and brick.left <= self.pacman.collider_x <= brick.right:
    #                 self.pacman.move_up = False
    #                 print("cant move up")
    #             else:
    #                 self.pacman.move_up = True
    #             if self.pacman.collider.bottom >= brick.top and brick.left <= self.pacman.collider_x <= brick.right:
    #                 self.pacman.move_down = False
    #                 print("cant move down")
    #             else:
    #                 self.pacman.move_down = True
    #             return

    def check_play_button(self, settings, play_button, mouse_x, mouse_y):
        """Start a new game when the player clicks Play."""
        button_clicked = play_button.rect.collidepoint(mouse_x, mouse_y)
        if button_clicked and not settings.game_active:
            # Reset the game settings.
            settings.initialize_dynamic_settings()

            # Hide the mouse cursor.
            pygame.mouse.set_visible(False)

            self.stats.game_active = True

    def check_high_scores(self, settings, high_scores, mouse_x, mouse_y):
        """Load the list of high scores."""
        button_clicked = high_scores.rect.collidepoint(mouse_x, mouse_y)
        if button_clicked:
            self.menu.high_scores()
            settings.high_score_screen = True
Beispiel #3
0
class Maze:
    BRICK_SIZE = 11

    def __init__(self, screen, mazefile, stats, clock):
        self.screen = screen
        self.stats = stats
        self.clock = clock
        self.radio = pygame.mixer
        self.radio.init()
        self.filename = mazefile
        with open(self.filename, 'r') as f:
            self.rows = f.readlines()

        self.maze_obj = []
        size = Maze.BRICK_SIZE
        self.brick = Image(screen, 'brick', size, size)

        self.default_ports = pygame.sprite.Group()
        self.def_port_list = []
        self.lport = Portal(self.screen, 'blue')
        self.rport = Portal(self.screen, 'orange')

        self.pac_dots = pygame.sprite.Group()
        self.pac_dot = Pacdot(screen, pygame.image.load('images/pacdot.png'))
        self.pac_dot.image = pygame.transform.scale(self.pac_dot.image, (8, 8))

        self.power_dots = pygame.sprite.Group()
        self.power = Pacdot(screen, pygame.image.load('images/pacdot.png'))
        self.power.image = pygame.transform.scale(self.power.image, (15, 15))

        self.shield = Image(screen, 'shield', size, size)
        self.pac_man = Pacman(screen, self)
        self.blinky = Ghost(screen, self, 'blinky')
        self.pinky = Ghost(screen, self, 'pinky')
        self.inky = Ghost(screen, self, 'inky')
        self.clyde = Ghost(screen, self, 'clyde')
        self.ghosts = pygame.sprite.Group()
        self.fruit = None
        self.fruits = pygame.sprite.Group()

        self.deltax = self.deltay = Maze.BRICK_SIZE

        # used for menu purposes
        self.title = None
        self.title_rect = None
        self.start_button = None
        self.scores_button = None
        # end of menu items

        # used for sounds
        self.intro = self.radio.Sound('sounds/intro.wav')
        self.ambient = self.radio.Sound('sounds/ambient.wav')
        self.eatdot = self.radio.Sound('sounds/eatdot.wav')
        self.eatghost = self.radio.Sound('sounds/eatghost.wav')
        self.powersound = self.radio.Sound('sounds/power.wav')
        self.death = self.radio.Sound('sounds/death.wav')
        self.gameover = self.radio.Sound('sounds/gameover.wav')

        self.ambient_playing = False
        self.power_playing = False
        # end of sounds

        self.lives_counter = pygame.sprite.Group()
        self.lives_sprites = []

        self.active_ports = pygame.sprite.Group()
        self.bport = None
        self.oport = None
        self.blue_active = False
        self.orange_active = False

        self.build()

    def build(self):
        w, h = Maze.BRICK_SIZE, Maze.BRICK_SIZE
        dx, dy = self.deltax, self.deltay
        self.fruit = Fruit(self.screen, self.stats.current_level % 9)
        self.fruits.add(self.fruit)
        for i in range(0, self.stats.lives):
            temp = Pacman(self.screen, self)
            temp.rect.y = 650
            temp.rect.x = i * 30
            self.lives_counter.add(temp)
            self.lives_sprites.append(temp)

        for nrow in range(len(self.rows)):
            row = self.rows[nrow]
            for ncol in range(len(row)):
                col = row[ncol]
                current_rect = pygame.Rect(ncol * dx, nrow * dy, w, h)
                if col == 'X':
                    self.maze_obj.append(('brick', current_rect))
                elif col == '.':
                    temp = self.pac_dot.spawn()
                    temp.rect = current_rect
                    self.maze_obj.append(('pacdot', temp))
                    self.pac_dots.add(temp)
                elif col == 'M':
                    self.pac_man.rect.x = current_rect.x - 11
                    self.pac_man.rect.y = current_rect.y - 11
                    self.maze_obj.append(('pacman', self.pac_man.rect))
                elif col == 'P':
                    temp = self.power.spawn()
                    temp.rect = current_rect
                    temp.rect.x -= 3
                    temp.rect.y -= 3
                    self.maze_obj.append(('power', current_rect))
                    self.power_dots.add(temp)
                elif col == 'S':
                    self.maze_obj.append(('shield', current_rect))
                elif col == '1':
                    self.blinky.rect.x = current_rect.x - 10
                    self.blinky.rect.y = current_rect.y - 10
                    self.maze_obj.append(('blinky', self.blinky.rect))
                    self.ghosts.add(self.blinky)
                elif col == '2':
                    self.pinky.rect.x = current_rect.x - 10
                    self.pinky.rect.y = current_rect.y - 10
                    self.maze_obj.append(('pinky', self.pinky.rect))
                    self.ghosts.add(self.pinky)
                elif col == '3':
                    self.inky.rect.x = current_rect.x - 10
                    self.inky.rect.y = current_rect.y - 10
                    self.maze_obj.append(('inky', self.inky.rect))
                    self.ghosts.add(self.inky)
                elif col == '4':
                    self.clyde.rect.x = current_rect.x - 10
                    self.clyde.rect.y = current_rect.y - 10
                    self.maze_obj.append(('clyde', self.clyde.rect))
                    self.ghosts.add(self.clyde)
                elif col == 'B':
                    self.lport.rect = current_rect
                    self.maze_obj.append(('port1', self.lport.rect))
                    self.default_ports.add(self.lport)
                    self.def_port_list.append(self.lport)
                elif col == 'O':
                    self.rport.rect = current_rect
                    self.maze_obj.append(('port2', self.rport.rect))
                    self.default_ports.add(self.rport)
                    self.def_port_list.append(self.rport)

    def blitme(self):
        self.pac_dots.draw(self.screen)
        self.power_dots.draw(self.screen)
        self.default_ports.draw(self.screen)
        for obj in self.maze_obj:
            if obj[0] == 'brick':
                self.screen.blit(self.brick.image, obj[1])
            elif obj[0] == 'shield':
                self.screen.blit(self.shield.image, obj[1])
        self.stats.blitme()

    @staticmethod
    def check_events(self):
        keys = pygame.key.get_pressed()
        if keys[pygame.K_UP] and self.top_is_clear(self.pac_man):
            self.pac_man.moveup()
        if keys[pygame.K_DOWN] and self.bot_is_clear(self.pac_man):
            self.pac_man.movedown()
        if keys[pygame.K_LEFT] and self.left_is_clear(self.pac_man):
            self.pac_man.moveleft()
        if keys[pygame.K_RIGHT] and self.right_is_clear(self.pac_man):
            self.pac_man.moveright()

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                self.stats.new_high_score(self.stats.current_score)
                sys.exit()
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_q:
                    self.fire_blue_portal()
                if event.key == pygame.K_e:
                    self.fire_orange_portal()
                if event.key == pygame.K_SPACE:
                    self.active_ports.empty()
                    self.blue_active = False
                    self.orange_active = False

    def update(self):
        self.check_events(self)
        self.screen.fill((0, 0, 0))
        self.blitme()
        self.dot_eating()
        self.pac_portal_collision()
        self.pac_active_portal_collision()
        self.pac_ghost_collision()
        self.ghost_active_portal_collision()
        if self.stats.game_status == 0:
            return
        if self.pac_man.ghost_pt_mult == 5:
            self.fruit.show_self()
        if not self.pac_man.dying:
            self.pac_man.move()
            self.ghosts.update()
            if self.pac_man.powered_up and not self.power_playing:
                self.ambient.stop()
                self.ambient_playing = False
                self.powersound.play(-1)
                self.power_playing = True
            elif not self.pac_man.powered_up and self.power_playing:
                self.powersound.stop()
                self.power_playing = False
            elif not self.pac_man.powered_up and not self.ambient_playing:
                self.ambient.play(-1)
                self.ambient_playing = True
        else:
            self.ambient.stop()
            self.ambient_playing = False
        self.fruit.set_invisible()
        if self.stats.game_status == 1:
            self.fruits.draw(self.screen)
            self.active_ports.draw(self.screen)
            self.pac_man.blitme()
            self.blinky.blitme()
            self.pinky.blitme()
            self.inky.blitme()
            self.clyde.blitme()
            self.lives_counter.draw(self.screen)

    def dot_eating(self):
        dot_collisions = pygame.sprite.spritecollideany(
            self.pac_man, self.pac_dots)
        if dot_collisions:
            dot_collisions.kill()
            self.stats.current_score += 10
            self.eatdot.play()
        dot_collisions = pygame.sprite.spritecollideany(
            self.pac_man, self.power_dots)
        if dot_collisions:
            dot_collisions.kill()
            self.stats.current_score += 50
            self.pac_man.powered_up = True
            for ghost in self.ghosts:
                ghost.scared = True
        if len(self.pac_dots) == 0 and len(self.power_dots) == 0:
            pygame.time.wait(1000)
            self.new_level()
        fruit_collision = pygame.sprite.spritecollideany(
            self.pac_man, self.fruits)
        if fruit_collision:
            self.stats.current_score += 1000
            self.eatghost.play()
            fruit_collision.kill()
            fruit_collision.visible = False

    def pac_ghost_collision(self):
        collisions = pygame.sprite.spritecollideany(self.pac_man, self.ghosts)
        if collisions and not self.pac_man.powered_up:
            for ghost in self.ghosts:
                ghost.return_to_center()
            if self.stats.lives > 0:
                self.pac_man.die()
                self.stats.lives -= 1
                self.lives_sprites[self.stats.lives].kill()
                self.death.play()
            else:
                self.stats.new_high_score(self.stats.current_score)
                self.reset_game()
                self.stats.game_status = 0
        elif collisions and self.pac_man.powered_up:
            collisions.return_to_center()
            self.stats.current_score += (2 ^ self.pac_man.ghost_pt_mult) * 100
            self.pac_man.ghost_pt_mult += 1
            self.eatghost.play()

    def pac_portal_collision(self):
        collision = pygame.sprite.spritecollideany(self.pac_man,
                                                   self.default_ports)
        if self.pac_man.is_teleporting >= 100:
            self.pac_man.is_teleporting = 0
        if collision and self.pac_man.is_teleporting == 0:
            self.pac_man.is_teleporting += 1
            if collision.color == 0:
                self.pac_man.rect.x = self.def_port_list[1].rect.x
            else:
                self.pac_man.rect.x = self.def_port_list[0].rect.x
        elif self.pac_man.is_teleporting != 0:
            self.pac_man.is_teleporting += 1

    def pac_active_portal_collision(self):
        collision = pygame.sprite.spritecollideany(self.pac_man,
                                                   self.active_ports)
        if self.pac_man.is_teleporting >= 100:
            self.pac_man.is_teleporting = 0
        if collision and self.pac_man.is_teleporting == 0 and self.blue_active and self.orange_active:
            self.pac_man.is_teleporting += 1
            if collision.color == 0:
                self.pac_man.rect.centerx = self.oport.rect.centerx
                self.pac_man.rect.centery = self.oport.rect.centery
            else:
                self.pac_man.rect.centerx = self.bport.rect.centerx
                self.pac_man.rect.centery = self.bport.rect.centery
        elif self.pac_man.is_teleporting != 0:
            self.pac_man.is_teleporting += 1

    def ghost_active_portal_collision(self):
        collision = pygame.sprite.groupcollide(self.ghosts, self.active_ports,
                                               False, False)
        if collision:
            for ghost in collision:
                if ghost.is_teleporting == 0 and self.blue_active and self.orange_active \
                        and self.stats.current_level > 9:
                    ghost.is_teleporting += 1
                    if collision[ghost][0].color == 0:
                        ghost.rect.centerx = self.oport.rect.centerx
                        ghost.rect.centery = self.oport.rect.centery
                    else:
                        ghost.rect.centerx = self.bport.rect.centerx
                        ghost.rect.centery = self.bport.rect.centery

    def fire_blue_portal(self):
        if not self.blue_active:
            port_loc = self.pac_man.find_portal_spot()
            self.bport = Portal(self.screen, 'blue')
            self.bport.rect.centerx = port_loc[0]
            self.bport.rect.centery = port_loc[1]
            self.active_ports.add(self.bport)
            self.blue_active = True

    def fire_orange_portal(self):
        if not self.orange_active:
            port_loc = self.pac_man.find_portal_spot()
            self.oport = Portal(self.screen, 'orange')
            self.oport.rect.centerx = port_loc[0]
            self.oport.rect.centery = port_loc[1]
            self.active_ports.add(self.oport)
            self.orange_active = True

    # returns x y position for retrieval of object from maze obj 2d array
    # real=true for top left xy, real=false for bot right
    @staticmethod
    def get_obj_pos(obj):
        x = int(obj.rect.x / Maze.BRICK_SIZE)
        y = int(obj.rect.y / Maze.BRICK_SIZE)
        return x, y

    # checks for possible collision, direction is 1 up, 2 right, 3 down, 4 left
    def front_path_is_clear(self, direction, obj):
        obj_pos = self.get_obj_pos(obj)
        if type(obj) is Pacman:
            if direction == 1:
                return self.rows[obj_pos[1]][
                    obj_pos[0] +
                    1] != 'X' and self.rows[obj_pos[1]][obj_pos[0] + 1] != 'S'
            if direction == 2:
                return self.rows[obj_pos[1] + 1][obj_pos[0] + 3] != 'X' \
                       and self.rows[obj_pos[1] + 1][obj_pos[0] + 3] != 'S'
            if direction == 3:
                return self.rows[obj_pos[1] + 3][obj_pos[0] + 1] != 'X' \
                       and self.rows[obj_pos[1] + 3][obj_pos[0] + 1] != 'S'
            if direction == 4:
                return self.rows[obj_pos[1] + 1][
                    obj_pos[0]] != 'X' and self.rows[obj_pos[1] +
                                                     1][obj_pos[0]] != 'S'
        else:
            if direction == 1:
                return self.rows[obj_pos[1]][obj_pos[0] + 1] != 'X'
            if direction == 2:
                return self.rows[obj_pos[1] + 1][obj_pos[0] + 3] != 'X'
            if direction == 3:
                return self.rows[obj_pos[1] + 3][obj_pos[0] + 1] != 'X'
            if direction == 4:
                return self.rows[obj_pos[1] + 1][obj_pos[0]] != 'X'

    def left_is_clear(self, obj):
        flag = True
        for i in range(2, 8):
            if self.screen.get_at(
                (obj.rect.x - 2, obj.rect.y + i)) == (36, 28, 252):
                flag = False
        for i in range(25, 32):
            if self.screen.get_at(
                (obj.rect.x - 2, obj.rect.y + i)) == (36, 28, 252):
                flag = False
        return flag

    def right_is_clear(self, obj):
        flag = True
        for i in range(2, 8):
            if self.screen.get_at(
                (obj.rect.x + 35, obj.rect.y + i)) == (36, 28, 252):
                flag = False
        for i in range(25, 32):
            if self.screen.get_at(
                (obj.rect.x + 35, obj.rect.y + i)) == (36, 28, 252):
                flag = False
        return flag

    def bot_is_clear(self, obj):
        flag = True
        for i in range(2, 8):
            if self.screen.get_at(
                (obj.rect.x + i, obj.rect.y + 35)) == (36, 28, 252):
                flag = False
        for i in range(25, 32):
            if self.screen.get_at(
                (obj.rect.x + i, obj.rect.y + 35)) == (36, 28, 252):
                flag = False
        return flag

    def top_is_clear(self, obj):
        flag = True
        for i in range(2, 8):
            if self.screen.get_at(
                (obj.rect.x + i, obj.rect.y - 3)) == (36, 28, 252):
                flag = False
        for i in range(25, 32):
            if self.screen.get_at(
                (obj.rect.x + i, obj.rect.y - 3)) == (36, 28, 252):
                flag = False
        return flag

    def new_level(self):
        self.maze_obj = []
        self.pac_dots.empty()
        self.power_dots.empty()
        self.active_ports.empty()
        self.bport = None
        self.oport = None
        self.blue_active = False
        self.orange_active = False
        self.default_ports.empty()
        self.def_port_list = []
        for ghost in self.ghosts:
            ghost.return_to_center()
            ghost.scared_timer = 0
            ghost.scared = False
        self.ghosts.empty()
        self.pac_man.power_timer = 0
        self.pac_man.powered_up = False
        self.stats.game_speed += 2
        self.stats.current_level += 1
        self.ambient.stop()
        self.ambient_playing = False
        self.powersound.stop()
        self.power_playing = False
        self.lives_sprites = []
        self.lives_counter.empty()
        self.fruits.empty()
        self.build()
        pygame.time.wait(700)

    def reset_game(self):
        self.maze_obj = []
        self.lives_sprites = []
        self.lives_counter.empty()
        self.pac_dots.empty()
        self.power_dots.empty()
        self.active_ports.empty()
        self.bport = None
        self.oport = None
        self.blue_active = False
        self.orange_active = False
        self.default_ports.empty()
        self.def_port_list = []
        for ghost in self.ghosts:
            ghost.return_to_center()
        self.ghosts.empty()
        self.stats.game_speed = 60
        self.stats.current_level = 0
        self.stats.lives = 3
        self.stats.current_score = 0
        self.radio.stop()
        self.ambient_playing = False
        self.power_playing = False
        self.fruits.empty()
        self.gameover.play()
        self.build()
        self.screen.fill((0, 0, 0))
        pygame.time.wait(700)

    def show_menu(self, anim):
        self.screen.fill((0, 0, 0))
        self.check_menu_events()
        anim.update()
        self.screen.blit(self.title, self.title_rect)
        self.start_button.draw_button()
        self.scores_button.draw_button()
        anim.blitme()

    def create_menu(self):
        self.title = pygame.image.load('images/title.png')
        self.title = pygame.transform.scale(self.title, (750, 750))
        self.title_rect = self.title.get_rect()
        self.title_rect.x = 80
        self.title_rect.y = 100

        self.start_button = Button(self.screen, "PLAY GAME", 1)
        self.scores_button = Button(self.screen, "HIGH SCORES", 2)
        return Animation(self.screen, self)

    def check_menu_events(self):
        m_x, m_y = pygame.mouse.get_pos()
        if self.start_button.rect.collidepoint(m_x, m_y):
            self.start_button.lightup()
        else:
            self.start_button.lightdown()

        if self.scores_button.rect.collidepoint(m_x, m_y):
            self.scores_button.lightup()
        else:
            self.scores_button.lightdown()

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()

            elif event.type == pygame.MOUSEBUTTONDOWN:
                m_x, m_y = pygame.mouse.get_pos()
                if self.start_button.rect.collidepoint(m_x, m_y):
                    self.stats.game_status = 1
                    self.intro.play()
                    time.sleep(3.5)
                elif self.scores_button.rect.collidepoint(m_x, m_y):
                    self.stats.show_score_list()
Beispiel #4
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()
Beispiel #5
0
class Game:
    def __init__(self):
        pygame.init()
        self.screen_width = 599
        self.screen_height = 800
        self.screen = pygame.display.set_mode(
            (self.screen_width, self.screen_height))
        pygame.display.set_caption('Pacman')
        self.clock = pygame.time.Clock()

        self.WHITE = (255, 255, 255)
        self.BLACK = (0, 0, 0)
        self.hovered = False

        # self.blinky = Ghost(self.screen, self.maze, 'blinky')
        #self.ghosts = Group()

        # Give files needed to populate the maze
        self.expandfile = ExpandFile('test.txt', expandBy=2)
        self.maze = Maze(self.screen, 'test.txt', 'images/wall',
                         'images/foodPellet', 'images/portal_1',
                         'images/portal_2')

        self.player = Pacman(self.screen, self.maze, self.maze.pacmanposition)
        self.blinky = Ghost(self.screen, 'blinky', self.maze,
                            self.maze.blinky_position, self.player)
        self.clyde = Ghost(self.screen, 'clyde', self.maze,
                           self.maze.clyde_position, self.player)
        self.inkey = Ghost(self.screen, 'inkey', self.maze,
                           self.maze.inkey_position, self.player)
        self.pinky = Ghost(self.screen, 'pinky', self.maze,
                           self.maze.pinky_position, self.player)

        self.intro_music = pygame.mixer.Sound("sounds/intro.wav")

        self.intro_logo = pygame.image.load('images/pacmanLogo.png')

    def get_color(self):
        if not self.hovered:
            return (255, 255, 255)
        else:
            return (100, 100, 100)

    def game_intro(self):
        pygame.draw.rect(self.screen, (75, 80, 33),
                         (0, 0, self.screen_width, self.screen_height))
        font = pygame.font.SysFont(None, 50)
        self.screen_text = font.render("Play", True, self.get_color())
        self.text_rect = self.screen_text.get_rect()

        self.screen.blit(self.screen_text, (270, 400))
        self.screen.blit(self.intro_logo, (110, 100))
        intro = True

        while intro:

            for event in pygame.event.get():
                print(event)
                if event.type == pygame.QUIT:
                    pygame.quit()
                    quit()
                elif event.type == pygame.MOUSEBUTTONDOWN:
                    pygame.mixer.Sound.play(self.intro_music)
                    self.play()

            pygame.display.update()

    def play(self):
        eventloop = EventLoop(finished=False)

        while not eventloop.finished:
            self.clock.tick(30)
            eventloop.check_events(self.player)
            self.player.update()
            self.update_screen()

    def update_screen(self):
        self.screen.fill(self.BLACK)
        self.maze.blitme()
        self.player.blitme()
        self.blinky.blitme()
        self.clyde.blitme()
        self.inkey.blitme()
        self.pinky.blitme()
        # Draw scoreboard
        self.player.points_gathered()

        pygame.display.flip()
Beispiel #6
0
class Game:
    BLACK = (0, 0, 0)

    def __init__(self):
        pygame.init()
        pygame.mixer.init()
        self.screen = pygame.display.set_mode((280, 360))
        self.screen_rect = self.screen.get_rect()
        self.font = pygame.font.Font(None, 50)
        self.font2 = pygame.font.Font(None, 25)
        self.won = False
        self.lost = False
        pygame.display.set_caption("Pacman Portal")
        self.clock = pygame.time.Clock()
        self.maze = Maze(self.screen, 'mazefile1.txt')
        self.p_man = PacMan(self.screen, 25, 13, self.maze)
        self.pinky = Ghost('p', self.p_man, self.screen, self.maze, 16, 14)
        self.blinky = Ghost('b', self.p_man, self.screen, self.maze, 13, 14)
        self.inky = Ghost('i', self.p_man, self.screen, self.maze, 16, 12,
                          self.blinky)
        self.clyde = Ghost('c', self.p_man, self.screen, self.maze, 16, 16)
        self.ghosts = Group()
        self.ghosts.add(self.blinky)
        self.ghosts.add(self.pinky)
        self.ghosts.add(self.inky)
        self.ghosts.add(self.clyde)
        self.stats = GameStats()
        self.pb = Button(self.screen, "Play", 150)
        self.sb = Scoreboard(self.screen, self.stats)
        self.menu_timer = pygame.time.get_ticks()

    def update_screen(self):

        self.screen.fill(Game.BLACK)
        if self.stats.game_active:

            self.maze.blitme()
            if not self.p_man.dead:
                self.blinky.update_target()
                self.pinky.update_target()
                self.inky.update_target()
                self.clyde.update_target()
                self.ghosts.update(True)
                self.pinky.blitme()
                self.inky.blitme()
                self.blinky.blitme()
                self.clyde.blitme()

            self.p_man.update()
            self.p_man.blitme()
            self.sb.prep_score()
            self.sb.prep_high_score()
            self.sb.show_score()

        elif self.won:
            surface = self.font.render("YOU WIN!", True, (255, 255, 255))
            text_rect = surface.get_rect(center=(self.screen_rect.centerx,
                                                 self.screen_rect.centerx))
            self.screen.blit(surface, text_rect)
            surface = self.font2.render("YOUR SCORE:" + str(self.stats.score),
                                        True, (255, 255, 255))
            text_rect = surface.get_rect(center=(self.screen_rect.centerx,
                                                 self.screen_rect.centerx +
                                                 50))
            self.screen.blit(surface, text_rect)
            surface = self.font2.render(
                "HIGH SCORE:" + str(self.stats.high_score), True,
                (255, 255, 255))
            text_rect = surface.get_rect(center=(self.screen_rect.centerx,
                                                 self.screen_rect.centerx +
                                                 100))
            self.screen.blit(surface, text_rect)
        elif self.lost:
            surface = self.font.render("GAME OVER", True, (255, 255, 255))
            text_rect = surface.get_rect(center=(self.screen_rect.centerx,
                                                 self.screen_rect.centerx))
            self.screen.blit(surface, text_rect)
            surface = self.font2.render("YOUR SCORE:" + str(self.stats.score),
                                        True, (255, 255, 255))
            text_rect = surface.get_rect(center=(self.screen_rect.centerx,
                                                 self.screen_rect.centerx +
                                                 50))
            self.screen.blit(surface, text_rect)
            surface = self.font2.render(
                "HIGH SCORE:" + str(self.stats.high_score), True,
                (255, 255, 255))
            text_rect = surface.get_rect(center=(self.screen_rect.centerx,
                                                 self.screen_rect.centerx +
                                                 100))
            self.screen.blit(surface, text_rect)
        else:

            surface = self.font.render("PacMan", True, (255, 255, 255))
            text_rect = surface.get_rect(center=(self.screen_rect.centerx,
                                                 self.screen_rect.top + 25))
            self.screen.blit(surface, text_rect)
            self.pb.draw_button()
            if pygame.time.get_ticks() - self.menu_timer >= 500:
                surface = self.font2.render('"BLINKY"', True, (255, 255, 255))
                text_rect = surface.get_rect(
                    center=(self.screen_rect.centerx + 20,
                            self.screen_rect.top + 75))
                self.screen.blit(surface, text_rect)
                b_image = self.blinky.im.image
                b_rect = b_image.get_rect()
                b_rect.centery = self.screen_rect.top + 75
                b_rect.centerx = self.screen_rect.centerx - 50
                self.screen.blit(b_image, b_rect)
            if pygame.time.get_ticks() - self.menu_timer >= 1500:
                surface = self.font2.render('"PINKY"', True, (255, 255, 255))
                text_rect = surface.get_rect(
                    center=(self.screen_rect.centerx + 20,
                            self.screen_rect.top + 100))
                self.screen.blit(surface, text_rect)
                p_image = self.pinky.im.image
                p_rect = p_image.get_rect()
                p_rect.centery = self.screen_rect.top + 100
                p_rect.centerx = self.screen_rect.centerx - 50
                self.screen.blit(p_image, p_rect)
            if pygame.time.get_ticks() - self.menu_timer >= 2500:
                surface = self.font2.render('"INKY"', True, (255, 255, 255))
                text_rect = surface.get_rect(
                    center=(self.screen_rect.centerx + 20,
                            self.screen_rect.top + 125))
                self.screen.blit(surface, text_rect)
                i_image = self.inky.im.image
                i_rect = i_image.get_rect()
                i_rect.centery = self.screen_rect.top + 125
                i_rect.centerx = self.screen_rect.centerx - 50
                self.screen.blit(i_image, i_rect)
            if pygame.time.get_ticks() - self.menu_timer >= 3500:
                surface = self.font2.render('"CLYDE"', True, (255, 255, 255))
                text_rect = surface.get_rect(
                    center=(self.screen_rect.centerx + 20,
                            self.screen_rect.top + 150))
                self.screen.blit(surface, text_rect)
                c_image = self.clyde.im.image
                c_rect = c_image.get_rect()
                c_rect.centery = self.screen_rect.top + 150
                c_rect.centerx = self.screen_rect.centerx - 50
                self.screen.blit(c_image, c_rect)
        pygame.display.flip()

    def play(self):
        loop = EventLoop(self.p_man, self.maze, self.ghosts, self.stats,
                         self.pb)

        while not loop.finished:
            loop.check_events()
            if self.stats.game_active:
                self.pinky.start(loop.start_time)
                self.inky.start(loop.start_time)
                self.blinky.start(loop.start_time)
                self.clyde.start(loop.start_time)
                loop.check_collisions()
            self.update_screen()
            self.clock.tick(60)
            self.lost = self.p_man.finished
            self.won = loop.won
            if self.won:
                self.stats.game_active = False
            elif self.lost:
                self.stats.game_active = False
Beispiel #7
0
class Game:
    BLACK = (0, 0, 0)

    def __init__(self):
        pygame.init()
        self.settings = ai_settings = Settings()
        self.screen = pygame.display.set_mode(
            (ai_settings.screen_width, ai_settings.screen_height))
        pygame.display.set_caption('Pacman Portal')
        self.play_button = Button(ai_settings=ai_settings,
                                  screen=self.screen,
                                  msg='Play Game')

        self.stats = GameStats(settings=ai_settings)
        self.sb = Scoreboard(settings=ai_settings,
                             screen=self.screen,
                             stats=self.stats)

        # self.expandfile = ExpandFile('images/pacman_maze.txt', expandby=4)
        self.maze = Maze(self.screen,
                         mazefile='images/pacman_maze.txt',
                         brickfile='brick',
                         blueportalfile='bluePortal',
                         orangeportalfile='orangePortal',
                         shieldfile='shield',
                         pointfile='point',
                         powerfile='power',
                         cherryfile='cherry')

        self.blueGhost = Ghost(screen=self.screen,
                               settings=ai_settings,
                               ghost_type=1)
        self.redGhost = Ghost(screen=self.screen,
                              settings=ai_settings,
                              ghost_type=2)
        self.orangeGhost = Ghost(screen=self.screen,
                                 settings=ai_settings,
                                 ghost_type=3)
        self.pinkGhost = Ghost(screen=self.screen,
                               settings=ai_settings,
                               ghost_type=4)
        self.player = Player(screen=self.screen,
                             settings=ai_settings,
                             stats=self.stats,
                             sb=self.sb,
                             inky=self.blueGhost,
                             blinky=self.redGhost,
                             clyde=self.orangeGhost,
                             pinky=self.pinkGhost)
        self.game_over_screen = GameOver(screen=self.screen,
                                         settings=self.settings)

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

    def play(self):
        eloop = EventLoop(finished=True, settings=self.settings)
        self.reset_game()
        while True:
            while eloop.finished:
                eloop.check_play_button(self.stats, self.sb, self.play_button)
                if not self.stats.game_active:
                    self.play_button.draw_button()
                pygame.display.flip()
            while self.settings.begin.get_busy():
                eloop.check_events(self.stats, self.player)
                self.settings.begin.get_busy()
            while not eloop.finished:
                eloop.check_events(self.stats, self.player)
                self.update_screen()
                self.player_ghost_update()
                if self.stats.lives_left == 0:
                    self.reset_game()
                    while self.game_over_screen.counter != 2:
                        eloop.check_events(self.stats, self.player)
                        self.game_over_screen.blit_me()
                        pygame.display.flip()
                    eloop.finished = True

    def update_screen(self):
        self.screen.fill(self.BLACK)
        self.maze.blitme()
        self.player.blitme()
        self.blueGhost.blitme()
        self.redGhost.blitme()
        self.orangeGhost.blitme()
        self.pinkGhost.blitme()
        self.sb.show_score()
        pygame.display.flip()

    def player_ghost_update(self):
        self.player.update(self.maze)
        self.blueGhost.update(self.maze)
        self.redGhost.update(self.maze)
        self.orangeGhost.update(self.maze)
        self.pinkGhost.update(self.maze)

    def reset_game(self):
        pygame.mixer.stop()
        self.blueGhost.reset_ghost()
        self.redGhost.reset_ghost()
        self.orangeGhost.reset_ghost()
        self.pinkGhost.reset_ghost()
        self.player.reset_player()
        self.stats.reset_stats()
        self.settings.reset_settings()
        self.maze.build()
        pygame.mouse.set_visible(True)
        self.stats.game_active = False