from src.agent.map.gameMap import GameMap from mesa.visualization.modules import CanvasGrid from mesa.visualization.modules.TextVisualization import TextElement from mesa.visualization.ModularVisualization import ModularServer from src.decisiontree import create_model from src.direction import Direction from collections import defaultdict def player_representation(agent): portrayal = {"Shape": "sprites/heroE.png", "Layer": 1} if agent.isPlayer and agent.direction==Direction.N: portrayal["Shape"] = "sprites/heroN.png" elif agent.isPlayer and agent.direction == Direction.W: portrayal["Shape"] = "sprites/heroW.png" elif agent.isPlayer and agent.direction == Direction.S: portrayal["Shape"] = "sprites/heroS.png" elif agent.isBox: portrayal["Shape"] = "sprites/box.png" portrayal["Layer"] = 0 elif agent.isCreature: if agent.name=='Goblin': portrayal["Shape"] = 'sprites/goblin.png' elif agent.name=='Skeleton': portrayal["Shape"] = 'sprites/skeletonArcher.png' return portrayal def chest_content(agent): if agent.isBox: portrayal = {"Shape": "sprites/coin.jpg", "Layer": 1} return portrayal grid = CanvasGrid(player_representation, 10, 10, 700, 700) image=CanvasGrid(chest_content, 1, 1, 200, 200) class PlayerStats(TextElement): def render(self, model): hp = str(model.get_hp()) position = str(model.get_position()) gold = str(model.get_gold()) return "Player hp: {}
Current position: {}
Player's gold: {}".format( hp, position, gold) # class ChestContent(CanvasGrid): # def render(self,model): # grid_state = defaultdict(list) # portrayal = self.chest_content(agent) # portrayal["x"] = 0 # portrayal["y"] = 0 # grid_state[portrayal["Layer"]].append(portrayal) # return # class FightStats(TextElement): # def render(self, model): # if model.player.combat: # return model.player.describe_situation(model.player.opponent, model.player.strategy) # else: # return "Not fighting" server = ModularServer(GameMap, [grid,PlayerStats()], "Map", {"x": 10, "y": 10}) #create_model() server.port = 8081 server.launch()