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