Exemple #1
0
 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
Exemple #2
0
 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  #####
Exemple #3
0
 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 play(self):
        print("Start game")
        # game_state = ts.GameState.TITLE
        #
        # while True:
        #     if game_state is ts.GameState.TITLE:
        #         game_state = ts.title_screen(self.screen)
        #
        #     if game_state is ts.GameState.PlAY:

        self.level.reset()
        levelmap = self.level.getLevel()
        self.maze.getMaze(levelmap["name"].split(".")[0])
        self.maze.constructMaze(self.background, self.background_flash, levelmap["row"])
        self.nodes = NodeGroup(levelmap["name"])
        self.pellets = PelletGroup(levelmap["name"])
        self.pacman = Pacman(self.sound, self.nodes, self.sheet)
        self.ghosts = GhostGroup(self.nodes, self.sheet)
        self.pelletsEaten = 0
        self.fruit = None
        self.pause.force(True)
        self.text.showReady()
        self.sound.intro()
        self.text.updateLevel(self.level.level + 1)
        self.gameover = False
        self.maze.reset()
        self.flashBackground = False
Exemple #5
0
    def startGame(self):
        self.setBackGround()
        self.maze = Maze(self.level, self.sheet)
        self.maze.fillMaze(self.background)
        self.mazeFlash = Maze(self.level, self.sheet, startcol=11)
        self.mazeFlash.fillMaze(self.backgroundFlash)

        self.nodes = NodeGroup(self.level)
        self.pellets = PelletGroup(self.level)
        self.commonSetup()
 def startLevel(self):
     print("Start game")
     self.setBackground()
     self.maze.getMaze("maze")
     self.maze.constructMaze(self.background, self.background_flash, 0)
     self.nodes = NodeGroup("maze.txt")
     self.pellets = PelletGroup("maze.txt")
     self.pacman.nodes = self.nodes
     self.pacman.reset()
     self.ghosts = GhostGroup(self.nodes, self.sheet)
     self.pelletsEaten = 0
     self.text.updateLevel(self.level.level+1)
     self.fruit = None
     self.pause.force(True)
     self.flashBackground = False
     self.maze.reset()
     self.portal = None
     self.portal2 = None
     self.portal3 = None
     self.portal4 = None
Exemple #7
0
    def start_game(self):

        self.sheet = Spritesheet()
        self.nodes = NodeGroup("maze.txt")
        self.pellets = Pellets_Group("maze.txt")
        self.pacman = Pacman(self.nodes, self.sheet)
        self.ghosts = GhostGroup(self.nodes, self.sheet)
        self.life_icons = LifeIcon(self.sheet)
        self.paused = False
        self.maze = Maze(self.sheet)
        self.maze.get_maze("maze")
        self.maze.combine_maze(self.background)
Exemple #8
0
    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)
Exemple #9
0
    def startLevel(self):
        #Begins the given level

        levelmap = self.level.getLevel()
        self.setBackground()
        self.nodes = NodeGroup(levelmap["mazename"])
        self.pellets = PelletGroup(levelmap["pelletname"])
        self.pacman.nodes = self.nodes
        self.pacman.reset()
        #self.ghosts = GhostGroup(self.nodes, self.sheet)   Ghost command, removed to get rid of ghosts
        self.pelletsEaten = 0
        self.fruit = None
        self.pause.force(True)
        self.text.showReady()
        self.text.updateLevel(self.level.level + 1)
Exemple #10
0
    def startGame(self):
        #Sets up the game

        self.level.reset()
        levelmap = self.level.getLevel()
        self.maze.getMaze(levelmap["mazename"].split(".")[0])
        self.maze.constructMaze(self.background)
        self.nodes = NodeGroup(levelmap["mazename"])
        self.pellets = PelletGroup(levelmap["pelletname"])
        self.pacman = Pacman(self.nodes, self.sheet)
        #self.ghosts = GhostGroup(self.nodes, self.sheet)  Ghost command, removed to get rid of ghosts
        self.pelletsEaten = 0
        self.fruit = None
        self.pause.force(False)  #Was True, set to false for automatic start
        self.gameover = False
        self.score = 0
class Pink_Ghost(Entity):
    def __init__(self, nodes, spritesheet):
        Entity.__init__(self, nodes, spritesheet)
        self.name = "ghost"
        self.image = self.spritesheet.getImage(0, 3, 32, 32)
        self.points = 200
        self.nodee = NodeGroup("maze1.txt")

    def getValidDirections(self):
        validDirections = []
        for key in self.node.neighbors.keys():
            if self.node.neighbors[key] is not None:
                if key != self.direction * -1:
                    validDirections.append(key)
        if len(validDirections) == 0:
            validDirections.append(self.forceBacktrack())
        return validDirections

    def randomDirection(self, validDirections):
        index = randint(0, len(validDirections) - 1)
        return validDirections[index]

    def moveBySelf(self):
        if self.overshotTarget():
            #self.node = self.target
            #self.portal()
            #validDirections = self.getValidDirections()
            #self.direction = self.randomDirection(validDirections)
            #self.target = self.node.neighbors[self.direction]
            self.target = self.nodee.return_node()
            self.setPosition()

    def forceBacktrack(self):
        if self.direction * -1 == UP:
            return UP
        if self.direction * -1 == DOWN:
            return DOWN
        if self.direction * -1 == LEFT:
            return LEFT
        if self.direction * -1 == RIGHT:
            return RIGHT

    def portalSlowdown(self):
        self.speed = 100
        if self.node.portalNode or self.target.portalNode:
            self.speed = 50
Exemple #12
0
 def startGame(self):
     self.nodes = NodeGroup("maze1.txt")
     self.pellets = PelletGroup("pellets1.txt")
     self.pacman = Pacman(self.nodes, self.sheet)
     self.ghost = Ghost(self.nodes, self.sheet)
     self.orange_ghost = Orange_Ghost(self.nodes, self.sheet)
     self.blue_ghost = Blue_Ghost(self.nodes, self.sheet)
     self.pink_ghost = Pink_Ghost(self.nodes, self.sheet)
     self.maze.getMaze('maze1')
     self.maze.constructMaze(self.background)
     self.text = TextGroup()
     self.pelletsEaten = 0
     self.score = 0
     self.pp_time = 0
     self.time_eat = -7000
     self.gameover = 3
     self.pac_pellet = 0
     self.start_time = time.time()
Exemple #13
0
 def startLevel(self):
     print("Start new level")
     levelmap = self.level.getLevel()
     self.setBackground()
     self.maze.getMaze(levelmap["name"].split(".")[0])
     self.maze.constructMaze(self.background, self.background_flash,
                             levelmap["row"])
     self.nodes = NodeGroup(levelmap["name"])
     self.pellets = PelletGroup(levelmap["name"])
     self.pacman.nodes = self.nodes
     self.pacman.reset()
     self.ghosts = GhostGroup(self.nodes, self.sheet)
     self.pelletsEaten = 0
     self.fruit = None
     self.pause.force(True)
     self.text.updateLevel(self.level.level + 1)
     self.flashBackground = False
     self.maze.reset()
