Exemplo n.º 1
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()
Exemplo n.º 2
0
class GameController(object):
    def __init__(self):
        pygame.init()
        pygame.mixer.music.load('sound/pacman_beginning.wav')
        pygame.mixer.music.play(-1)
        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.level = LevelController()
        self.text = TextGroup()
        self.score = 0
        self.gameover = False
        self.sheet = Spritesheet()
        self.maze = Maze(self.sheet)
        self.flashBackground = False
        self.eat_sound = pygame.mixer.Sound("sound/pacman_chomp.wav")
        self.music_sound = pygame.mixer.Sound("sound/pacman_beginning.wav")
        self.death_sound = pygame.mixer.Sound("sound/pacman_death.wav")
        self.eatghost_sound = pygame.mixer.Sound("sound/pacman_eatghost.wav")
        self.eatfruit_sound = pygame.mixer.Sound("sound/pacman_eatfruit.wav")
        self.intermission_sound = pygame.mixer.Sound(
            "sound/pacman_intermission.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()
        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.nodes, self.sheet)
        self.ghosts = GhostGroup(self.nodes, self.sheet)
        self.pelletsEaten = 0
        self.fruit = None
        self.pause.force(True)
        self.text.showReady()
        self.text.updateLevel(self.level.level + 1)
        self.gameover = False
        self.maze.reset()
        self.flashBackground = False

    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()

    def restartLevel(self):
        print("Restart current level")
        self.pacman.reset()
        self.ghosts = GhostGroup(self.nodes, self.sheet)
        self.pause.force(True)
        self.fruit = None
        self.flashBackground = False
        self.maze.reset()

    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.pause.pauseType != None:
                    self.pause.settlePause(self)

                self.checkPelletEvents()
                self.checkGhostEvents()
                self.checkFruitEvents()

            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:
                exit()
            elif event.type == KEYDOWN:
                if event.key == K_SPACE:
                    if self.gameover:
                        self.startGame()
                    else:
                        self.pause.player()
                        if self.pause.paused:
                            pygame.mixer.music.pause()

                            self.text.showPause()
                        else:
                            self.text.hideMessages()
                            pygame.mixer.music.unpause()

    def checkPelletEvents(self):
        pellet = self.pacman.eatPellets(self.pellets.pelletList)
        if pellet:
            pygame.mixer.music.pause()
            pygame.mixer.Sound.play(self.eat_sound)
            pygame.mixer.music.unpause()
            self.pelletsEaten += 1
            self.score += pellet.points
            if (self.pelletsEaten == 70 or self.pelletsEaten == 140):
                if self.fruit is None:
                    levelmap = self.level.getLevel()
                    self.fruit = Fruit(self.nodes, self.sheet,
                                       levelmap["fruit"])
            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":
                pygame.mixer.music.pause()
                pygame.mixer.Sound.play(self.eatghost_sound)
                pygame.mixer.music.unpause()
                self.ghosts.updatePoints()
                ghost.spawnMode(speed=2)
                self.pause.startTimer(1)
                self.pacman.visible = False
                ghost.visible = False

            elif ghost.mode.name != "SPAWN":
                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):
                pygame.mixer.music.pause()
                pygame.mixer.Sound.play(self.eatfruit_sound)
                pygame.mixer.music.unpause()
                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 resolveDeath(self):
        if self.pacman.lives == 0:
            self.gameover = True
            self.pacman.visible = False
            self.text.showGameOver()
            #pygame.mixer.music.pause()
            #pygame.mixer.Sound.play(self.death_sound)
            pygame.mixer.music.stop()

        else:
            pygame.mixer.music.pause()
            pygame.mixer.Sound.play(self.death_sound)
            pygame.mixer.music.unpause()
            self.restartLevel()
        self.pause.pauseType = None

    def resolveLevelClear(self):
        pygame.mixer.music.pause()
        self.level.nextLevel()
        self.startLevel()
        pygame.mixer.music.unpause()
        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)
        self.pacman.render(self.screen)
        self.ghosts.render(self.screen)
        self.pacman.renderLives(self.screen)
        self.text.render(self.screen)
        pygame.display.update()
