hp, gold and position visualized
This commit is contained in:
parent
42aec48676
commit
d5b132290c
14
server.py
14
server.py
@ -1,5 +1,6 @@
|
|||||||
from src.agent.map.gameMap import GameMap
|
from src.agent.map.gameMap import GameMap
|
||||||
from mesa.visualization.modules import CanvasGrid
|
from mesa.visualization.modules import CanvasGrid
|
||||||
|
from mesa.visualization.modules.TextVisualization import TextElement
|
||||||
from mesa.visualization.ModularVisualization import ModularServer
|
from mesa.visualization.ModularVisualization import ModularServer
|
||||||
|
|
||||||
|
|
||||||
@ -13,10 +14,19 @@ def player_representation(agent):
|
|||||||
portrayal["Shape"] = 'sprites/goblin.png'
|
portrayal["Shape"] = 'sprites/goblin.png'
|
||||||
return portrayal
|
return portrayal
|
||||||
|
|
||||||
|
|
||||||
grid = CanvasGrid(player_representation, 10, 10, 500, 500)
|
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: {}<br>Current position: {}<br>Player's gold: {}".format(
|
||||||
|
hp, position, gold)
|
||||||
|
|
||||||
server = ModularServer(GameMap,
|
server = ModularServer(GameMap,
|
||||||
[grid],
|
[grid, MyTextElement()],
|
||||||
"Map",
|
"Map",
|
||||||
{"x": 10, "y": 10})
|
{"x": 10, "y": 10})
|
||||||
server.port = 8081
|
server.port = 8081
|
||||||
|
@ -4,7 +4,7 @@ from src.agent.model import *
|
|||||||
from src.agent.state import AgentState
|
from src.agent.state import AgentState
|
||||||
from src.direction import Direction
|
from src.direction import Direction
|
||||||
from src.treesearch.actionsInterpreter import ActionInterpreter
|
from src.treesearch.actionsInterpreter import ActionInterpreter
|
||||||
|
from src.agent.model.dice.dice import roll_the_dice
|
||||||
from src.treesearch.bfs import BFS
|
from src.treesearch.bfs import BFS
|
||||||
|
|
||||||
|
|
||||||
@ -24,6 +24,7 @@ class Player(Creature):
|
|||||||
self.armor = arm
|
self.armor = arm
|
||||||
self.isBox = False
|
self.isBox = False
|
||||||
self.isCreature = False
|
self.isCreature = False
|
||||||
|
self.isPlayer=True
|
||||||
self.direction = Direction.N
|
self.direction = Direction.N
|
||||||
self.queue = []
|
self.queue = []
|
||||||
self.has_goal_chest = False
|
self.has_goal_chest = False
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
from mesa import Model
|
from mesa import Model
|
||||||
|
from mesa.datacollection import DataCollector
|
||||||
|
|
||||||
from src.agent.hero import Player
|
from src.agent.hero import Player
|
||||||
|
|
||||||
from src.agent.model.dice.dice import roll_the_dice
|
from src.agent.model.dice.dice import roll_the_dice
|
||||||
@ -24,20 +26,18 @@ class GameMap(Model):
|
|||||||
self.listOfChests = []
|
self.listOfChests = []
|
||||||
self.grid = MultiGrid(x, y, False)
|
self.grid = MultiGrid(x, y, False)
|
||||||
self.schedule = RandomActivation(self) # agenci losowo po kolei wykonują swoje akcje
|
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.boxes_number = boxes_number
|
||||||
self.creatures_number = creatures_number
|
self.creatures_number = creatures_number
|
||||||
self.running = True
|
self.running = True
|
||||||
# player = Player(1000, self)
|
# player = Player(1000, self)
|
||||||
player = Player(1000, self, "Janusz", 3, 3, 3, 20, 20, WM1, A1, 0, WR1, S1, self.listOfChests)
|
self.player = Player(1000, self, "Janusz", 3, 3, 3, 20, 20, WM1, A1, 0, WR1, S1, self.listOfChests)
|
||||||
self.schedule.add(player)
|
self.schedule.add(self.player)
|
||||||
x = self.random.randrange(self.grid.width)
|
x = self.random.randrange(self.grid.width)
|
||||||
y = self.random.randrange(self.grid.height)
|
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):
|
for i in range(self.boxes_number):
|
||||||
box = Box(i, self)
|
box = Box(i, self)
|
||||||
# self.schedule.add(box)
|
|
||||||
x = self.random.randrange(self.grid.width)
|
x = self.random.randrange(self.grid.width)
|
||||||
y = self.random.randrange(self.grid.height)
|
y = self.random.randrange(self.grid.height)
|
||||||
if self.grid.is_cell_empty((x, y)):
|
if self.grid.is_cell_empty((x, y)):
|
||||||
@ -59,11 +59,19 @@ class GameMap(Model):
|
|||||||
else:
|
else:
|
||||||
pass
|
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):
|
def get_list_of_chests(self):
|
||||||
return self.listOfChests
|
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):
|
def step(self):
|
||||||
self.schedule.step()
|
self.schedule.step()
|
||||||
# self.datacollector.collect(self) #na razie niepotrzebne
|
self.datacollector.collect(self)
|
||||||
|
#print(str(self.datacollector.model_reporters))
|
||||||
|
@ -8,6 +8,7 @@ class Box(Agent):
|
|||||||
self.gold = 3 * roll_the_dice(6)
|
self.gold = 3 * roll_the_dice(6)
|
||||||
self.isBox = True
|
self.isBox = True
|
||||||
self.isCreature = False
|
self.isCreature = False
|
||||||
|
self.isPlayer = False
|
||||||
|
|
||||||
def step(self):
|
def step(self):
|
||||||
pass
|
pass
|
||||||
|
@ -16,6 +16,7 @@ class Creature(Agent):
|
|||||||
self.armor = armor
|
self.armor = armor
|
||||||
self.isBox = False
|
self.isBox = False
|
||||||
self.isCreature = True
|
self.isCreature = True
|
||||||
|
self.isPlayer = False
|
||||||
|
|
||||||
def melee_attack(self, opponent):
|
def melee_attack(self, opponent):
|
||||||
attack_value = self.strength + roll_the_dice(6)
|
attack_value = self.strength + roll_the_dice(6)
|
||||||
|
@ -1,5 +1,4 @@
|
|||||||
import random
|
import random
|
||||||
|
|
||||||
|
|
||||||
def roll_the_dice(number):
|
def roll_the_dice(number):
|
||||||
return random.randint(1, number)
|
return random.randint(1, number)
|
Loading…
Reference in New Issue
Block a user