From d5b132290ce1ef9c4147ad458f762a6aa5dfe99d Mon Sep 17 00:00:00 2001 From: tubks Date: Mon, 10 May 2021 20:04:13 +0200 Subject: [PATCH] hp, gold and position visualized --- server.py | 14 ++++++++++++-- src/agent/hero.py | 3 ++- src/agent/map/gameMap.py | 24 ++++++++++++++++-------- src/agent/model/box.py | 1 + src/agent/model/creature.py | 1 + src/agent/model/dice/dice.py | 1 - 6 files changed, 32 insertions(+), 12 deletions(-) diff --git a/server.py b/server.py index ae17886..fbf9583 100644 --- a/server.py +++ b/server.py @@ -1,5 +1,6 @@ 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 @@ -13,10 +14,19 @@ def player_representation(agent): portrayal["Shape"] = 'sprites/goblin.png' return portrayal - grid = CanvasGrid(player_representation, 10, 10, 500, 500) + +class MyTextElement(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) + server = ModularServer(GameMap, - [grid], + [grid, MyTextElement()], "Map", {"x": 10, "y": 10}) server.port = 8081 diff --git a/src/agent/hero.py b/src/agent/hero.py index 06f052d..368c312 100644 --- a/src/agent/hero.py +++ b/src/agent/hero.py @@ -4,7 +4,7 @@ from src.agent.model import * from src.agent.state import AgentState from src.direction import Direction from src.treesearch.actionsInterpreter import ActionInterpreter - +from src.agent.model.dice.dice import roll_the_dice from src.treesearch.bfs import BFS @@ -24,6 +24,7 @@ class Player(Creature): self.armor = arm self.isBox = False self.isCreature = False + self.isPlayer=True self.direction = Direction.N self.queue = [] self.has_goal_chest = False diff --git a/src/agent/map/gameMap.py b/src/agent/map/gameMap.py index 7306d5d..fd5a904 100644 --- a/src/agent/map/gameMap.py +++ b/src/agent/map/gameMap.py @@ -1,4 +1,6 @@ from mesa import Model +from mesa.datacollection import DataCollector + from src.agent.hero import Player from src.agent.model.dice.dice import roll_the_dice @@ -24,20 +26,18 @@ class GameMap(Model): self.listOfChests = [] self.grid = MultiGrid(x, y, False) self.schedule = RandomActivation(self) # agenci losowo po kolei wykonują swoje akcje - # to jest potrzebne przy założeniu, że potwory chodzą? self.boxes_number = boxes_number self.creatures_number = creatures_number self.running = True # player = Player(1000, self) - player = Player(1000, self, "Janusz", 3, 3, 3, 20, 20, WM1, A1, 0, WR1, S1, self.listOfChests) - self.schedule.add(player) + self.player = Player(1000, self, "Janusz", 3, 3, 3, 20, 20, WM1, A1, 0, WR1, S1, self.listOfChests) + self.schedule.add(self.player) x = self.random.randrange(self.grid.width) y = self.random.randrange(self.grid.height) - self.grid.place_agent(player, (x, y)) + self.grid.place_agent(self.player, (x, y)) for i in range(self.boxes_number): box = Box(i, self) - # self.schedule.add(box) x = self.random.randrange(self.grid.width) y = self.random.randrange(self.grid.height) if self.grid.is_cell_empty((x, y)): @@ -59,11 +59,19 @@ class GameMap(Model): else: pass - # self.datacollector=DataCollector #informacje o stanie planszy, pozycja agenta - + self.datacollector=DataCollector(model_reporters={"HP":self.player.health, "Gold":self.player.gold, "Position (x, y)":self.player.pos}) #informacje o stanie planszy, pozycja agenta +#other data: position, strength & other parameters def get_list_of_chests(self): return self.listOfChests + def get_hp(self): + return self.player.health + def get_gold(self): + return self.player.gold + def get_position(self): + return str(self.player.pos)+str(self.player.direction.name) + def step(self): self.schedule.step() - # self.datacollector.collect(self) #na razie niepotrzebne + self.datacollector.collect(self) + #print(str(self.datacollector.model_reporters)) diff --git a/src/agent/model/box.py b/src/agent/model/box.py index d8709f9..6cdeed8 100644 --- a/src/agent/model/box.py +++ b/src/agent/model/box.py @@ -8,6 +8,7 @@ class Box(Agent): self.gold = 3 * roll_the_dice(6) self.isBox = True self.isCreature = False + self.isPlayer = False def step(self): pass diff --git a/src/agent/model/creature.py b/src/agent/model/creature.py index 0c1309b..c6b8d3d 100644 --- a/src/agent/model/creature.py +++ b/src/agent/model/creature.py @@ -16,6 +16,7 @@ class Creature(Agent): self.armor = armor self.isBox = False self.isCreature = True + self.isPlayer = False def melee_attack(self, opponent): attack_value = self.strength + roll_the_dice(6) diff --git a/src/agent/model/dice/dice.py b/src/agent/model/dice/dice.py index 642e2fa..a4afbcd 100644 --- a/src/agent/model/dice/dice.py +++ b/src/agent/model/dice/dice.py @@ -1,5 +1,4 @@ import random - def roll_the_dice(number): return random.randint(1, number) \ No newline at end of file