示例#1
0
def Game():
    pygame.init()

    gamesettings = Settings()
    screen = pygame.display.set_mode(
        (gamesettings.screen_width, gamesettings.screen_height))
    pygame.display.set_caption("Pacman Portal")

    # Start screen
    startScreen = StartScreen(screen, gamesettings)
    showgamestats = GameStats(screen, gamesettings)

    # Grouping blocks and pellets
    blocks = Group()
    powerpills = Group()
    shield = Group()
    portal = Group()

    thepacman = Pacman(screen, gamesettings)

    # Making the ghosts
    redghost = Ghosts(screen, "red")
    cyanghost = Ghosts(screen, "cyan")
    orangeghost = Ghosts(screen, "orange")
    pinkghost = Ghosts(screen, "pink")

    startScreen.makeScreen(screen)
    gf.readFile(screen, blocks, shield, powerpills, portal)

    screen.fill(BLACK)
    while True:
        screen.fill(BLACK)
        showgamestats.blitstats()
        gf.check_events(thepacman)
        gf.check_collision(thepacman, blocks, powerpills, shield)
        thepacman.update()
        for block in blocks:
            block.blitblocks()
        for theshield in shield:
            theshield.blitshield()
        for pill in powerpills:
            pill.blitpowerpills()
        for theportal in portal:
            theportal.blitportal()
        thepacman.blitpacman()
        redghost.blitghosts()
        cyanghost.blitghosts()
        orangeghost.blitghosts()
        pinkghost.blitghosts()

        pygame.display.flip()
示例#2
0
def Game():
    pygame.init()

    settings = Settings()
    screen = pygame.display.set_mode((settings.screen_width, settings.screen_height))
    pygame.display.set_caption("Pacman Portal")

    startScreen = StartScreen(screen, settings)
    showgamestats = GameStats(screen, settings)

    bricks = Group()
    powerpellets = Group()
    pellets = Group()
    shield = Group()
    fruits = Group()
    portal = Group()

    thepacman = Pacman(screen, settings)

    blinky = Ghosts(screen, "red")
    inky = Ghosts(screen, "cyan")
    clyde = Ghosts(screen, "orange")
    pinky = Ghosts(screen, "pink")

    startScreen.makeScreen(screen)
    gf.readFile(screen, bricks, shield, pellets, powerpellets)

    screen.fill(BLACK)
    while True:
        screen.fill(BLACK)
        showgamestats.blitstats()
        gf.check_events(thepacman)
        gf.check_collision(thepacman, bricks, pellets, powerpellets, shield, fruits)
        thepacman.update()
        for brick in bricks:
            brick.blitbricks()
        for theshield in shield:
            theshield.blitshield()
        for power in powerpellets:
            power.blitpowerpills()
        for pellet in pellets:
            pellet.blitpellet()
        thepacman.blitpacman()
        blinky.blitghosts()
        inky.blitghosts()
        clyde.blitghosts()
        pinky.blitghosts()

        pygame.display.flip()