Exemple #14
0
def run_game():
    pygame.init()
    settings = Settings()
    screen = pygame.display.set_mode((settings.screen_width, settings.screen_height))
    pygame.display.set_caption("Pacman Portal by Diana Joya")

    nodes = NodeGroup(screen, "pacmanportalmaze.txt")
    stars = StarGroup(screen, "pacmanportalmaze.txt")
    maze = Maze(screen, settings, "pacmanportalmaze.txt", "brick", "shield")

    stats = GameStats(settings)
    score_board = ScoreBoard(settings, screen, stats)

    ghost = TesterGhost(settings, screen, nodes)
    redenemy = Red(settings, screen, nodes)
    pinkenemy = Pink(settings, screen, nodes)
    orangeenemy = Orange(settings, screen, nodes)

    update_score = Scores(settings, screen, stats, nodes, stars, score_board, redenemy, pinkenemy, orangeenemy, ghost)
    pacman = Pacman(settings, screen, stats, nodes, stars, update_score, redenemy, pinkenemy, orangeenemy, ghost)

    start_screen = Start(settings, screen, stats, pacman, ghost, redenemy, pinkenemy, orangeenemy)
    update = UpdateGame(settings, screen, stats, nodes, stars, maze, pacman, ghost, redenemy, pinkenemy, orangeenemy,
                        score_board, update_score, start_screen)

    while True:
        if stats.game_active:
            pacman.update()
            ghost.update()
            redenemy.update()
            pinkenemy.update()
            orangeenemy.update()
        if stats.game_over:
            start_screen.reset()
            start_screen.start_animation()
        update.check_events()
        update.update_screen()
