Exemple #1
0
class GameController(object):
    def __init__(self):
        pygame.init()
        dx, dy = 25, 25
        rows, columns = 20, 20
        self.nodes = NodeGroup(dx, dy, rows, columns, dx, dy)
        self.nodes.usePacmanTemplate()
        rows, columns = self.nodes.rows, self.nodes.columns
        #print len(self.nodes.nodeList)
        #print "+++++++++++++++++"
        #huntkill.generateMaze(self.nodes)
        backtracker.generateMaze(self.nodes)
        #wilson.generateMaze(self.nodes)
        #aldous_broder.generateMaze(self.nodes)
        #binaryTree.generateMaze(self.nodes.nodeList)
        #sidewinder.generateMaze(self.nodes.nodeList)
        #print "DEADENDS = " + str(self.nodes.deadends())

        self.nodes.braid()
        #self.nodes.removeRedundantNodes()

        SCREENSIZE = (dx * (columns + 1), dy * (rows + 1))
        self.screen = pygame.display.set_mode(SCREENSIZE, 0, 32)
        self.setBackGround(SCREENSIZE)

    def setBackGround(self, screensize):
        self.background = pygame.surface.Surface(screensize).convert()
        self.background.fill((0, 0, 0))

    def update(self):
        self.checkEvents()
        self.render()

    def checkEvents(self):
        for event in pygame.event.get():
            if event.type == QUIT:
                exit()

    def render(self):
        self.screen.blit(self.background, (0, 0))
        self.nodes.render(self.screen)
        pygame.display.update()
