From a68d3842261381b53f18a03109d5d1600317cda1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joanna=20Rado=C5=82a?= Date: Sat, 13 Mar 2021 17:41:38 +0100 Subject: [PATCH] initial map model; visualisation with boxes --- model.py | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ server.py | 20 ++++++++++++++++++++ 2 files changed, 71 insertions(+) create mode 100644 model.py create mode 100644 server.py diff --git a/model.py b/model.py new file mode 100644 index 0000000..c2043f0 --- /dev/null +++ b/model.py @@ -0,0 +1,51 @@ +from mesa import Model, Agent +from agent import Player +from mesa.time import RandomActivation +from mesa.space import SingleGrid +#from mesa.datacollection import DataCollector +import random + +x = 11 +y = 11 +possible_contents = ['gold', 'sword', 'shield', 'potion', 'empty'] +step_counter = 0 +boxes_number = 5 + + +class GameMap(Model): + def __init__(self, x, y, boxes_number): + self.grid = SingleGrid(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 + 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) + + + # self.datacollector=DataCollector #informacje o stanie planszy, pozycja agenta + + def step(self): + self.schedule.step() + # self.datacollector.collect(self) #na razie niepotrzebne + + +class Box(Agent): + def __init__(self, unique_id, model): + super().__init__(unique_id, model) + # self.x_coor = random.randrange(1, x + 1) # pola 1-10, zmienić na 0-9? + # self.y_coor = random.randrange(1, y + 1) + self.closed = True + self.content = 'unknown' + + def open(self): + self.closed = False + self.content = random.choice( + possible_contents) # lepiej od razu przypisać content zamiast w ogóle dawać unknown? + + def step(self): + pass diff --git a/server.py b/server.py new file mode 100644 index 0000000..072b68c --- /dev/null +++ b/server.py @@ -0,0 +1,20 @@ +from model import GameMap +from mesa.visualization.modules import CanvasGrid +from mesa.visualization.ModularVisualization import ModularServer + + +def box_representation(box): + portrayal = {"Shape": "circle", + "Filled": "true", + "Layer": 0, + "Color": "red", + "r": 0.5} + return portrayal + +grid = CanvasGrid(box_representation, 10, 10, 500, 500) +server = ModularServer(GameMap, + [grid], + "Map", + {"x":10, "y":10, "boxes_number":5}) +server.port = 8521 +server.launch() \ No newline at end of file