def exitGame(): """ exitGame() -> The game is exited """ AgentServer.get().stopAll() pygame.quit() exit(0)
def buildLevelObject(self, levelRepresentation): level = levelRepresentation['level'] class_map = levelRepresentation['class_map'] values = levelRepresentation['values'] levelRepresentation['config_ai'](values) # this is a callable object #print(levelRepresentation['level'].zone_coordinates()) levelRepresentation['built_objects'] = level.build_objects(class_map, values) AgentServer.get().configure(level.conn_map()) return levelRepresentation
def level2AI(self, values): """ Adds AI cores in order to build level objects This also configures the AI core layout """ AI_server = AgentServer.get() values['e']['agent'] = AI_server.newAgent(2) values['r']['agent'] = AI_server.newAgent(2) values['j']['agent'] = AI_server.newFakeAgent()
def clearCurrentLevel(self): AI_server = AgentServer.get() AI_server.stopAll() AI_server.clear() # Include Tile class to the objects in the game so that it is clered # when new level is loaded tiles = {'tile': Tile} allLevelObjects = self.levels[self.currentLevel]['class_map'] allObjects = {k: v for k, v in itertools.chain(allLevelObjects.items(), tiles.items())} for key, value in allObjects.items(): value.clear()
def start(self, screen2): """ Method that initializes the game and the corresponding pieces of the game """ # Whether to show the pause Menu menuShow = False # Whether to continue the level levelContinue = True # Whether the game continues gameNotEnd = True # Inialize the pause menu pauseMenu = Menu() clock = pygame.time.Clock() # Initialize Game Clock total_frames = 0 level = 1 # Game Start while gameNotEnd: # Treasure taken taken = False if self.soundOptions['game_music'] and not taken: self.playMainThemeMusic() # premature tile creation to preserve tile invariant # invariant: relation between tile number and tile position for y in range(0, self.screen.get_height(), 16): for x in range(0, self.screen.get_width(), 16): Tile(x, y, 'empty') # Loads the initial level representation currentLevelList = LevelList(self.width, self.height, self.screen) try: #if game state must jump if not self.loadgame: currentLevel = currentLevelList.buildLevelObject(currentLevelList.levels[level]) else: currentLevel = GameOption.loadGame(currentLevelList) level = currentLevel['levelIndex'] currentLevelList.currentLevel = level self.initializeLoadedGame(currentLevel) winCoordinates = currentLevel['level'].coordinates(('-',))['-'] # make unbuildable and unwalkable objects unwalkable (walls) for y in range(0, self.screen.get_height(), 16): for x in range(0, self.screen.get_width(), 16): if (x, y) in currentLevel['unwalkable']: tile_number = ((x / 16) + Tile.HorizontalDifference) + ( (y / 16) * Tile.VerticalDifference) tile = Tile.get_tile(tile_number) tile.walkable = False tile.type = 'solid' mainCharacter = currentLevel['built_objects']['j'][0] enemy = currentLevel['built_objects']['e'][0] if mainCharacter.treasureCaptured or enemy.treasureCaptured: self.pauseMainThemeMusic() self.playObjectTakenMusic() background = currentLevel['level'].build_static_background(currentLevel['tile_map'], default='.') interaction = Interaction(self.screen, self.FPS, currentLevel) # The server must be configured at this point AI_server = AgentServer.get() AI_server.startAll() levelContinue = False # Game Loop while not levelContinue: if not menuShow: # blit the background self.screen.blit(background, (0, 0)) Message.text_to_screen(self.screen, 'Health: {0}'.format(mainCharacter.health),0, -1) # show general game information if interaction.isUserCallingHelpScreen(): Message.showGeneralGameInformation(self.screen, interaction.controlDefinition) else: Message.showGeneralGameInformation(self.screen, interaction.helpButton) Laser.charactersShotDamageHandler(self.screen) mainCharacter.movement(self.screen) enemy.movement(self.screen) interaction.interactionHandler() menuShow = interaction.isUserCallingGameMenu() if menuShow: self.pauseMainThemeMusic() # apply interaction of all AI cores AI_server.next() if mainCharacter.treasureCaptured or enemy.treasureCaptured: if not taken: taken = True self.pauseMainThemeMusic() self.playObjectTakenMusic() else: taken = False if mainCharacter.satifiesWinConditions(winCoordinates): currentLevelList.clearCurrentLevel() levelContinue = True level += 1 currentLevelList.currentLevel += 1 self.loadgame = False if self.soundOptions['game_music']: self.restartMainThemeMusic() # Drawing of the Characters in the currentLevel for gameObject in currentLevel['class_map'].values(): gameObject.draw(self.screen) else: selections = pauseMenu.show_menu(screen2, self.FPS, "pauseMenu") if selections['exit_game'] is True: GameOption.exitGame() elif selections['resume_game'] is True: menuShow = False self.resumeMainThemeMusic() interaction.showGameMenu = False elif selections['save_game'] is True: GameOption.saveGame(currentLevel) print('Game Saved') screen2.blit(pygame.transform.scale( self.screen, screen2.get_rect().size), (0, 0)) pygame.display.flip() clock.tick(self.FPS) total_frames += 1 except KeyError: # Max Levels reached self.restartMainThemeMusic() self.playWinMusic() currentLevelList.clearCurrentLevel() gameNotEnd = False self.screen.blit(pygame.image.load("img/gamewon.png"), (0, 0)) screen2.blit(pygame.transform.scale( self.screen, screen2.get_rect().size), (0, 0)) pygame.display.flip() pygame.time.delay(3500) clock.tick(self.FPS) total_frames += 1 GameOption.exitGame()