Exemple #2
0
class GameController(object):
    def __init__(self):
        pygame.init()
        self.screen = pygame.display.set_mode(SCREENSIZE, 0, 32)
        self.background = None
        self.setBackground()
        self.clock = pygame.time.Clock()
        self.hiscore = 0
        self.hiscoreLabel = Text(str(self.hiscore).zfill(8), WHITE, 0, 16, 16)
        self.level = 1
        self.levelLabel = Text(str(self.level).zfill(2), WHITE, 368, 16, 16)
        self.sheet = Spritesheet()
        self.maze = Maze(self.sheet)
        self.trophies = FruitTrophy()
        self.startLabel = Text("START!", RED, 190, 320, 16)
        self.pauseLabel = Text("PAUSED!", RED, 180, 320, 16)
        self.started = False
        self.pausedByPlayer = False
        self.pauseTime = 0
        self.timer = 0
        self.nextLevelAfterPause = False
        self.startAfterPause = False
        self.restartAfterPause = False
        self.flash_background = False
        self.flash_rate = 0.2
        self.flashtime = 0
        self.show_white_background = False
        self.ghost_score = None
        self.fruit_score = None
        self.gameover = False

    def setBackground(self):
        self.background = pygame.surface.Surface(SCREENSIZE).convert()
        self.background_white = pygame.surface.Surface(SCREENSIZE).convert()
        self.background.fill(BLACK)

    def startGame(self):
        print("Restarting the game")
        self.maze.setup(self.level)
        self.nodes = NodeGroup(self.maze.filename + ".txt")
        self.pellets = PelletGroup(self.maze.filename + ".txt")
        self.pacman = Pacman(self.nodes, self.sheet)
        self.ghosts = GhostGroup(self.nodes, self.sheet)
        self.paused = True
        self.fruit = None
        self.pelletsEaten = 0
        self.lifeIcons = LifeIcon(self.sheet)
        self.maze.stitchMaze(self.background, self.background_white)
        self.hiScoreTxtStatic = Text("HI SCORE", WHITE, 0, 0, 16)
        self.scoreTxtStatic = Text("SCORE", WHITE, 208, 0, 16)
        self.levelTxtStatic = Text("LEVEL", WHITE, 368, 0, 16)
        self.score = 0
        self.scoreLabel = Text(str(self.score).zfill(8), WHITE, 208, 16, 16)
        self.started = False
        self.pausedByPlayer = False
        self.pauseTime = 0
        self.timer = 0
        self.nextLevelAfterPause = False
        self.startAfterPause = False
        self.restartAfterPause = False
        self.flash_background = False
        self.flash_rate = 0.2
        self.flashtime = 0
        self.show_white_background = False
        self.gameover = False
        self.level = 1  #####

    def restartLevel(self):
        print("Restarting the level")
        self.pacman.reset()
        self.ghosts = GhostGroup(self.nodes, self.sheet)
        self.paused = True
        self.started = False
        self.pausedByPlayer = False
        self.fruit = None
        self.pauseTime = 0
        self.timer = 0
        self.nextLevelAfterPause = False
        self.startAfterPause = False
        self.restartAfterPause = False
        self.gameover = False
        self.flash_background = False
        self.flash_rate = 0.2
        self.flashtime = 0
        self.show_white_background = False

    def nextLevel(self):
        self.setBackground()  ###
        self.level += 1
        self.maze.setup(self.level)
        self.nodes = NodeGroup(self.maze.filename + ".txt")
        self.pellets = PelletGroup(self.maze.filename + ".txt")
        self.pacman.updateNodes(self.nodes)
        self.pacman.reset()
        self.ghosts = GhostGroup(self.nodes, self.sheet)
        self.paused = True
        self.started = False
        self.pausedByPlayer = False
        self.fruit = None
        self.pelletsEaten = 0
        self.lifeIcons = LifeIcon(self.sheet)
        self.maze.stitchMaze(self.background, self.background_white)
        self.pellets = PelletGroup(self.maze.filename + ".txt")
        self.levelLabel.updateText(str(self.level).zfill(2))
        self.pauseTime = 0
        self.timer = 0
        self.nextLevelAfterPause = False
        self.startAfterPause = False
        self.restartAfterPause = False
        self.gameover = False
        self.flash_background = False
        self.flash_rate = 0.2
        self.flashtime = 0
        self.show_white_background = False

    def update(self):
        dt = self.clock.tick(30) / 1000.0
        self.pellets.update(dt)
        if not self.paused:
            self.pacman.update(dt)
            self.ghosts.update(dt, self.pacman)
            if self.fruit is not None:
                self.fruit.update(dt)
        else:
            if not self.pacman.alive:
                print("death sequence")
                self.pacman.updateAnimation(dt)
        self.checkEvents()

        if self.pauseTime > 0:
            self.paused = True
            self.timer += dt
            if self.timer >= self.pauseTime:
                self.paused = False
                self.pauseTime = 0
                self.timer = 0
                self.ghost_score = None
                if self.nextLevelAfterPause:
                    self.nextLevel()
                if self.startAfterPause:
                    self.startGame()
                if self.restartAfterPause:
                    self.restartLevel()

        if self.flash_background:
            self.flashtime += dt
            if self.flashtime >= self.flash_rate:
                self.flashtime = 0
                self.show_white_background = not self.show_white_background

        if self.fruit_score is not None:
            self.fruit_score.update(dt)
            if self.fruit_score.hide:
                self.fruit_score = None

        self.render()

    def checkEvents(self):
        for event in pygame.event.get():
            if event.type == QUIT:
                exit()
            elif event.type == KEYDOWN:
                if event.key == K_SPACE:
                    self.started = True
                    self.paused = not self.paused
                    self.pausedByPlayer = self.paused

        if not self.paused:
            self.checkPelletEvents()
            self.checkGhostEvents()
            self.checkFruitEvents()

    def checkPelletEvents(self):
        pellet = self.pacman.eatPellets(self.pellets.pelletList)
        if pellet:
            self.pelletsEaten += 1
            self.updateScores(pellet.points)
            self.ghosts.resetPoints()
            if (self.pelletsEaten == 70 or self.pelletsEaten == 140):
                if self.fruit is None:
                    self.fruit = Fruit(self.nodes, self.sheet, self.level)
            self.pellets.pelletList.remove(pellet)
            if pellet.name == "powerpellet":
                self.ghosts.freightMode()
            if self.pellets.isEmpty():
                self.pauseTime = 3
                self.nextLevelAfterPause = True
                self.flash_background = True

    def checkGhostEvents(self):
        self.ghosts.release(self.pelletsEaten)
        ghost = self.pacman.eatGhost(self.ghosts)
        if ghost is not None:
            if ghost.mode.name == "FREIGHT":
                self.pauseTime = 0.5
                self.ghost_score = Text(str(self.ghosts.points), WHITE,
                                        ghost.position.x, ghost.position.y, 8)
                ghost.spawnMode()
                self.updateScores(self.ghosts.points)
                self.ghosts.doublePoints()
            elif ghost.mode.name != "SPAWN":
                self.pacman.alive = False
                self.paused = True
                self.pauseTime = 2
                self.pacman.decreaseLives()
                self.gameover = self.pacman.lives == 0
                if self.gameover:
                    print("All of Pacmans lives are gone")
                    self.startAfterPause = True
                else:
                    print("Lost a life, but still going")
                    self.restartAfterPause = True

    def checkFruitEvents(self):
        if self.fruit is not None:
            if self.pacman.eatFruit(self.fruit):
                self.updateScores(self.fruit.points)
                self.trophies.add(self.fruit.name, self.fruit.collect())
                self.fruit_score = Text(str(self.fruit.points), WHITE,
                                        self.fruit.position.x,
                                        self.fruit.position.y, 8)
                self.fruit_score.lifespan = 1
                self.fruit = None
            else:
                if self.fruit.killme:
                    self.fruit = None

    def updateScores(self, value):
        self.score += value
        self.scoreLabel.updateText(str(self.score).zfill(8))
        if self.score > self.hiscore:
            self.hiscore = self.score
            self.hiscoreLabel.updateText(str(self.hiscore).zfill(8))

    def render(self):
        if self.show_white_background:
            self.screen.blit(self.background_white, (0, 0))
        else:
            self.screen.blit(self.background, (0, 0))

        self.nodes.render(self.screen)
        self.pellets.render(self.screen)
        if self.fruit is not None:
            self.fruit.render(self.screen)
        if not self.paused or not self.started:
            self.pacman.render(self.screen)
            self.ghosts.render(self.screen)
        self.lifeIcons.render(self.screen, self.pacman.lives - 1)
        self.hiScoreTxtStatic.render(self.screen)
        self.scoreTxtStatic.render(self.screen)
        self.levelTxtStatic.render(self.screen)
        self.scoreLabel.render(self.screen)
        self.hiscoreLabel.render(self.screen)
        self.levelLabel.render(self.screen)
        self.trophies.render(self.screen)

        if self.pausedByPlayer:
            self.pauseLabel.render(self.screen)
        if not self.started:
            self.startLabel.render(self.screen)

        if self.ghost_score is not None:
            self.ghost_score.render(self.screen)
        if self.fruit_score is not None:
            self.fruit_score.render(self.screen)

        if self.paused and not self.pacman.alive:
            self.pacman.render(self.screen)

        pygame.display.update()