示例#3
0
def Game():
    pygame.init()

    gamesettings = Settings()
    screen = pygame.display.set_mode(
        (gamesettings.screen_width, gamesettings.screen_height))
    pygame.display.set_caption("Pacman Portal")

    # Start screen
    showgamestats = GameStats(screen, gamesettings)
    startScreen = StartScreen(screen, gamesettings, showgamestats)

    # Grouping blocks and pellets and ghosts
    blocks = Group()
    powerpills = Group()
    shield = Group()
    portals = Group()
    ghosts = Group()
    intersections = Group()
    fruit = Fruit(screen)

    thepacman = Pacman(screen, gamesettings)

    # Making the ghosts
    redghost = Ghosts(screen, "red")
    cyanghost = Ghosts(screen, "cyan")
    orangeghost = Ghosts(screen, "orange")
    pinkghost = Ghosts(screen, "pink")

    ghosts.add(redghost)
    ghosts.add(cyanghost)
    ghosts.add(orangeghost)
    ghosts.add(pinkghost)

    # Making the two portals
    orange = Portal(screen, "orange")
    blue = Portal(screen, "blue")

    portals.add(orange)
    portals.add(blue)

    startScreen.makeScreen(screen, gamesettings)
    fruit.fruitReset()
    gf.readFile(screen, blocks, shield, powerpills, intersections)

    frames = 0  # for the victory fanfare and death animation

    # play intro chime
    playIntro = True

    screen.fill(BLACK)
    while True:
        if (gamesettings.game_active):
            pygame.time.Clock().tick(120)  #120 fps lock
            screen.fill(BLACK)
            showgamestats.blitstats()
            gf.check_events(thepacman, powerpills, gamesettings, orange, blue)
            gf.check_collision(thepacman, blocks, powerpills, shield, ghosts,
                               intersections, showgamestats, gamesettings,
                               fruit, orange, blue)
            for block in blocks:
                block.blitblocks()
            for theshield in shield:
                theshield.blitshield()
            for pill in powerpills:
                pill.blitpowerpills()
            for portal in portals:
                portal.blitportal()
            for ghost in ghosts:
                ghost.blitghosts()
                ghost.update()
                if (ghost.DEAD):
                    ghost.playRetreatSound()
                elif (ghost.afraid):
                    ghost.playAfraidSound(
                    )  # if ghosts are afraid, loop their sound
            for intersection in intersections:
                intersection.blit()
            fruit.blitfruit()
            thepacman.blitpacman()
            thepacman.update()

            if (len(powerpills) == 0):
                gamesettings.game_active = False
                gamesettings.victory_fanfare = True
            if (playIntro and pygame.time.get_ticks() % 200 <= 50):
                mixer.Channel(2).play(
                    pygame.mixer.Sound('sounds/pacman_beginning.wav'))
                pygame.time.wait(4500)
                playIntro = False
        elif (gamesettings.victory_fanfare):
            if (frames <= 120):
                for block in blocks:
                    block.color = ((255, 255, 255))
                    block.blitblocks()
            elif (frames <= 240):
                for block in blocks:
                    block.color = ((0, 0, 255))
                    block.blitblocks()
            elif (frames <= 360):
                for block in blocks:
                    block.color = ((255, 255, 255))
                    block.blitblocks()
            elif (frames <= 480):
                for block in blocks:
                    block.color = ((0, 0, 255))
                    block.blitblocks()
            else:
                gamesettings.game_active = True
                gamesettings.victory_fanfare = False
                thepacman.resetPosition()
                for ghost in ghosts:
                    ghost.resetPosition()
                    ghost.speed += 1
                showgamestats.level += 1
                fruit.fruitReset()
                gf.readFile(screen, blocks, shield, powerpills, intersections)
                frames = 0
                pygame.time.wait(1000)
            frames += 1
        elif (thepacman.DEAD):
            thepacman.deathAnimation(frames)
            frames += 1
            if (frames > 600):
                gamesettings.game_active = True
                thepacman.DEAD = False
                thepacman.resetPosition()
                for ghost in ghosts:
                    ghost.resetPosition()
                frames = 0
                pygame.time.wait(1000)

        if (showgamestats.num_lives < 0):
            #reset game and save score
            screen.fill(BLACK)
            pygame.time.wait(2000)
            gamesettings.game_active = False
            thepacman.resetPosition()
            for ghost in ghosts:
                ghost.resetPosition()
                ghost.speed = 1
            showgamestats.num_lives = 3
            showgamestats.save_hs_to_file()
            showgamestats.score = 0
            showgamestats.level = 1
            fruit.fruitReset()
            playIntro = True  # reset the chime
            gf.readFile(screen, blocks, shield, powerpills, intersections)
            startScreen.makeScreen(screen, gamesettings)

        pygame.display.flip()
    def makeScreen(self, screen, gamesettings):

        # create second set of characters for the title screen
        titlepacman = Pacman(screen, gamesettings)
        titlepacman.rect.x, titlepacman.rect.y = 0, 280

        titleredghost = Ghosts(screen, "red")
        titleredghost.rect.x = -70
        titlecyanghost = Ghosts(screen, "cyan")
        titlecyanghost.rect.x = -105
        titleorangeghost = Ghosts(screen, "orange")
        titleorangeghost.rect.x = -140
        titlepinkghost = Ghosts(screen, "pink")
        titlepinkghost.rect.x = -175
        titlecyanghost.speed, titleorangeghost.speed, titlepinkghost.speed, titleredghost.speed = 2, 2, 2, 2

        pygame.init()
        pygame.display.set_caption("PACMAN")

        background = pygame.Surface(screen.get_size())
        background = background.convert()
        background.fill(BLACK)

        # Display P in front of the PACMAN pic
        font = pygame.font.Font(None, 144)
        text1 = font.render("PA", 2, WHITE)
        textpos1 = text1.get_rect()
        textpos1 = ((self.settings.screen_width / 2) - 300,
                    (self.settings.screen_height / 8) - 75)
        font = pygame.font.Font(None, 144)

        # Display "MAN" after the PACMAN pic
        text2 = font.render("MAN", 2, WHITE)
        textpos2 = text2.get_rect()
        textpos2 = ((self.settings.screen_width / 2),
                    (self.settings.screen_height / 8) - 75)

        # PACMAN position
        pacman_pos = ((self.settings.screen_width / 2) - 150,
                      (self.settings.screen_height / 8) - 55)

        # Ghosts position and text
        # Cyan Ghost
        font = pygame.font.Font(None, 44)
        text3 = font.render(" INKY", 2, (0, 255, 255))
        textpos3 = ((self.settings.screen_width / 2) - 250,
                    (self.settings.screen_height / 4) + 20)
        background.blit(self.image, (180, 220))

        # Orange Ghost
        text4 = font.render(" CLYDE", 2, (255, 165, 0))
        textpos4 = ((self.settings.screen_width / 2) - 150,
                    (self.settings.screen_height / 4) + 20)
        background.blit(self.image2, (290, 220))

        # Pink Ghost
        text5 = font.render(" PINKY", 2, (255, 20, 147))
        textpos5 = ((self.settings.screen_width / 2) - 25,
                    (self.settings.screen_height / 4) + 20)
        background.blit(self.image3, (410, 220))

        # Red Ghost
        text6 = font.render(" BLINKY", 2, (250, 0, 0))
        textpos6 = ((self.settings.screen_width / 2) + 100,
                    (self.settings.screen_height / 4) + 20)
        background.blit(self.image4, (550, 220))

        # Draw onto screen
        background.blit(text1, textpos1)
        background.blit(self.pacmanimage, pacman_pos)
        background.blit(text2, textpos2)
        background.blit(text3, textpos3)
        background.blit(text4, textpos4)
        background.blit(text5, textpos5)
        background.blit(text6, textpos6)

        # Blit everything to screen
        screen.blit(
            self.image,
            (self.settings.screen_width / 2, self.settings.screen_height / 3))
        screen.blit(background, (200, 200))

        #Play button
        play_button = Button(screen, "Play")

        # High Scores button
        hs_button = Button(screen, "High Scores")
        show_hs_menu = False

        # animation
        titlepacman.moving_right = True
        titlecyanghost.moving_right = True
        titleorangeghost.moving_right = True
        titlepinkghost.moving_right = True
        titleredghost.moving_right = True

        pygame.display.flip()

        while True:
            pygame.time.Clock().tick(120)  # 120 fps lock
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    pygame.quit()
                    quit()
                elif event.type == pygame.MOUSEBUTTONDOWN:
                    show_hs_menu = False
                    mouse_x, mouse_y = pygame.mouse.get_pos()
                    play_clicked = play_button.rect.collidepoint(
                        mouse_x, mouse_y)
                    hs_clicked = hs_button.rect.collidepoint(mouse_x, mouse_y)
                    if (play_clicked):
                        self.settings.game_active = True
                        return
                    elif (hs_clicked):
                        show_hs_menu = True
            if (show_hs_menu):
                screen.fill(BLACK)
                pygame.font.init()
                myfont = pygame.font.SysFont(None, 40)
                score1 = myfont.render(
                    '1. ' + str(self.showgamestats.high_scores_all[0]), False,
                    (255, 255, 255))
                score2 = myfont.render(
                    '2. ' + str(self.showgamestats.high_scores_all[1]), False,
                    (255, 255, 255))
                score3 = myfont.render(
                    '3. ' + str(self.showgamestats.high_scores_all[2]), False,
                    (255, 255, 255))
                notify = myfont.render(
                    'Click anywhere on the screen to go back', False,
                    (255, 255, 255))
                screen.blit(score1, (150, 58))
                screen.blit(score2, (150, 188))
                screen.blit(score3, (150, 318))
                screen.blit(notify, (50, 400))
            else:
                if (titlepinkghost.rect.x > 850):
                    # move left
                    titlepacman.moving_right, titlepacman.moving_left = False, True
                    titlecyanghost.moving_right, titlecyanghost.moving_left, titlecyanghost.afraid = False, True, True
                    titleorangeghost.moving_right, titleorangeghost.moving_left, titleorangeghost.afraid = False, True, True
                    titlepinkghost.moving_right, titlepinkghost.moving_left, titlepinkghost.afraid = False, True, True
                    titleredghost.moving_right, titleredghost.moving_left, titleredghost.afraid = False, True, True
                elif (titlepacman.rect.x < -50):
                    # move right
                    titlepacman.moving_right, titlepacman.moving_left = True, False
                    titlecyanghost.moving_right, titlecyanghost.moving_left, titlecyanghost.afraid = True, False, False
                    titleorangeghost.moving_right, titleorangeghost.moving_left, titleorangeghost.afraid = True, False, False
                    titlepinkghost.moving_right, titlepinkghost.moving_left, titlepinkghost.afraid = True, False, False
                    titleredghost.moving_right, titleredghost.moving_left, titleredghost.afraid = True, False, False

                screen.blit(background, (0, 0))
                play_button.draw_button()
                hs_button.draw_button()

                titlepacman.blitpacman()
                titlepacman.update()
                titlecyanghost.blitghosts()
                titleorangeghost.blitghosts()
                titleredghost.blitghosts()
                titlepinkghost.blitghosts()
                titlecyanghost.update()
                titleorangeghost.update()
                titleredghost.update()
                titlepinkghost.update()

            pygame.display.flip()