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 mesa import Model, Agent
from agent import Player from agent import Player
from mesa.time import RandomActivation from mesa.time import RandomActivation
from mesa.space import SingleGrid from mesa.space import MultiGrid
#from mesa.datacollection import DataCollector #from mesa.datacollection import DataCollector
import random import random
@ -14,17 +14,23 @@ boxes_number = 5
class GameMap(Model): class GameMap(Model):
def __init__(self, x, y, boxes_number): 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 self.schedule = RandomActivation(self) # agenci losowo po kolei wykonują swoje akcje
# to jest potrzebne przy założeniu, że potwory chodzą? # to jest potrzebne przy założeniu, że potwory chodzą?
self.boxes_number = boxes_number 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): for i in range(self.boxes_number):
box = Box(i, self) box = Box(i, self)
self.schedule.add(box) 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)
try:
self.grid.place_agent(box, (x, y)) self.grid.place_agent(box, (x, y))
player = Player(i, self) except:
pass
# self.datacollector=DataCollector #informacje o stanie planszy, pozycja agenta # 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.y_coor = random.randrange(1, y + 1)
self.closed = True self.closed = True
self.content = 'unknown' self.content = 'unknown'
self.isBox=True
def open(self): def open(self):
self.closed = False self.closed = False

View File

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