Exemple #3
0
class GameController(object):
    def __init__(self):
        pygame.init()
        self.screen = pygame.display.set_mode(SCREENSIZE, 0, 32)
        self.background = None
        self.setBackground()
        self.clock = pygame.time.Clock()
        
    def setBackground(self):
        self.background = pygame.surface.Surface(SCREENSIZE).convert()
        self.background.fill(BLACK)

    def startGame(self):
        self.nodes = NodeGroup("maze.txt")
        self.pacman = Pacman(self.nodes)
        self.ghosts = GhostGroup(self.nodes)
        self.gameover = False

    
    def update(self):
        if not self.gameover:
            dt = self.clock.tick(30) / 1000.0
            self.pacman.update(dt)
            self.ghosts.update(dt, self.pacman)
        self.checkGhostEvents()
        self.checkEvents()
        self.render()

    def checkGhostEvents(self):
        ghost = self.pacman.eatGhost(self.ghosts)
        if ghost is not None:
            if ghost.mode.name == "FREIGHT":
                ghost.spawnMode(speed=2)
                self.pacman.visible = False
                ghost.visible = False
            elif ghost.mode.name == "CHASE" or ghost.mode.name == "SCATTER":
                #self.pacman.loseLife()
                exit()

    def checkEvents(self):
        for event in pygame.event.get():
            if event.type == QUIT:
                exit()
            elif event.type == KEYDOWN:
                if event.key == K_SPACE:
                    if self.gameover:
                        self.startGame()

    def restartLevel(self):
        self.pacman.reset()
        self.ghosts = GhostGroup(self.nodes)

    #def resolveDeath(self):
        #if self.pacman.lives == 0:
            #self.gameover = True
        #else:
            #self.restartLevel()


    def render(self):
        self.screen.blit(self.background, (0, 0))
        self.nodes.render(self.screen)
        self.pacman.render(self.screen)
        self.ghosts.render(self.screen)
        #self.pacman.renderLives(self.screen)
        pygame.display.update()