Exemple #15
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 #16
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 #17
0
class GameController(object):
    def __init__(self):
        pygame.init()
        self.screen = pygame.display.set_mode(SCREENSIZE, 0, 32)
        self.background = None
        self.background_flash = None
        self.setBackground()
        self.clock = pygame.time.Clock()
        self.pelletsEaten = 0
        self.fruit = None
        self.pause = Pauser(True)
        self.text = TextGroup()
        self.score = 0
        self.gameover = False
        self.sheet = Spritesheet()
        self.maze = Maze(self.sheet)
        self.flashBackground = False
        self.portal = None
        self.portal2 = None
        self.portal3 = None
        self.portal4 = None
        self.level = LevelController()
        self.mainScreen = True
        self.gameScreen = False
        self.hsScreen = False
        self.eat_sound = pygame.mixer.Sound("pacman_chomp.wav")
        self.main_menu_sound = pygame.mixer.Sound("pacman_intermission.wav")
        self.beginning_sound = pygame.mixer.Sound("pacman_beginning.wav")
        self.eat_fruit = pygame.mixer.Sound("pacman_eatfruit.wav")
        self.eat_ghost = pygame.mixer.Sound("pacman_eatghost.wav")
        self.death_sound = pygame.mixer.Sound("pacman_death.wav")
        
    def setBackground(self):
        self.background = pygame.surface.Surface(SCREENSIZE).convert()
        self.background_flash = pygame.surface.Surface(SCREENSIZE).convert()
        self.background.fill(BLACK)

    def startGame(self):
        print("Start game")
        self.level.reset()
        self.maze.getMaze("maze")
        self.maze.constructMaze(self.background, self.background_flash, 0)
        self.nodes = NodeGroup("maze.txt")
        self.pellets = PelletGroup("maze.txt")
        self.pacman = Pacman(self.nodes, self.sheet)
        self.ghosts = GhostGroup(self.nodes, self.sheet)
        self.pelletsEaten = 0
        self.fruit = None
        self.pause.force(True)
        self.text.showReady()
        self.gameover = False
        self.maze.reset()
        self.flashBackground = False
        self.text.updateLevel(self.level.level + 1)
        self.portal = None
        self.portal2 = None
        self.portal3 = None
        self.portal4 = None

    def startLevel(self):
        print("Start game")
        self.setBackground()
        self.maze.getMaze("maze")
        self.maze.constructMaze(self.background, self.background_flash, 0)
        self.nodes = NodeGroup("maze.txt")
        self.pellets = PelletGroup("maze.txt")
        self.pacman.nodes = self.nodes
        self.pacman.reset()
        self.ghosts = GhostGroup(self.nodes, self.sheet)
        self.pelletsEaten = 0
        self.text.updateLevel(self.level.level+1)
        self.fruit = None
        self.pause.force(True)
        self.flashBackground = False
        self.maze.reset()
        self.portal = None
        self.portal2 = None
        self.portal3 = None
        self.portal4 = None

    def resolveLevelClear(self):
        self.level.nextLevel()
        self.startLevel()
        self.pause.pauseType = None

    def restartLevel(self):
        print("Restart game")
        self.pacman.reset()
        self.ghosts = GhostGroup(self.nodes, self.sheet)
        self.pause.force(True)
        self.fruit = None
        self.flashBackground = False
        self.maze.reset()
        # self.portal = None  # commented so portals dont disappear on reset/death
        # self.portal2 = None
        # self.portal3 = None
        # self.portal4 = None
        
    def update(self):
        if not self.gameover:
            dt = self.clock.tick(30) / 1000.0
            if not self.pause.paused:
                self.pacman.update(dt)
                self.ghosts.update(dt, self.pacman)
                if self.fruit is not None:
                    self.fruit.update(dt)

                if self.portal is not None:
                    self.portal.update(dt)
                    self.portal2.update(dt)
                if self.portal3 is not None:
                    self.portal3.update(dt)
                    self.portal4.update(dt)

                if self.pause.pauseType != None:
                    self.pause.settlePause(self)
            
                self.checkPelletEvents()
                self.checkGhostEvents()
                self.checkFruitEvents()
                self.checkPortalEvents()

            else:
                if self.flashBackground:
                    self.maze.flash(dt)
                    
                if self.pacman.animateDeath:
                    self.pacman.updateDeath(dt)

            self.pause.update(dt)
            self.pellets.update(dt)
            self.text.update(dt)
        self.checkEvents()
        self.text.updateScore(self.score)
        self.render()

    def checkEvents(self):
        for event in pygame.event.get():
            if event.type == QUIT:
                if self.score > 0:
                    with open("high_scores.txt", 'a') as f:
                        f.write(f'Highscore was {game.score}\n')
                exit()
            elif event.type == KEYDOWN:
                if event.key == K_SPACE:
                    if self.gameover:
                        self.startGame()
                    else:
                        self.pause.player()
                        if self.pause.paused:
                            self.text.showPause()
                        else:
                            self.text.hideMessages()
            elif event.type == pygame.MOUSEBUTTONDOWN:
                mouse_x, mouse_y = pygame.mouse.get_pos()
                if self.play_button.rect.collidepoint(mouse_x, mouse_y):
                    self.mainScreen = False
                    self.gameScreen = True
                elif self.score_button.rect.collidepoint(mouse_x, mouse_y):
                    self.mainScreen = False
                    self.hsScreen = True
                elif self.back_button.rect.collidepoint(mouse_x, mouse_y):
                    self.mainScreen = True
                    self.hsScreen = False

    def checkPelletEvents(self):
        pellet = self.pacman.eatPellets(self.pellets.pelletList)
        if pellet:
            self.eat_sound.set_volume(0.2)
            self.eat_sound.play()
            self.pelletsEaten += 1
            self.score += pellet.points
            if self.pelletsEaten == 25 or self.pelletsEaten == 150:
                if self.fruit is None:
                    self.fruit = Fruit(self.nodes, self.sheet, "cherry")
            if self.pelletsEaten == 75:
                if self.portal is None:
                    self.portal = Portal(self.nodes, self.sheet)
                if self.portal2 is None:
                    self.portal2 = Portal(self.nodes, self.sheet)
            if self.pelletsEaten == 125:
                if self.portal3 is None:
                    self.portal3 = Portal2(self.nodes, self.sheet)
                if self.portal4 is None:
                    self.portal4 = Portal2(self.nodes, self.sheet)
            self.pellets.pelletList.remove(pellet)
            if pellet.name == "powerpellet":
                self.ghosts.resetPoints()
                self.ghosts.freightMode()
            if self.pellets.isEmpty():
                self.pacman.visible = False
                self.ghosts.hide()
                self.pause.startTimer(3, "clear")
                self.flashBackground = 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.eat_ghost.set_volume(0.2)
                self.eat_ghost.play()
                self.score += ghost.points
                self.text.createTemp(ghost.points, ghost.position)
                self.ghosts.updatePoints()
                ghost.spawnMode(speed=2)
                self.pause.startTimer(1)
                self.pacman.visible = False
                ghost.visible = False

            elif ghost.mode.name != "SPAWN":
                self.death_sound.set_volume(0.2)
                self.death_sound.play()
                self.pacman.loseLife()
                self.ghosts.hide()
                self.pause.startTimer(3, "die")

    def checkFruitEvents(self):
        if self.fruit is not None:
            if self.pacman.eatFruit(self.fruit):
                self.eat_fruit.set_volume(0.2)
                self.eat_fruit.play()
                self.score += self.fruit.points 
                self.text.createTemp(self.fruit.points, self.fruit.position)
                self.fruit = None
                
            elif self.fruit.destroy:
                self.fruit = None

    def checkPortalEvents(self):
        if self.portal is not None:
            if self.pacman.enterPortal(self.portal):
                b = self.nodes.getNode(19*TILEWIDTH, 4*TILEHEIGHT, self.nodes.nodeList)
                self.pacman.node = b
                self.pacman.setPosition()
                self.pacman.direction = STOP
            elif self.portal.destroy:
                self.portal = None

        if self.portal2 is not None:
            if self.pacman.enterPortal(self.portal2):
                b = self.nodes.getNode(3*TILEWIDTH, 32*TILEHEIGHT, self.nodes.nodeList)
                self.pacman.node = b
                self.pacman.setPosition()
                self.pacman.direction = STOP
            elif self.portal2.destroy:
                self.portal2 = None

        if self.portal3 is not None:
            if self.pacman.enterPortal(self.portal3):
                b = self.nodes.getNode(21*TILEWIDTH, 32*TILEHEIGHT, self.nodes.nodeList)
                self.pacman.node = b
                self.pacman.setPosition()
                self.pacman.direction = STOP
            elif self.portal3.destroy:
                self.portal3 = None

        if self.portal4 is not None:
            if self.pacman.enterPortal(self.portal4):
                b = self.nodes.getNode(5*TILEWIDTH, 4*TILEHEIGHT, self.nodes.nodeList)
                self.pacman.node = b
                self.pacman.setPosition()
                self.pacman.direction = STOP
            elif self.portal4.destroy:
                self.portal4 = None

    def resolveDeath(self):
        if self.pacman.lives == 0:
            self.gameover = True
            self.pacman.visible = False
            self.text.showGameOver()
        else:
            self.restartLevel()
        self.pause.pauseType = None
    
    def render(self):
        self.screen.blit(self.maze.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 self.portal is not None:
            self.portal.render(self.screen)
        if self.portal2 is not None:
            self.portal2.render(self.screen)
        if self.portal3 is not None:
            self.portal3.render(self.screen)
        if self.portal4 is not None:
            self.portal4.render(self.screen)
        self.pacman.render(self.screen)
        self.ghosts.render(self.screen)
        self.pacman.renderLives(self.screen)
        self.text.render(self.screen)
        pygame.display.update()

    def main_screen_ani(self, current_time):
        if current_time <= 0.5:
            self.screen.blit(self.sheet.getImage(4, 0, TILEWIDTH * 2, TILEHEIGHT * 2), (TILEWIDTH, 18 * TILEHEIGHT))
            pygame.draw.circle(self.screen, WHITE, (25 * TILEWIDTH, 19.2 * TILEHEIGHT), 8)
        elif 0.5 < current_time <= 1:
            self.screen.blit(self.sheet.getImage(1, 0, TILEWIDTH * 2, TILEHEIGHT * 2), (3*TILEWIDTH, 18 * TILEHEIGHT))
            pygame.draw.circle(self.screen, WHITE, (25 * TILEWIDTH, 19.2 * TILEHEIGHT), 8)
        elif 1 < current_time <= 1.5:
            self.screen.blit(self.sheet.getImage(1, 1, TILEWIDTH * 2, TILEHEIGHT * 2), (6 * TILEWIDTH, 18 * TILEHEIGHT))
            pygame.draw.circle(self.screen, WHITE, (25 * TILEWIDTH, 19.2 * TILEHEIGHT), 8)
        elif 1.5 < current_time <= 2:
            self.screen.blit(self.sheet.getImage(1, 0, TILEWIDTH * 2, TILEHEIGHT * 2), (9 * TILEWIDTH, 18 * TILEHEIGHT))
            pygame.draw.circle(self.screen, WHITE, (25 * TILEWIDTH, 19.2 * TILEHEIGHT), 8)
            self.screen.blit(self.sheet.getImage(6, 2, TILEWIDTH * 2, TILEHEIGHT * 2), (TILEWIDTH, 18 * TILEHEIGHT))
        elif 2 < current_time <= 2.5:
            self.screen.blit(self.sheet.getImage(4, 0, TILEWIDTH * 2, TILEHEIGHT * 2), (12 * TILEWIDTH, 18 * TILEHEIGHT))
            pygame.draw.circle(self.screen, WHITE, (25 * TILEWIDTH, 19.2 * TILEHEIGHT), 8)
            self.screen.blit(self.sheet.getImage(6, 2, TILEWIDTH * 2, TILEHEIGHT * 2), (3 * TILEWIDTH, 18 * TILEHEIGHT))
            self.screen.blit(self.sheet.getImage(6, 3, TILEWIDTH * 2, TILEHEIGHT * 2), (TILEWIDTH, 18 * TILEHEIGHT))
        elif 2.5 < current_time <= 3:
            self.screen.blit(self.sheet.getImage(1, 0, TILEWIDTH * 2, TILEHEIGHT * 2), (15 * TILEWIDTH, 18 * TILEHEIGHT))
            pygame.draw.circle(self.screen, WHITE, (25 * TILEWIDTH, 19.2 * TILEHEIGHT), 8)
            self.screen.blit(self.sheet.getImage(6, 2, TILEWIDTH * 2, TILEHEIGHT * 2), (6 * TILEWIDTH, 18 * TILEHEIGHT))
            self.screen.blit(self.sheet.getImage(6, 3, TILEWIDTH * 2, TILEHEIGHT * 2), (3 * TILEWIDTH, 18 * TILEHEIGHT))
            self.screen.blit(self.sheet.getImage(6, 4, TILEWIDTH * 2, TILEHEIGHT * 2), (TILEWIDTH, 18 * TILEHEIGHT))
        elif 3 < current_time <= 3.5:
            self.screen.blit(self.sheet.getImage(1, 1, TILEWIDTH * 2, TILEHEIGHT * 2), (18 * TILEWIDTH, 18 * TILEHEIGHT))
            pygame.draw.circle(self.screen, WHITE, (25 * TILEWIDTH, 19.2 * TILEHEIGHT), 8)
            self.screen.blit(self.sheet.getImage(6, 2, TILEWIDTH * 2, TILEHEIGHT * 2), (9 * TILEWIDTH, 18 * TILEHEIGHT))
            self.screen.blit(self.sheet.getImage(6, 3, TILEWIDTH * 2, TILEHEIGHT * 2), (6 * TILEWIDTH, 18 * TILEHEIGHT))
            self.screen.blit(self.sheet.getImage(6, 4, TILEWIDTH * 2, TILEHEIGHT * 2), (3 * TILEWIDTH, 18 * TILEHEIGHT))
            self.screen.blit(self.sheet.getImage(6, 5, TILEWIDTH * 2, TILEHEIGHT * 2), (TILEWIDTH, 18 * TILEHEIGHT))
        elif 3.5 < current_time <= 4:
            self.screen.blit(self.sheet.getImage(1, 0, TILEWIDTH * 2, TILEHEIGHT * 2), (21 * TILEWIDTH, 18 * TILEHEIGHT))
            pygame.draw.circle(self.screen, WHITE, (25 * TILEWIDTH, 19.2 * TILEHEIGHT), 8)
            self.screen.blit(self.sheet.getImage(6, 2, TILEWIDTH * 2, TILEHEIGHT * 2), (12 * TILEWIDTH, 18 * TILEHEIGHT))
            self.screen.blit(self.sheet.getImage(6, 3, TILEWIDTH * 2, TILEHEIGHT * 2), (9 * TILEWIDTH, 18 * TILEHEIGHT))
            self.screen.blit(self.sheet.getImage(6, 4, TILEWIDTH * 2, TILEHEIGHT * 2), (6 * TILEWIDTH, 18 * TILEHEIGHT))
            self.screen.blit(self.sheet.getImage(6, 5, TILEWIDTH * 2, TILEHEIGHT * 2), (3 * TILEWIDTH, 18 * TILEHEIGHT))
        elif 4 < current_time <= 4.5:
            self.screen.blit(self.sheet.getImage(4, 0, TILEWIDTH * 2, TILEHEIGHT * 2), (23 * TILEWIDTH, 18 * TILEHEIGHT))
            self.screen.blit(self.sheet.getImage(6, 2, TILEWIDTH * 2, TILEHEIGHT * 2), (15 * TILEWIDTH, 18 * TILEHEIGHT))
            self.screen.blit(self.sheet.getImage(6, 3, TILEWIDTH * 2, TILEHEIGHT * 2), (12 * TILEWIDTH, 18 * TILEHEIGHT))
            self.screen.blit(self.sheet.getImage(6, 4, TILEWIDTH * 2, TILEHEIGHT * 2), (9 * TILEWIDTH, 18 * TILEHEIGHT))
            self.screen.blit(self.sheet.getImage(6, 5, TILEWIDTH * 2, TILEHEIGHT * 2), (6 * TILEWIDTH, 18 * TILEHEIGHT))
        elif 4.5 < current_time <= 5:
            self.screen.blit(self.sheet.getImage(0, 0, TILEWIDTH * 2, TILEHEIGHT * 2), (23 * TILEWIDTH, 18 * TILEHEIGHT))
            self.screen.blit(self.sheet.getImage(0, 6, TILEWIDTH * 2, TILEHEIGHT * 2), (15 * TILEWIDTH, 18 * TILEHEIGHT))
            self.screen.blit(self.sheet.getImage(0, 6, TILEWIDTH * 2, TILEHEIGHT * 2), (12 * TILEWIDTH, 18 * TILEHEIGHT))
            self.screen.blit(self.sheet.getImage(0, 6, TILEWIDTH * 2, TILEHEIGHT * 2), (9 * TILEWIDTH, 18 * TILEHEIGHT))
            self.screen.blit(self.sheet.getImage(0, 6, TILEWIDTH * 2, TILEHEIGHT * 2), (6 * TILEWIDTH, 18 * TILEHEIGHT))
        elif 5 < current_time <= 5.5:
            self.screen.blit(self.sheet.getImage(0, 1, TILEWIDTH * 2, TILEHEIGHT * 2), (21 * TILEWIDTH, 18 * TILEHEIGHT))
            self.screen.blit(self.sheet.getImage(2, 6, TILEWIDTH * 2, TILEHEIGHT * 2), (12 * TILEWIDTH, 18 * TILEHEIGHT))
            self.screen.blit(self.sheet.getImage(2, 6, TILEWIDTH * 2, TILEHEIGHT * 2), (9 * TILEWIDTH, 18 * TILEHEIGHT))
            self.screen.blit(self.sheet.getImage(2, 6, TILEWIDTH * 2, TILEHEIGHT * 2), (6 * TILEWIDTH, 18 * TILEHEIGHT))
            self.screen.blit(self.sheet.getImage(2, 6, TILEWIDTH * 2, TILEHEIGHT * 2), (3 * TILEWIDTH, 18 * TILEHEIGHT))
        elif 5.5 < current_time <= 6:
            self.screen.blit(self.sheet.getImage(0, 0, TILEWIDTH * 2, TILEHEIGHT * 2), (18 * TILEWIDTH, 18 * TILEHEIGHT))
            self.screen.blit(self.sheet.getImage(0, 6, TILEWIDTH * 2, TILEHEIGHT * 2), (9 * TILEWIDTH, 18 * TILEHEIGHT))
            self.screen.blit(self.sheet.getImage(0, 6, TILEWIDTH * 2, TILEHEIGHT * 2), (6 * TILEWIDTH, 18 * TILEHEIGHT))
            self.screen.blit(self.sheet.getImage(0, 6, TILEWIDTH * 2, TILEHEIGHT * 2), (3 * TILEWIDTH, 18 * TILEHEIGHT))
            self.screen.blit(self.sheet.getImage(0, 6, TILEWIDTH * 2, TILEHEIGHT * 2), (TILEWIDTH, 18 * TILEHEIGHT))
        elif 6 < current_time <= 6.5:
            self.screen.blit(self.sheet.getImage(4, 0, TILEWIDTH * 2, TILEHEIGHT * 2), (15 * TILEWIDTH, 18 * TILEHEIGHT))
            self.screen.blit(self.sheet.getImage(2, 6, TILEWIDTH * 2, TILEHEIGHT * 2), (6 * TILEWIDTH, 18 * TILEHEIGHT))
            self.screen.blit(self.sheet.getImage(2, 6, TILEWIDTH * 2, TILEHEIGHT * 2), (3 * TILEWIDTH, 18 * TILEHEIGHT))
            self.screen.blit(self.sheet.getImage(2, 6, TILEWIDTH * 2, TILEHEIGHT * 2), (TILEWIDTH, 18 * TILEHEIGHT))
        elif 6.5 < current_time <= 7:
            self.screen.blit(self.sheet.getImage(0, 0, TILEWIDTH * 2, TILEHEIGHT * 2), (12 * TILEWIDTH, 18 * TILEHEIGHT))
            self.screen.blit(self.sheet.getImage(0, 6, TILEWIDTH * 2, TILEHEIGHT * 2), (3 * TILEWIDTH, 18 * TILEHEIGHT))
            self.screen.blit(self.sheet.getImage(0, 6, TILEWIDTH * 2, TILEHEIGHT * 2), (TILEWIDTH, 18 * TILEHEIGHT))
        elif 7 < current_time <= 7.5:
            self.screen.blit(self.sheet.getImage(0, 1, TILEWIDTH * 2, TILEHEIGHT * 2), (9 * TILEWIDTH, 18 * TILEHEIGHT))
            self.screen.blit(self.sheet.getImage(2, 6, TILEWIDTH * 2, TILEHEIGHT * 2), (TILEWIDTH, 18 * TILEHEIGHT))
        elif 7.5 < current_time <= 8:
            self.screen.blit(self.sheet.getImage(0, 0, TILEWIDTH * 2, TILEHEIGHT * 2), (6 * TILEWIDTH, 18 * TILEHEIGHT))
        elif 8 < current_time <= 8.5:
            self.screen.blit(self.sheet.getImage(4, 0, TILEWIDTH * 2, TILEHEIGHT * 2), (3 * TILEWIDTH, 18 * TILEHEIGHT))
        elif 9 < current_time <= 11:
            self.screen.blit(self.sheet.getImage(6, 2, TILEWIDTH * 2, TILEHEIGHT * 2), (21 * TILEWIDTH, 18 * TILEHEIGHT))
            self.screen.blit(pygame.image.load('blinky_text.png'), (10 * TILEWIDTH, 15 * TILEHEIGHT))
        elif 11 < current_time <= 13:
            self.screen.blit(self.sheet.getImage(6, 3, TILEWIDTH * 2, TILEHEIGHT * 2), (21 * TILEWIDTH, 18 * TILEHEIGHT))
            self.screen.blit(pygame.image.load('pinky_text.png'), (10 * TILEWIDTH, 15 * TILEHEIGHT))
        elif 13 < current_time <= 15:
            self.screen.blit(self.sheet.getImage(6, 4, TILEWIDTH * 2, TILEHEIGHT * 2), (21 * TILEWIDTH, 18 * TILEHEIGHT))
            self.screen.blit(pygame.image.load('inkey_text.png'), (10 * TILEWIDTH, 15 * TILEHEIGHT))
        elif 15 < current_time <= 17:
            self.screen.blit(self.sheet.getImage(6, 5, TILEWIDTH * 2, TILEHEIGHT * 2), (21 * TILEWIDTH, 18 * TILEHEIGHT))
            self.screen.blit(pygame.image.load('clyde_text.png'), (10 * TILEWIDTH, 15 * TILEHEIGHT))

    def main(self):
        while True:
            if self.mainScreen:
                self.screen.blit(self.background, (0, 0))
                self.play_button = Button(self.screen, 'Play Game', 10 * TILEWIDTH, 29 * TILEHEIGHT, TILEHEIGHT, 9)
                self.score_button = Button(self.screen, 'High Score', 9.5 * TILEWIDTH, 32 * TILEHEIGHT, TILEHEIGHT, 10)
                self.image = pygame.image.load('pacman_title.png')
                self.screen.blit(self.image, (3 * TILEWIDTH, TILEHEIGHT))
                self.timer = pygame.time.get_ticks() / 1000
                self.main_screen_ani(self.timer)
                self.main_menu_sound.set_volume(0.2)
                self.main_menu_sound.play()
                self.play_button.draw()
                self.score_button.draw()
                self.checkEvents()
            elif self.gameScreen:
                game.startGame()
                self.main_menu_sound.stop()
                self.beginning_sound.set_volume(0.2)
                self.beginning_sound.play()
                while True:
                    game.update()
            elif self.hsScreen:
                self.screen.blit(self.background, (0, 0))
                self.row = 0
                self.font = pygame.font.Font("PressStart2P-vaV7.ttf", TILEHEIGHT)
                with open('high_scores.txt') as f:
                    for line in f:
                        temp_string = self.font.render(line, True, WHITE)
                        self.screen.blit(temp_string, (TILEWIDTH, self.row * TILEHEIGHT))
                self.back_button = Button(self.screen, 'Go back', 20 * TILEWIDTH, 29 * TILEHEIGHT, TILEHEIGHT, 7)
                self.back_button.draw()
                self.checkEvents()

            pygame.display.update()
            self.clock.tick(30)
Exemple #18
0
    def startGame(self):

        self.nodes = NodeGroup("maze.txt")
        self.pacman = Pacman(self.nodes)
        self.ghost = Ghost(self.nodes)
Exemple #19
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 #20
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()
Exemple #21
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 #22
0
    def __init__(self):
        self.nodes = NodeGroup(gridUnit, gridUnit)
        self.nodes.createNodeListFromFile("map.txt")

        # initialize agents
        self.agentRed = Ghost(self.nodes.nodeList[randint(0, 2499)])
        self.agentRed.color = (255, 0, 0)
        #print self.clyde1.id
        self.agentGreen = Ghost(self.nodes.nodeList[randint(0, 2499)])
        self.agentGreen.color = (0, 255, 0)
        #print self.clyde2.id
        self.agentBlue = Ghost(self.nodes.nodeList[randint(0, 2499)])
        self.agentBlue.color = (0, 0, 255)
        #print self.clyde3.id
        self.agentYellow = Ghost(self.nodes.nodeList[randint(0, 2499)])
        self.agentYellow.color = (255, 255, 0)
        #print self.clyde4.id
        self.agentPurple = Ghost(self.nodes.nodeList[randint(0, 2499)])
        self.agentPurple.color = (128, 0, 128)
        #print self.clyde5.id

        # initialize targets
        self.targetRed1 = PacMan(self.nodes.nodeList[randint(0, 2499)],
                                 self.agentRed.id)
        self.targetRed1.color = (240, 128, 128)
        self.targetRed2 = PacMan(self.nodes.nodeList[randint(0, 2499)],
                                 self.agentRed.id)
        self.targetRed2.color = (240, 128, 128)
        self.targetRed3 = PacMan(self.nodes.nodeList[randint(0, 2499)],
                                 self.agentRed.id)
        self.targetRed3.color = (240, 128, 128)
        self.targetRed4 = PacMan(self.nodes.nodeList[randint(0, 2499)],
                                 self.agentRed.id)
        self.targetRed4.color = (240, 128, 128)
        self.targetRed5 = PacMan(self.nodes.nodeList[randint(0, 2499)],
                                 self.agentRed.id)
        self.targetRed5.color = (240, 128, 128)

        self.targetGreen1 = PacMan(self.nodes.nodeList[randint(0, 2499)],
                                   self.agentGreen.id)
        self.targetGreen1.color = (128, 240, 128)
        self.targetGreen2 = PacMan(self.nodes.nodeList[randint(0, 2499)],
                                   self.agentGreen.id)
        self.targetGreen2.color = (128, 240, 128)
        self.targetGreen3 = PacMan(self.nodes.nodeList[randint(0, 2499)],
                                   self.agentGreen.id)
        self.targetGreen3.color = (128, 240, 128)
        self.targetGreen4 = PacMan(self.nodes.nodeList[randint(0, 2499)],
                                   self.agentGreen.id)
        self.targetGreen4.color = (128, 240, 128)
        self.targetGreen5 = PacMan(self.nodes.nodeList[randint(0, 2499)],
                                   self.agentGreen.id)
        self.targetGreen5.color = (128, 240, 128)

        self.targetBlue1 = PacMan(self.nodes.nodeList[randint(0, 2499)],
                                  self.agentBlue.id)
        self.targetBlue1.color = (128, 128, 240)
        self.targetBlue2 = PacMan(self.nodes.nodeList[randint(0, 2499)],
                                  self.agentBlue.id)
        self.targetBlue2.color = (128, 128, 240)
        self.targetBlue3 = PacMan(self.nodes.nodeList[randint(0, 2499)],
                                  self.agentBlue.id)
        self.targetBlue3.color = (128, 128, 240)
        self.targetBlue4 = PacMan(self.nodes.nodeList[randint(0, 2499)],
                                  self.agentBlue.id)
        self.targetBlue4.color = (128, 128, 240)
        self.targetBlue5 = PacMan(self.nodes.nodeList[randint(0, 2499)],
                                  self.agentBlue.id)
        self.targetBlue5.color = (128, 128, 240)

        self.targetYellow1 = PacMan(self.nodes.nodeList[randint(0, 2499)],
                                    self.agentYellow.id)
        self.targetYellow1.color = (240, 240, 128)
        self.targetYellow2 = PacMan(self.nodes.nodeList[randint(0, 2499)],
                                    self.agentYellow.id)
        self.targetYellow2.color = (240, 240, 128)
        self.targetYellow3 = PacMan(self.nodes.nodeList[randint(0, 2499)],
                                    self.agentYellow.id)
        self.targetYellow3.color = (240, 240, 128)
        self.targetYellow4 = PacMan(self.nodes.nodeList[randint(0, 2499)],
                                    self.agentYellow.id)
        self.targetYellow4.color = (240, 240, 128)
        self.targetYellow5 = PacMan(self.nodes.nodeList[randint(0, 2499)],
                                    self.agentYellow.id)
        self.targetYellow5.color = (240, 240, 128)

        self.targetPurple1 = PacMan(self.nodes.nodeList[randint(0, 2499)],
                                    self.agentPurple.id)
        self.targetPurple1.color = (240, 128, 240)
        self.targetPurple2 = PacMan(self.nodes.nodeList[randint(0, 2499)],
                                    self.agentPurple.id)
        self.targetPurple2.color = (240, 128, 240)
        self.targetPurple3 = PacMan(self.nodes.nodeList[randint(0, 2499)],
                                    self.agentPurple.id)
        self.targetPurple3.color = (240, 128, 240)
        self.targetPurple4 = PacMan(self.nodes.nodeList[randint(0, 2499)],
                                    self.agentPurple.id)
        self.targetPurple4.color = (240, 128, 240)
        self.targetPurple5 = PacMan(self.nodes.nodeList[randint(0, 2499)],
                                    self.agentPurple.id)
        self.targetPurple5.color = (240, 128, 240)

        self.Agents = [
            self.agentRed, self.agentGreen, self.agentBlue, self.agentYellow,
            self.agentPurple
        ]

        self.Targets = [
            self.targetRed1, self.targetRed2, self.targetRed3, self.targetRed4,
            self.targetRed5, self.targetGreen1, self.targetGreen2,
            self.targetGreen3, self.targetGreen4, self.targetGreen5,
            self.targetBlue1, self.targetBlue2, self.targetBlue3,
            self.targetBlue4, self.targetBlue5, self.targetYellow1,
            self.targetYellow2, self.targetYellow3, self.targetYellow4,
            self.targetYellow5, self.targetPurple1, self.targetPurple2,
            self.targetPurple3, self.targetPurple4, self.targetPurple5
        ]

        #for target in self.Targets:
        #    print target.owner

        self.checkList = []
Exemple #23
0
class World(object):
    def __init__(self):
        self.nodes = NodeGroup(gridUnit, gridUnit)
        self.nodes.createNodeListFromFile("map.txt")

        # initialize agents
        self.agentRed = Ghost(self.nodes.nodeList[randint(0, 2499)])
        self.agentRed.color = (255, 0, 0)
        #print self.clyde1.id
        self.agentGreen = Ghost(self.nodes.nodeList[randint(0, 2499)])
        self.agentGreen.color = (0, 255, 0)
        #print self.clyde2.id
        self.agentBlue = Ghost(self.nodes.nodeList[randint(0, 2499)])
        self.agentBlue.color = (0, 0, 255)
        #print self.clyde3.id
        self.agentYellow = Ghost(self.nodes.nodeList[randint(0, 2499)])
        self.agentYellow.color = (255, 255, 0)
        #print self.clyde4.id
        self.agentPurple = Ghost(self.nodes.nodeList[randint(0, 2499)])
        self.agentPurple.color = (128, 0, 128)
        #print self.clyde5.id

        # initialize targets
        self.targetRed1 = PacMan(self.nodes.nodeList[randint(0, 2499)],
                                 self.agentRed.id)
        self.targetRed1.color = (240, 128, 128)
        self.targetRed2 = PacMan(self.nodes.nodeList[randint(0, 2499)],
                                 self.agentRed.id)
        self.targetRed2.color = (240, 128, 128)
        self.targetRed3 = PacMan(self.nodes.nodeList[randint(0, 2499)],
                                 self.agentRed.id)
        self.targetRed3.color = (240, 128, 128)
        self.targetRed4 = PacMan(self.nodes.nodeList[randint(0, 2499)],
                                 self.agentRed.id)
        self.targetRed4.color = (240, 128, 128)
        self.targetRed5 = PacMan(self.nodes.nodeList[randint(0, 2499)],
                                 self.agentRed.id)
        self.targetRed5.color = (240, 128, 128)

        self.targetGreen1 = PacMan(self.nodes.nodeList[randint(0, 2499)],
                                   self.agentGreen.id)
        self.targetGreen1.color = (128, 240, 128)
        self.targetGreen2 = PacMan(self.nodes.nodeList[randint(0, 2499)],
                                   self.agentGreen.id)
        self.targetGreen2.color = (128, 240, 128)
        self.targetGreen3 = PacMan(self.nodes.nodeList[randint(0, 2499)],
                                   self.agentGreen.id)
        self.targetGreen3.color = (128, 240, 128)
        self.targetGreen4 = PacMan(self.nodes.nodeList[randint(0, 2499)],
                                   self.agentGreen.id)
        self.targetGreen4.color = (128, 240, 128)
        self.targetGreen5 = PacMan(self.nodes.nodeList[randint(0, 2499)],
                                   self.agentGreen.id)
        self.targetGreen5.color = (128, 240, 128)

        self.targetBlue1 = PacMan(self.nodes.nodeList[randint(0, 2499)],
                                  self.agentBlue.id)
        self.targetBlue1.color = (128, 128, 240)
        self.targetBlue2 = PacMan(self.nodes.nodeList[randint(0, 2499)],
                                  self.agentBlue.id)
        self.targetBlue2.color = (128, 128, 240)
        self.targetBlue3 = PacMan(self.nodes.nodeList[randint(0, 2499)],
                                  self.agentBlue.id)
        self.targetBlue3.color = (128, 128, 240)
        self.targetBlue4 = PacMan(self.nodes.nodeList[randint(0, 2499)],
                                  self.agentBlue.id)
        self.targetBlue4.color = (128, 128, 240)
        self.targetBlue5 = PacMan(self.nodes.nodeList[randint(0, 2499)],
                                  self.agentBlue.id)
        self.targetBlue5.color = (128, 128, 240)

        self.targetYellow1 = PacMan(self.nodes.nodeList[randint(0, 2499)],
                                    self.agentYellow.id)
        self.targetYellow1.color = (240, 240, 128)
        self.targetYellow2 = PacMan(self.nodes.nodeList[randint(0, 2499)],
                                    self.agentYellow.id)
        self.targetYellow2.color = (240, 240, 128)
        self.targetYellow3 = PacMan(self.nodes.nodeList[randint(0, 2499)],
                                    self.agentYellow.id)
        self.targetYellow3.color = (240, 240, 128)
        self.targetYellow4 = PacMan(self.nodes.nodeList[randint(0, 2499)],
                                    self.agentYellow.id)
        self.targetYellow4.color = (240, 240, 128)
        self.targetYellow5 = PacMan(self.nodes.nodeList[randint(0, 2499)],
                                    self.agentYellow.id)
        self.targetYellow5.color = (240, 240, 128)

        self.targetPurple1 = PacMan(self.nodes.nodeList[randint(0, 2499)],
                                    self.agentPurple.id)
        self.targetPurple1.color = (240, 128, 240)
        self.targetPurple2 = PacMan(self.nodes.nodeList[randint(0, 2499)],
                                    self.agentPurple.id)
        self.targetPurple2.color = (240, 128, 240)
        self.targetPurple3 = PacMan(self.nodes.nodeList[randint(0, 2499)],
                                    self.agentPurple.id)
        self.targetPurple3.color = (240, 128, 240)
        self.targetPurple4 = PacMan(self.nodes.nodeList[randint(0, 2499)],
                                    self.agentPurple.id)
        self.targetPurple4.color = (240, 128, 240)
        self.targetPurple5 = PacMan(self.nodes.nodeList[randint(0, 2499)],
                                    self.agentPurple.id)
        self.targetPurple5.color = (240, 128, 240)

        self.Agents = [
            self.agentRed, self.agentGreen, self.agentBlue, self.agentYellow,
            self.agentPurple
        ]

        self.Targets = [
            self.targetRed1, self.targetRed2, self.targetRed3, self.targetRed4,
            self.targetRed5, self.targetGreen1, self.targetGreen2,
            self.targetGreen3, self.targetGreen4, self.targetGreen5,
            self.targetBlue1, self.targetBlue2, self.targetBlue3,
            self.targetBlue4, self.targetBlue5, self.targetYellow1,
            self.targetYellow2, self.targetYellow3, self.targetYellow4,
            self.targetYellow5, self.targetPurple1, self.targetPurple2,
            self.targetPurple3, self.targetPurple4, self.targetPurple5
        ]

        #for target in self.Targets:
        #    print target.owner

        self.checkList = []

    def update(self, time_passed, screen):
        self.checkTargetsAquired()

        for target in self.Targets:
            if target.isInCheckList:
                if target not in self.checkList:
                    self.checkList.append(target)
                    #print self.checkList

            if target.isFound:
                if target in self.checkList:
                    self.checkList.remove(target)
                    #print self.checkList
                self.Targets.remove(target)
            #target.update()

        self.agentRed.update(time_passed, self.Targets, self.checkList)
        self.agentGreen.update(time_passed, self.Targets, self.checkList)
        self.agentBlue.update(time_passed, self.Targets, self.checkList)
        self.agentYellow.update(time_passed, self.Targets, self.checkList)
        self.agentPurple.update(time_passed, self.Targets, self.checkList)

        # render targets
        for target in self.Targets:
            target.render(screen)

        # render agents
        self.agentRed.render(screen)
        self.agentGreen.render(screen)
        self.agentBlue.render(screen)
        self.agentYellow.render(screen)
        self.agentPurple.render(screen)

    def returnNodes(self):
        return self.nodes

    def checkTargetsAquired(self):
        if self.agentRed.targetsFound == 5 \
                and self.agentGreen.targetsFound == 5 \
                and self.agentBlue.targetsFound == 5 \
                and self.agentYellow.targetsFound == 5 \
                and self.agentPurple.targetsFound == 5:
            return True
 def __init__(self, nodes, spritesheet):
     Entity.__init__(self, nodes, spritesheet)
     self.name = "ghost"
     self.image = self.spritesheet.getImage(0, 3, 32, 32)
     self.points = 200
     self.nodee = NodeGroup("maze1.txt")
    def __init__(self):
        self.nodes = NodeGroup(gridUnit, gridUnit)
        self.nodes.getBoardNodes("Field.txt")

        # initialize agents
        self.agentRed = Agent(self.nodes.nodeList[randint(0, 2000)])
        self.agentRed.color = (255, 0, 0)

        self.agentWhite = Agent(self.nodes.nodeList[randint(0, 2000)])
        self.agentWhite.color = (255, 255, 255)

        self.agentTeal = Agent(self.nodes.nodeList[randint(0, 2000)])
        self.agentTeal.color = (0, 255, 255)

        self.agentPurple = Agent(self.nodes.nodeList[randint(0, 2000)])
        self.agentPurple.color = (100, 100, 200)

        self.agentGrey = Agent(self.nodes.nodeList[randint(0, 2000)])
        self.agentGrey.color = (150, 150, 150)

        # initialize targets
        self.targetRed1 = Targets(self.nodes.nodeList[randint(0, 2000)],
                                  self.agentRed.id)
        self.targetRed1.color = (250, 1, 1)

        self.targetRed2 = Targets(self.nodes.nodeList[randint(0, 2000)],
                                  self.agentRed.id)
        self.targetRed2.color = (250, 2, 2)

        self.targetRed3 = Targets(self.nodes.nodeList[randint(0, 2000)],
                                  self.agentRed.id)
        self.targetRed3.color = (250, 3, 3)

        self.targetRed4 = Targets(self.nodes.nodeList[randint(0, 2000)],
                                  self.agentRed.id)
        self.targetRed4.color = (250, 4, 4)

        self.targetRed5 = Targets(self.nodes.nodeList[randint(0, 2000)],
                                  self.agentRed.id)
        self.targetRed5.color = (250, 5, 5)

        self.targetWhite1 = Targets(self.nodes.nodeList[randint(0, 2000)],
                                    self.agentWhite.id)
        self.targetWhite1.color = (255, 255, 255)

        self.targetWhite2 = Targets(self.nodes.nodeList[randint(0, 2000)],
                                    self.agentWhite.id)
        self.targetWhite2.color = (255, 255, 255)

        self.targetWhite3 = Targets(self.nodes.nodeList[randint(0, 2000)],
                                    self.agentWhite.id)
        self.targetWhite3.color = (255, 255, 255)

        self.targetWhite4 = Targets(self.nodes.nodeList[randint(0, 2000)],
                                    self.agentWhite.id)
        self.targetWhite4.color = (255, 255, 255)

        self.targetWhite5 = Targets(self.nodes.nodeList[randint(0, 2000)],
                                    self.agentWhite.id)
        self.targetWhite5.color = (255, 255, 255)

        self.targetTeal1 = Targets(self.nodes.nodeList[randint(0, 2000)],
                                   self.agentTeal.id)
        self.targetTeal1.color = (0, 255, 255)

        self.targetTeal2 = Targets(self.nodes.nodeList[randint(0, 2000)],
                                   self.agentTeal.id)
        self.targetTeal2.color = (0, 255, 255)

        self.targetTeal3 = Targets(self.nodes.nodeList[randint(0, 2000)],
                                   self.agentTeal.id)
        self.targetTeal3.color = (0, 255, 255)

        self.targetTeal4 = Targets(self.nodes.nodeList[randint(0, 2000)],
                                   self.agentTeal.id)
        self.targetTeal4.color = (0, 255, 255)

        self.targetTeal5 = Targets(self.nodes.nodeList[randint(0, 2000)],
                                   self.agentTeal.id)
        self.targetTeal5.color = (0, 255, 255)

        self.targetPurple1 = Targets(self.nodes.nodeList[randint(0, 2000)],
                                     self.agentPurple.id)
        self.targetPurple1.color = (100, 100, 200)

        self.targetPurple2 = Targets(self.nodes.nodeList[randint(0, 2000)],
                                     self.agentPurple.id)
        self.targetPurple2.color = (100, 100, 200)

        self.targetPurple3 = Targets(self.nodes.nodeList[randint(0, 2000)],
                                     self.agentPurple.id)
        self.targetPurple3.color = (100, 100, 200)

        self.targetPurple4 = Targets(self.nodes.nodeList[randint(0, 2000)],
                                     self.agentPurple.id)
        self.targetPurple4.color = (100, 100, 200)

        self.targetPurple5 = Targets(self.nodes.nodeList[randint(0, 2000)],
                                     self.agentPurple.id)
        self.targetPurple5.color = (100, 100, 200)

        self.targetGrey1 = Targets(self.nodes.nodeList[randint(0, 2000)],
                                   self.agentGrey.id)
        self.targetGrey1.color = (150, 150, 150)

        self.targetGrey2 = Targets(self.nodes.nodeList[randint(0, 2000)],
                                   self.agentGrey.id)
        self.targetGrey2.color = (150, 150, 150)

        self.targetGrey3 = Targets(self.nodes.nodeList[randint(0, 2000)],
                                   self.agentGrey.id)
        self.targetGrey3.color = (150, 150, 150)

        self.targetGrey4 = Targets(self.nodes.nodeList[randint(0, 2000)],
                                   self.agentGrey.id)
        self.targetGrey4.color = (150, 150, 150)

        self.targetGrey5 = Targets(self.nodes.nodeList[randint(0, 2000)],
                                   self.agentGrey.id)
        self.targetGrey5.color = (150, 150, 150)

        self.Agents = [
            self.agentRed, self.agentWhite, self.agentTeal, self.agentPurple,
            self.agentGrey
        ]

        self.Targets = [
            self.targetRed1, self.targetRed2, self.targetRed3, self.targetRed4,
            self.targetRed5, self.targetWhite1, self.targetWhite2,
            self.targetWhite3, self.targetWhite4, self.targetWhite5,
            self.targetTeal1, self.targetTeal2, self.targetTeal3,
            self.targetTeal4, self.targetTeal5, self.targetPurple1,
            self.targetPurple2, self.targetPurple3, self.targetPurple4,
            self.targetPurple5, self.targetGrey1, self.targetGrey2,
            self.targetGrey3, self.targetGrey4, self.targetGrey5
        ]

        self.checkList = []
Exemple #26
0
 def startGame(self):
     self.nodes = NodeGroup("maze.txt")
     self.pacman = Pacman(self.nodes)
     self.ghosts = GhostGroup(self.nodes)
     self.gameover = False
class Environment(object):
    def __init__(self):
        self.nodes = NodeGroup(gridUnit, gridUnit)
        self.nodes.getBoardNodes("Field.txt")

        # initialize agents
        self.agentRed = Agent(self.nodes.nodeList[randint(0, 2000)])
        self.agentRed.color = (255, 0, 0)

        self.agentWhite = Agent(self.nodes.nodeList[randint(0, 2000)])
        self.agentWhite.color = (255, 255, 255)

        self.agentTeal = Agent(self.nodes.nodeList[randint(0, 2000)])
        self.agentTeal.color = (0, 255, 255)

        self.agentPurple = Agent(self.nodes.nodeList[randint(0, 2000)])
        self.agentPurple.color = (100, 100, 200)

        self.agentGrey = Agent(self.nodes.nodeList[randint(0, 2000)])
        self.agentGrey.color = (150, 150, 150)

        # initialize targets
        self.targetRed1 = Targets(self.nodes.nodeList[randint(0, 2000)],
                                  self.agentRed.id)
        self.targetRed1.color = (250, 1, 1)

        self.targetRed2 = Targets(self.nodes.nodeList[randint(0, 2000)],
                                  self.agentRed.id)
        self.targetRed2.color = (250, 2, 2)

        self.targetRed3 = Targets(self.nodes.nodeList[randint(0, 2000)],
                                  self.agentRed.id)
        self.targetRed3.color = (250, 3, 3)

        self.targetRed4 = Targets(self.nodes.nodeList[randint(0, 2000)],
                                  self.agentRed.id)
        self.targetRed4.color = (250, 4, 4)

        self.targetRed5 = Targets(self.nodes.nodeList[randint(0, 2000)],
                                  self.agentRed.id)
        self.targetRed5.color = (250, 5, 5)

        self.targetWhite1 = Targets(self.nodes.nodeList[randint(0, 2000)],
                                    self.agentWhite.id)
        self.targetWhite1.color = (255, 255, 255)

        self.targetWhite2 = Targets(self.nodes.nodeList[randint(0, 2000)],
                                    self.agentWhite.id)
        self.targetWhite2.color = (255, 255, 255)

        self.targetWhite3 = Targets(self.nodes.nodeList[randint(0, 2000)],
                                    self.agentWhite.id)
        self.targetWhite3.color = (255, 255, 255)

        self.targetWhite4 = Targets(self.nodes.nodeList[randint(0, 2000)],
                                    self.agentWhite.id)
        self.targetWhite4.color = (255, 255, 255)

        self.targetWhite5 = Targets(self.nodes.nodeList[randint(0, 2000)],
                                    self.agentWhite.id)
        self.targetWhite5.color = (255, 255, 255)

        self.targetTeal1 = Targets(self.nodes.nodeList[randint(0, 2000)],
                                   self.agentTeal.id)
        self.targetTeal1.color = (0, 255, 255)

        self.targetTeal2 = Targets(self.nodes.nodeList[randint(0, 2000)],
                                   self.agentTeal.id)
        self.targetTeal2.color = (0, 255, 255)

        self.targetTeal3 = Targets(self.nodes.nodeList[randint(0, 2000)],
                                   self.agentTeal.id)
        self.targetTeal3.color = (0, 255, 255)

        self.targetTeal4 = Targets(self.nodes.nodeList[randint(0, 2000)],
                                   self.agentTeal.id)
        self.targetTeal4.color = (0, 255, 255)

        self.targetTeal5 = Targets(self.nodes.nodeList[randint(0, 2000)],
                                   self.agentTeal.id)
        self.targetTeal5.color = (0, 255, 255)

        self.targetPurple1 = Targets(self.nodes.nodeList[randint(0, 2000)],
                                     self.agentPurple.id)
        self.targetPurple1.color = (100, 100, 200)

        self.targetPurple2 = Targets(self.nodes.nodeList[randint(0, 2000)],
                                     self.agentPurple.id)
        self.targetPurple2.color = (100, 100, 200)

        self.targetPurple3 = Targets(self.nodes.nodeList[randint(0, 2000)],
                                     self.agentPurple.id)
        self.targetPurple3.color = (100, 100, 200)

        self.targetPurple4 = Targets(self.nodes.nodeList[randint(0, 2000)],
                                     self.agentPurple.id)
        self.targetPurple4.color = (100, 100, 200)

        self.targetPurple5 = Targets(self.nodes.nodeList[randint(0, 2000)],
                                     self.agentPurple.id)
        self.targetPurple5.color = (100, 100, 200)

        self.targetGrey1 = Targets(self.nodes.nodeList[randint(0, 2000)],
                                   self.agentGrey.id)
        self.targetGrey1.color = (150, 150, 150)

        self.targetGrey2 = Targets(self.nodes.nodeList[randint(0, 2000)],
                                   self.agentGrey.id)
        self.targetGrey2.color = (150, 150, 150)

        self.targetGrey3 = Targets(self.nodes.nodeList[randint(0, 2000)],
                                   self.agentGrey.id)
        self.targetGrey3.color = (150, 150, 150)

        self.targetGrey4 = Targets(self.nodes.nodeList[randint(0, 2000)],
                                   self.agentGrey.id)
        self.targetGrey4.color = (150, 150, 150)

        self.targetGrey5 = Targets(self.nodes.nodeList[randint(0, 2000)],
                                   self.agentGrey.id)
        self.targetGrey5.color = (150, 150, 150)

        self.Agents = [
            self.agentRed, self.agentWhite, self.agentTeal, self.agentPurple,
            self.agentGrey
        ]

        self.Targets = [
            self.targetRed1, self.targetRed2, self.targetRed3, self.targetRed4,
            self.targetRed5, self.targetWhite1, self.targetWhite2,
            self.targetWhite3, self.targetWhite4, self.targetWhite5,
            self.targetTeal1, self.targetTeal2, self.targetTeal3,
            self.targetTeal4, self.targetTeal5, self.targetPurple1,
            self.targetPurple2, self.targetPurple3, self.targetPurple4,
            self.targetPurple5, self.targetGrey1, self.targetGrey2,
            self.targetGrey3, self.targetGrey4, self.targetGrey5
        ]

        self.checkList = []

    def update(self, time_passed, screen):
        self.checkScenarioGoal()

        for target in self.Targets:
            if target.isInCheckList:
                if target not in self.checkList:
                    self.checkList.append(target)

            if target.isFound:
                if target in self.checkList:
                    self.checkList.remove(target)
                self.Targets.remove(target)

        self.agentRed.update(time_passed, self.Targets, self.checkList)
        self.agentWhite.update(time_passed, self.Targets, self.checkList)
        self.agentTeal.update(time_passed, self.Targets, self.checkList)
        self.agentPurple.update(time_passed, self.Targets, self.checkList)
        self.agentGrey.update(time_passed, self.Targets, self.checkList)

        for target in self.Targets:
            target.render2(screen)

        #Render agents
        self.agentRed.render(screen)
        self.agentWhite.render(screen)
        self.agentTeal.render(screen)
        self.agentPurple.render(screen)
        self.agentGrey.render(screen)

    def returnNodes(self):
        return self.nodes

    def checkScenarioGoal(self):
        if self.agentRed.targetsFound == 5 \
                or self.agentWhite.targetsFound == 5 \
                or self.agentTeal.targetsFound == 5 \
                or self.agentGrey.targetsFound == 5 \
                or self.agentPurple.targetsFound == 5:
            return True