added agent to the map, error handling of boxes placed at the same square

This commit is contained in:
Joanna Radoła 2021-03-16 15:36:24 +01:00
parent a68d384226
commit ee00c85b80
2 changed files with 16 additions and 6 deletions

View File

@ -1,7 +1,7 @@
from mesa import Model, Agent
from agent import Player
from mesa.time import RandomActivation
from mesa.space import SingleGrid
from mesa.space import MultiGrid
#from mesa.datacollection import DataCollector
import random
@ -14,17 +14,23 @@ boxes_number = 5
class GameMap(Model):
def __init__(self, x, y, boxes_number):
self.grid = SingleGrid(x, y, False)
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
player = Player(1, self)
x = self.random.randrange(self.grid.width)
y = self.random.randrange(self.grid.height)
self.grid.place_agent(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)
self.grid.place_agent(box, (x, y))
player = Player(i, self)
try:
self.grid.place_agent(box, (x, y))
except:
pass
# self.datacollector=DataCollector #informacje o stanie planszy, pozycja agenta
@ -41,6 +47,7 @@ class Box(Agent):
# self.y_coor = random.randrange(1, y + 1)
self.closed = True
self.content = 'unknown'
self.isBox=True
def open(self):
self.closed = False

View File

@ -3,15 +3,18 @@ from mesa.visualization.modules import CanvasGrid
from mesa.visualization.ModularVisualization import ModularServer
def box_representation(box):
def player_representation(agent):
portrayal = {"Shape": "circle",
"Filled": "true",
"Layer": 0,
"Color": "red",
"r": 0.5}
if agent.isBox:
portrayal["Color"] = "grey"
portrayal["Layer"] = 1
return portrayal
grid = CanvasGrid(box_representation, 10, 10, 500, 500)
grid = CanvasGrid(player_representation, 10, 10, 500, 500)
server = ModularServer(GameMap,
[grid],
"Map",