Exemple #4
0
class GameController(object):
    def __init__(self):
        pygame.init()
        self.screen = pygame.display.set_mode(SCREENSIZE, 0, 32)
        self.background = None
        self.set_background()
        self.clock = pygame.time.Clock()
        self.score = 0
        self.font = pygame.font.SysFont("arial", 20)
        self.lives = 5
        self.fruit = None
        self.pelletsEaten = 0
        self.sheet = SpriteSheet()
        self.cur_song = 2
        self.data_driver = MongoDriver("1")
        self.level = 1

    def switch_song(self, delta):
        self.cur_song += delta
        if self.cur_song == 5:
            self.cur_song = 1
        if self.cur_song == 0:
            self.cur_song = 4

        if self.cur_song == 1:
            pygame.mixer.music.load("Music/song1.mp3")
        elif self.cur_song == 2:
            pygame.mixer.music.load("Music/song2.mp3")
        elif self.cur_song == 3:
            pygame.mixer.music.load("Music/song3.mp3")
        elif self.cur_song == 4:
            pygame.mixer.music.load("Music/song4.mp3")
        pygame.mixer.music.play(999)
        pygame.event.wait()

    def set_background(self):
        self.background = pygame.Surface(SCREENSIZE).convert()
        self.background.fill(BLACK)

    def start_game(self):
        self.nodes = NodeGroup("Mazes/maze1.txt")
        self.pellets = PelletGroup("Mazes/maze1.txt")
        self.pacman = Pacman(self.nodes, self.sheet)
        self.ghosts = Ghosts(self.nodes, self.sheet)
        self.paused = True
        self.lifeIcons = Lives(self.sheet)
        self.maze = Maze(self.sheet)
        self.maze.get_maze("Mazes/maze1")
        self.maze.stitch_maze(self.background)
        self.pelletsEaten = 0
        self.switch_song(1)

    def update(self):
        dt = self.clock.tick(30) / 1000.0
        if not self.paused:
            self.pacman.update(dt)
            self.ghosts.update(dt, self.pacman)
            if self.fruit is not None:
                self.fruit.update(dt)

        else:
            if not self.pacman.alive:
                self.pacman.update(dt)
                if self.pacman.deathSequenceFinished:
                    if self.lives == 0:
                        self.start_game()
                    else:
                        self.restart_level()

        self.check_events()
        self.render()

    def save_game(self):
        self.data_driver.clean()
        self.data_driver.save(self.pellets.get_snapshot(), self.score,
                              self.lives, self.level)

    def load_game(self):
        self.start_game()
        self.pellets.create_pellet_list_snap(self.data_driver.get_pellets())
        self.score = self.data_driver.get_score()

        self.lives = self.data_driver.get_lives()
        self.level = self.data_driver.get_level()

    def check_events(self):
        for event in pygame.event.get():
            if event.type == QUIT:
                self.save_game()
                exit()
            if event.type == KEYDOWN:
                if event.key == K_SPACE:
                    self.paused = not self.paused
                if event.key == K_PERIOD:
                    self.switch_song(1)
                if event.key == K_COMMA:
                    self.switch_song(-1)
                if event.key == K_F8:
                    self.save_game()
                if event.key == K_F7:
                    self.load_game()

        if not self.paused:
            self.check_pellet_events()
            self.check_ghost_events()
            self.check_fruit_events()

    def check_pellet_events(self):
        pellet = self.pacman.eat_pellets(self.pellets.pelletList)
        if pellet:
            self.pelletsEaten += 1
            self.score += pellet.points
            if self.pelletsEaten == 70 or self.pelletsEaten == 140:
                if self.fruit is None:
                    self.fruit = Fruit(self.nodes, self.sheet)

            self.pellets.pelletList.remove(pellet)
            if pellet.name == "powerpellet":
                self.ghosts.freight_mode()
            if self.pellets.is_empty():
                self.level += 1
                self.start_game()

    def restart_level(self):
        self.pacman = Pacman(self.nodes, self.sheet)
        self.ghosts = Ghosts(self.nodes, self.sheet)
        self.fruit = None
        self.paused = True

    def check_ghost_events(self):
        self.ghosts.release(self.pelletsEaten)
        ghost = self.pacman.eat_ghost(self.ghosts)
        # if self.pacman.eatGhost(self.ghost):
        if ghost is not None:
            if ghost.mode.name == "FREIGHT":
                self.score += ghost.points
                ghost.spawn_mode()
            elif ghost.mode.name != "SPAWN":
                self.lives -= 1
                self.paused = True
                self.pacman.alive = False
                self.pacman.animate.set_animation("death", 0)
                if (self.lives < 1):
                    self.level = 1
                    self.lives = 5
                    self.score = 0
                    self.start_game()

    def check_fruit_events(self):
        if self.fruit is not None:
            if self.pacman.eat_fruit(self.fruit) or self.fruit.its_time_to_die:
                if not self.fruit.its_time_to_die:
                    self.score += self.fruit.points
                self.fruit = None

    # TODO: original font score
    def render_score(self):
        text = self.font.render("Score: " + str(self.score), False, WHITE)
        self.screen.blit(text, (10, 10, 100, 100))

    def render_level(self):
        text = self.font.render("Level: " + str(self.level), False, WHITE)
        self.screen.blit(text, (200, 10, 300, 100))

    def render(self):
        self.screen.blit(self.background, (0, 0))
        self.nodes.render(self.screen)
        self.pellets.render(self.screen)
        self.pacman.render(self.screen)
        self.ghosts.render(self.screen)
        self.render_score()
        self.render_level()
        if self.fruit is not None:
            self.fruit.render(self.screen)

        y = TILE_HEIGHT * N_ROWS
        text = self.font.render("Lives: ", False, WHITE)
        self.screen.blit(text, (10, 60, 100, 100))

        self.lifeIcons.render(self.screen, self.lives - 1)
        pygame.display.update()
