class CoopaModel(Model): """A model with some number of agents.""" def __init__(self, N, width, height, agent_type, log_path=None): self.running = True self.num_agents = N self.grid = SingleGrid(width, height, torus=False) self.schedule = RandomActivation(self) self.message_dispatcher = MessageDispatcher() self.layout = Layout() self._context = Context() self.agent_type = AGENT_TYPES[agent_type] self.layout.draw(self.grid) # Add drop point(s) self.drop_points = [DropPoint(1, self)] self.grid.place_agent(self.drop_points[0], (5, 5)) # Add recharging station(s) self.recharge_points = [RechargePoint(1, self)] self.grid.place_agent(self.recharge_points[0], (55, 5)) # Place resources tactically self._context.place_few_trash_in_all_rooms(self) # the mighty agents arrive for i in range(self.num_agents): a = self.agent_type(i, self, log_path=log_path) self.schedule.add(a) self.grid.position_agent(a) self.datacollector = DataCollector( model_reporters={ "Trash collected": compute_dropped_trashes, "Average battery power": compute_average_battery_power, "Max battery power": compute_max_battery_power, "Min battery power": compute_min_battery_power }, # agent_reporters={"Trash": "trash_count"} ) # An agent attribute self.name = "CoopaModel" self._logger = utils.create_logger(self.name, log_path=log_path) @property def time(self): return self.schedule.time def step(self): t = time.monotonic() self.datacollector.collect(self) self.schedule.step() self._log("Finished in {:.5f} seconds.".format(time.monotonic() - t), logging.INFO) def _log(self, msg, lvl=logging.DEBUG): self._logger.log(lvl, msg, extra={'time': self.time})
class Game: def __init__(self, player_no=0): self.field = Field() self.queue = MinoQueue() self.score = Score() self.__holding = Holding() self.layout = Layout(self.field, self.queue, self.score, self.__holding, player_no) self.mino = self.queue.get_next() self.state = GameState.PLAYING self.__reset_counter() self.player_no = player_no def update(self): removed_lines = self.field.remove_complete_lines() if len(removed_lines) > 0: self.score.count_removed(removed_lines) return if self.__is_game_over(): pyxel.stop(0) self.state = GameState.GAME_OVER self.layout.reduce_drawable_line() return self.advance_counter -= self.reduce_count if self.advance_counter <= 0: self.__advance() def draw(self): self.layout.draw(self.mino) def move(self, direction): if self.mino.can_move(self.field, direction): pyxel.play(1, 3) self.mino.move(direction) def rotate(self, direction): if self.mino.can_rotate(self.field, direction): pyxel.play(1, 5) self.mino.rotate(direction) def drop(self): while self.mino.can_move(self.field, MoveDirection.DOWN): self.mino.move(MoveDirection.DOWN) self.__put_on() def hold(self): if self.__holding.can_hold(): self.mino = self.__holding.hold(self.mino) if self.mino == None: self.mino = self.queue.get_next() def __is_game_over(self): if not self.mino.is_initial_position(): return False if self.mino.can_move(self.field, C.MoveDirection.DOWN): return False return True def __advance(self): if self.mino.can_move(self.field, MoveDirection.DOWN): self.mino.move(MoveDirection.DOWN) self.__reset_counter() else: self.__put_on() def __put_on(self): self.field.store(self.mino.blocks, self.mino.position) pyxel.play(1, 4) self.mino = self.queue.get_next() self.advance_counter += 1 self.__holding.unlock() def __reset_counter(self): self.advance_counter = C.ADVANCE_COUNTER self.reduce_count = C.REDUCE_COUNT
self.decamins = decamins self.mins = mins def display_time(self): h, m = time.localtime()[3:5] # sure? check self.hours.change_state(h) self.decamins.change_state(m / 10) # dovrebbe cambiare la divisione # tra 2.7 e 3 passando da int a float... ocjio self.mins.change_state(m % 10) def test_time(self, h, m): self.hours.change_state(h) self.decamins.change_state(m / 10) self.mins.change_state(m % 10) if __name__ == "__main__": l = Layout(screen) c = Clock(l.balls, l.triangle, l.penthagon) while True: c.display_time() l.draw() time.sleep(20) # non mi piace molto l'organizzazione, # meglio da clock aggiungere gli elementi al layout forse # cioè?