Exemplo n.º 3
0
class GameController(object):
    def __init__(self):
        pygame.init()
        self.screen = pygame.display.set_mode(SCREENSIZE, 0, 32)
        self.background = None
        self.level = 0
        self.setBackGround()
        self.clock = pygame.time.Clock()
        self.score = 0
        self.ghostScore = 200
        self.lives = 5
        self.idleTimer = 0
        self.displayedLevel = 0
        self.maxLevels = 2
        self.displayedFruits = []
        self.sheet = SpriteSheet()
        self.lifeIcons = Lives(self.sheet)
        self.maze = Maze(self.level, self.sheet)
        self.maze.fillMaze(self.background)
        self.allText = TextGroup()
        self.allText.add("hi_score_label", "HI SCORE", align="left")
        self.allText.add("score_label", "SCORE", align="center")
        self.allText.add("level_label", "LEVEL", align="right")
        self.allText.add("score", self.score, y=HEIGHT, align="center")
        self.allText.add("level",
                         self.displayedLevel,
                         y=2 * HEIGHT,
                         align="right")
        self.levelFlashTime = 0.3
        self.levelFlashTimer = 0
        self.drawBackgroundFlash = False
        self.flashLevel = False
        self.fruitScoreTimer = 0
        self.fruitEaten = False

    def setBackGround(self):
        self.background = pygame.surface.Surface(SCREENSIZE).convert()
        self.background.fill(BLACK)
        self.backgroundFlash = pygame.surface.Surface(SCREENSIZE).convert()
        self.backgroundFlash.fill(BLACK)

    def commonSetup(self):
        self.pacman = Pacman(self.nodes, self.level, self.sheet)
        self.ghosts = GhostGroup(self.nodes, self.level, self.sheet)
        self.paused = True
        self.fruit = None
        self.fruitTimer = 0
        self.pauseTimer = 0
        self.pauseTime = 5
        self.playerPaused = False
        self.startDelay = False
        self.restartDelay = False
        self.scoreAccumulator = 0
        self.maxLives = 5
        self.allText.add("start_label",
                         "BEGIN",
                         y=20 * HEIGHT,
                         align="center",
                         color=YELLOW)
        self.flashLevel = False
        self.drawBackgroundFlash = False
        self.levelFlashTimer = 0
        self.fruitScoreTimer = 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 restartLevel(self):
        self.commonSetup()

    def update(self):
        dt = self.clock.tick(30) / 1000.0
        if not self.paused:
            self.pacman.update(dt)
            self.ghosts.update(dt, self.pacman)
            self.checkPelletEvents(dt)
            self.checkGhostEvents(dt)
            self.checkFruitEvents(dt)
            self.applyScore()
        else:
            if not self.playerPaused:
                if not self.pacman.alive:
                    self.pacman.update(dt)
                    if self.pacman.deathSequenceFinished:
                        if self.lives > 0:
                            self.restartLevel()
                        else:
                            self.allText.add("game_over_label",
                                             "GAME OVER",
                                             y=20 * HEIGHT,
                                             align="center",
                                             color=RED)

                else:
                    self.pauseTimer += dt
                    if self.pauseTimer >= self.pauseTime:
                        self.paused = False
                        self.allText.remove("ghost_score")
                        self.allText.remove("start_label")
                        self.pacman.draw = True
                        for ghost in self.ghosts:
                            ghost.draw = True
                        if self.startDelay == True:
                            self.startGame()
                        if self.restartDelay == True:
                            self.restartLevel()

        if self.flashLevel:
            self.levelFlashTimer += dt
            if self.levelFlashTimer >= self.levelFlashTime:
                self.levelFlashTimer = 0
                self.drawBackgroundFlash = not self.drawBackgroundFlash

        if self.fruitEaten:
            self.fruitScoreTimer += dt
            if self.fruitScoreTimer >= self.fruitScoreTime:
                self.allText.remove("fruit_score")
                self.fruitScoreTimer = 0
                self.fruitEaten = False

        self.checkEvents()
        self.render()

    def checkEvents(self):
        for event in pygame.event.get():
            if event.type == QUIT:
                exit()
            if event.type == KEYDOWN:
                if event.key == K_SPACE:
                    self.allText.remove("start_label")
                    self.allText.remove("fruit_score")
                    if self.paused:
                        self.playerPaused = False
                        self.allText.remove("paused_label")
                    else:
                        self.playerPaused = True
                        self.allText.add("paused_label",
                                         "PAUSED",
                                         y=20 * HEIGHT,
                                         align="center",
                                         color=GREEN)
                    self.paused = not self.paused

    def checkPelletEvents(self, dt):
        pellet = self.pacman.eatPellets(self.pellets.pelletList)
        if pellet:
            self.pellets.numEaten += 1
            if (self.pellets.numEaten == 70 or self.pellets.numEaten == 140):
                self.fruit = CollectedFruit(self.nodes, self.level,
                                            self.displayedLevel, self.sheet)
            self.idleTimer = 0
            self.scoreAccumulator += pellet.value
            self.pellets.pelletList.remove(pellet)
            if pellet.name == "powerpellet":
                self.pellets.powerPellets.remove(pellet)
                self.ghosts.setFreightMode()
                self.ghostScore = 200

            if (self.ghosts.anyInFreightOrSpawn()):
                self.pacman.boostSpeed()
            else:
                self.pacman.reduceSpeed()

            if self.pellets.isEmpty():
                self.paused = True
                self.pauseTime = 3
                self.pauseTimer = 0
                self.startDelay = True
                self.flashLevel = True
                self.increaseLevel()

        else:
            self.idleTimer += dt
            if (self.ghosts.anyInFreightOrSpawn()):
                self.pacman.boostSpeed()
            else:
                if self.idleTimer >= 0.5:
                    self.pacman.normalSpeed()

        for pellet in self.pellets.powerPellets:
            pellet.update(dt)

    def checkGhostEvents(self, dt):
        ghost = self.pacman.eatGhost(self.ghosts)
        if ghost is not None:
            if ghost.mode.name == "FREIGHT":
                self.allText.add("ghost_score",
                                 self.ghostScore,
                                 x=ghost.position.x,
                                 y=ghost.position.y,
                                 size=0.5)

                ghost.setRespawnMode()
                self.scoreAccumulator += self.ghostScore
                self.ghostScore *= 2
                self.paused = True
                self.pauseTime = 0.5
                self.pauseTimer = 0
                self.pacman.draw = False
                ghost.draw = False
            elif ghost.mode.name != "SPAWN":
                self.paused = True
                #self.pauseTime = 1
                self.pauseTimer = 0
                self.restartDelay = True
                self.lives -= 1
                if self.lives == 0:
                    self.lives = 5
                    self.startDelay = True
                self.pacman.alive = False
                self.ghosts.noDraw()
                #ghost.draw = False
                self.pacman.animate.setAnimation("death", 0)

        if self.pellets.numEaten >= 30 or self.idleTimer >= 10:
            self.ghosts.release("inky")
        if self.pellets.numEaten >= 60 or self.idleTimer >= 10:
            self.ghosts.release("clyde")

    def checkFruitEvents(self, dt):
        if self.fruit is not None:
            if self.pacman.eatFruit(self.fruit):
                self.allText.add("fruit_score",
                                 self.fruit.value,
                                 x=self.fruit.position.x,
                                 y=self.fruit.position.y,
                                 size=0.5)
                self.scoreAccumulator += self.fruit.value
                self.addDisplayedFruit()
                self.fruitTimer = 0
                self.fruit = None
                self.fruitScoreTime = 1
                self.fruitScoreTimer = 0
                self.fruitEaten = True
            else:
                self.fruitTimer += dt
                if self.fruitTimer >= 10:
                    self.fruitTimer = 0
                    self.fruit = None

    def applyScore(self):
        if self.scoreAccumulator > 0:
            newScore = self.score + self.scoreAccumulator
            if ((newScore % 10000 - self.score % 10000) < 0
                    or newScore - self.score >= 10000):
                if self.lives < self.maxLives:
                    self.lives += 1
            self.score += self.scoreAccumulator
            self.scoreAccumulator = 0
            self.allText.remove("score")
            self.score = newScore
            self.allText.add("score", self.score, y=HEIGHT, align="center")

    def addDisplayedFruit(self):
        fruitNames = [n.name for n in self.displayedFruits]
        if self.fruit.name not in fruitNames:
            fruit = DisplayedFruit(self.fruit)
            fruit.setPosition(len(self.displayedFruits))
            self.displayedFruits.append(fruit)

    def increaseLevel(self):
        self.level += 1
        self.displayedLevel += 1
        self.level %= self.maxLevels

    def render(self):

        if self.drawBackgroundFlash:
            self.screen.blit(self.backgroundFlash, (0, 0))
        else:
            self.screen.blit(self.background, (0, 0))

        #self.nodes.render(self.screen)
        self.pellets.render(self.screen)

        for fruit in self.displayedFruits:
            fruit.render(self.screen)

        if not self.playerPaused:
            if self.fruit is not None:
                self.fruit.render(self.screen)
            self.ghosts.render(self.screen)
            self.pacman.render(self.screen)

        self.lifeIcons.render(self.screen, self.lives - 1)
        self.allText.render(self.screen)

        pygame.display.update()