Exemple #5
0
class GameController:
    """
    This the class that controls our basic game loop, what this method does
    is update what happens in game based on other events, and what just keeps
    game running until we end it
    """

    def __init__(self):
        pygame.init()
        self.nodes = None
        self.pacman = None
        self.ghost = None
        self.screen = pygame.display.set_mode(SCREENSIZE, 0, 32)
        self.background = None
        self.setBackground()
        self.clock = pygame.time.Clock()


    def setBackground(self):
        """
        We create a background and set it to the color BLACK that we defined in
        the constants.py file.
        """
        self.background = pygame.surface.Surface(SCREENSIZE).convert()
        self.background.fill(BLACK)

    def startGame(self):

        self.nodes = NodeGroup("maze.txt")
        self.pacman = Pacman(self.nodes)
        self.ghost = Ghost(self.nodes)

    def update(self):
        """
        The update method is a method that we call once per frame of the game.
        It's basically our game loop
        """
        dt = self.clock.tick(30) / 1000.0
        self.pacman.update(dt)
        self.ghost.update(dt, self.pacman)
        self.checkUpdater()
        self.render()

    def checkUpdater(self):
        """
        This method checks for certain events.
        Right now it is just checking to see when we exit out of the game.
        :return:
        """
        for event in pygame.event.get():
            if event.type == QUIT:
                exit()

    def render(self):
        """
        The render method is the method we'll use to draw the images to the screen.
        it uses the update method, it is consistently running until the
        window is closed, right now it just keeps drawing what we want on screen
        """

        self.screen.blit(self.background, (0, 0))
        self.nodes.render(self.screen)
        self.pacman.render(self.screen)
        self.ghost.render(self.screen)
        pygame.display.update()