Exemplo n.º 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()
Exemplo n.º 5
0
        pacman.reset()
        ghosts.reset()
        pellets.numEaten = 0
        
    if control.checkForEndGame(pellets):
        pacman.reset()
        ghosts.reset()
        pellets.reset()
        
    if control.startGame(dt):
        control.checkForRelease(ghosts.members, pellets.numEaten)
        powerEaten = pellets.checkCollided(pacman)
        ghosts.modeUpdate(dt, powerEaten)

        #control.updateSpeed(pacman, ghosts) #based on mode or other factors
        pacman.update(dt)
        ghosts.setGoal(pacman) #Should be a part of the ghosts mode update?
        ghosts.update(dt)
        ghosts.checkPacmanCollide(pacman)

    screen.blit(background, (0,0))
    nodes.render(screen)
    pellets.render(screen)
    pacman.render(screen)
    ghosts.render(screen)
    pygame.display.update()
            
#game = Game()
#game.run()

Exemplo n.º 6
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.sheet = Spritesheet()
        self.maze = Maze(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 = False
        self.pac_pellet = 0

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

    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()

    def update(self):
        dt = self.clock.tick(30) / 1000.0
        if self.gameover == 3:
            self.pacman.update(dt)
            self.ghost.update(dt)
            self.orange_ghost.update(dt)
            self.blue_ghost.update(dt)
            self.pink_ghost.update(dt)
            self.pellets.update(dt)

            self.checkPelletEvents()
            self.checkEvents()
            self.text.updateScore(self.score)
            if pygame.time.get_ticks() >= self.pp_time + 7000:
                self.pacman.speed = 100

            pacman = self.pacman.eatGhost(self.ghost)
            if pacman:
                if int(time.time()) > int(self.start_time) + 1:
                    if self.pac_pellet == 1:
                        self.text.Pac_win('PACMAN WINS')
                        self.gameover = 0
                    else:
                        self.text.Pac_win('YOU WIN')
                        self.gameover = 1

        elif self.gameover == 0 or self.gameover == 1:
            time.sleep(0.1)
            event = pygame.event.wait()
            if event.type == QUIT:
                exit()
            elif event.type == KEYDOWN:
                self.startGame()
        self.render()

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

    def checkPelletEvents(self):
        pellet = self.pacman.eatPellets(self.pellets.pelletList)
        if pellet:
            self.pelletsEaten += 1
            self.pellets.pelletList.remove(pellet)
            if pellet.name == "powerpellet":
                self.pacman.speed = 150
                self.pp_time = pygame.time.get_ticks()

        pacman_pellet = self.ghost.eatPellets(self.pellets.pelletList)
        runs = 0
        if pacman_pellet:
            if pacman_pellet.name == 'powerpellet':
                if runs == 0:
                    runs = runs + 1
                    self.pellets.pelletList.remove(pacman_pellet)
                    self.pac_pellet = 1
                    self.time_eat = pygame.time.get_ticks()
            else:
                self.pellets.pelletList.remove(pacman_pellet)
            self.score += pacman_pellet.points
        if pygame.time.get_ticks() > self.time_eat + 7000:
            self.pac_pellet = 0
            runs = 0

    def render(self):
        self.screen.blit(self.background, (0, 0))
        self.pellets.render(self.screen)

        self.orange_ghost.render(self.screen)
        self.blue_ghost.render(self.screen)
        self.pink_ghost.render(self.screen)
        self.text.render(self.screen)
        self.pacman.render(self.screen)
        self.ghost.render(self.screen)
        pygame.display.update()
Exemplo n.º 7
0
class GameController(object):
    #general activation
    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.pelletsEaten = 0
        self.fruit = None
        self.pause = Pauser(True)
        self.level = LevelController()
        self.text = TextGroup()
        self.sheet = Spritesheet()
        self.maze = Maze(self.sheet)

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

    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
        #self.text.showReady()    #Removed, so that we don't have Ready permanently on screen
        #self.text.updateLevel(self.level.level+1)  #Removed to start same level each time

    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)

    def restartLevel(self):
        #Restarts the game.
        self.pacman.reset()
        #self.ghosts = GhostGroup(self.nodes, self.sheet) Ghost command, removed to get rid of ghosts
        self.fruit = None
        self.pause.force(True)
        self.text.showReady()

    def update(self):
        #Where the game instances are run

        if not self.gameover:
            dt = self.clock.tick(300) / 300.0
            # CHANGE TIME  (   clock.tick( refresh rate )   / speed (lower number, higher speed)
            if not self.pause.paused:
                self.pacman.update(dt)
                #self.ghosts.update(dt, self.pacman)   Stopper spøgelser med at opdaterer
                if self.fruit is not None:
                    self.fruit.update(dt)
                if self.pause.pauseType != None:
                    self.pause.settlePause(self)
                self.checkPelletEvents()
                #self.checkGhostEvents()   Ghost command, removed to get rid of ghosts
                self.checkFruitEvents()

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

    def checkEvents(self):
        #Simple commands while in game

        for event in pygame.event.get():
            if event.type == QUIT:
                exit()
            elif self.gameover:
                self.startGame()
            #Spacebar is removed, and instead game just restarts automatically

            #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()

    def checkPelletEvents(self):
        #This section is in charge of checking if Pacman is on a pellet.

        pellet = self.pacman.eatPellets(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":
                pass
                #self.ghosts.resetPoints()   Ghost command, removed to get rid of ghosts
                #self.ghosts.freightMode()   Ghost command, removed to get rid of ghosts
            if self.pellets.isEmpty():
                self.pacman.visible = False
                #self.ghosts.hide()    Ghost command, removed to get rid of ghosts
                self.pause.startTimer(
                    0.1, "clear"
                )  #Used to be 3, has been set to 0.1. No reset timer.

    def checkGhostEvents(self):
        #In charge of ghosts

        self.ghosts.release(self.pelletsEaten)
        ghost = self.pacman.eatGhost(self.ghosts)
        if ghost is not None:
            if ghost.mode.name == "FREIGHT":
                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 == "CHASE" or ghost.mode.name == "SCATTER":
                self.pacman.loseLife()
                self.ghosts.hide()
                self.pause.startTimer(
                    0.1, "die")  #Death timer, set to 0 to optimize time.

    def checkFruitEvents(self):
        #basically in charge of fruits, in same way as above.

        if self.fruit is not None:
            if self.pacman.eatFruit(self.fruit):
                self.score += self.fruit.points
                self.text.createTemp(self.fruit.points, self.fruit.position)
                self.fruit = None
            elif self.fruit.destroy:
                self.fruit = None
            #if self.pacman.eatFruit(self.fruit) or self.fruit.destroy:
            #self.fruit = None

    def resolveDeath(self):
        #Checks if the game needs to end by keeping track of death tol

        if self.pacman.lives == 0:
            self.gameover = False  #This should be true, but has been changed to remove lives from the game
        else:
            self.restartLevel()
        self.pause.pauseType = None

    def resolveLevelClear(self):

        #self.level.nextLevel()
        #self.startLevel()

        self.startGame()
        self.pause.pauseType = None

    def render(self):
        #In charge of rendering all characters, ghosts, pellets, ghosts etc...

        self.screen.blit(self.background, (0, 0))
        #self.nodes.render(self.screen)  (Not my doing)
        self.pellets.render(self.screen)
        if self.fruit is not None:
            self.fruit.render(self.screen)
        self.pacman.render(self.screen)
        #self.ghosts.render(self.screen)    Ghost command, removed to get rid of ghosts
        self.pacman.renderLives(self.screen)
        self.text.render(self.screen)
        pygame.display.update()
Exemplo n.º 8
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)
class Game:
    def __init__(self):
        pygame.init()
        self.screen = pygame.display.set_mode(SCREENSIZE, 0, 1000)
        self.background = None
        self.background_flash = None
        self.setBackground()
        self.clock = pygame.time.Clock()
        self.pelletsEaten = 0
        self.fruit = None

        self.sound = Sound(sound="sounds/pmanpellets.wav")
        self.sound.play()
        self.sound.pause_bg()

        # self.play_button, self.quit_button = None

        self.pause = Pauser(True)
        self.level = LevelController()
        self.text = TextGroup()
        self.score = 0
        self.gameover = False
        self.sheet = Spritesheet()
        self.maze = Maze(self.sheet)
        self.flashBackground = False

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

    # def show_menu(self):
    #     self.play_button = Button(screen=self.screen, msg="Play", x=800, y=500)
    #     self.quit_button = Button(screen=self.screen, msg="Quit", x=800, y=700)

    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

            # if game_state is ts.GameState.QUIT:
            #     exit()

    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.showReady()
        self.sound.intro()
        self.text.updateLevel(self.level.level + 1)
        self.flashBackground = False
        self.maze.reset()

    def restartLevel(self):
        print("Restart current level")
        self.pacman.reset()
        self.ghosts = GhostGroup(self.nodes, self.sheet)
        self.pause.force(True)
        self.text.showReady()
        self.fruit = None
        self.flashBackground = False
        self.maze.reset()

    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.pause.pauseType != None:
                    self.pause.settlePause(self)

                self.checkPelletEvents()
                self.checkGhostEvents()
                self.checkFruitEvents()

            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:
                exit()
            elif event.type == KEYDOWN:
                if event.key == K_SPACE:
                    if self.gameover:
                        self.play()
                    else:
                        self.pause.player()
                        if self.pause.paused:
                            self.text.showPause()
                        else:
                            self.text.hideMessages()

    def checkPelletEvents(self):
        pellet = self.pacman.eatPellets(self.pellets.pelletList)
        if pellet:
            self.sound.eat_pellets()
            self.pelletsEaten += 1
            self.score += pellet.points
            if (self.pelletsEaten == 70 or self.pelletsEaten == 140):
                if self.fruit is None:
                    levelmap = self.level.getLevel()
                    self.fruit = Fruit(self.nodes, self.sheet, levelmap["fruit"])
            self.pellets.pelletList.remove(pellet)
            if pellet.name == "powerpellet":
                self.ghosts.resetPoints()
                self.ghosts.freightMode()
                self.sound.strong_pac()
            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.sound.eat_ghosts()
                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.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.sound.eat_fruits()
                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 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 resolveLevelClear(self):
        self.level.nextLevel()
        self.startLevel()
        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)
        self.pacman.render(self.screen)
        self.ghosts.render(self.screen)
        self.pacman.renderLives(self.screen)
        self.text.render(self.screen)
        pygame.display.update()