def initialize_items(self): '''Function to initialize various objects on the game's map. ''' coins_pos = config.coins_pos for coin in coins_pos: self.coins.append(Coin(coin[0], coin[1], None, None, self.screen)) cloud_pos = config.cloud1_pos for cloud in cloud_pos: self.clouds.append(Cloud1(cloud[0], cloud[1], 4, 16, self.screen))
def new_round(self): self.logger.info('STARTING REPLAY') # Bookkeeping self.step = 0 self.bombs = [] self.explosions = [] self.running = True self.frame = 0 # Game world and objects self.arena = np.array(self.replay['arena']) self.coins = [] for xy in self.replay['coins']: if self.arena[xy] == 0: self.coins.append(Coin(xy, True)) else: self.coins.append(Coin(xy, False)) self.active_agents = [a for a in self.agents] for i, agent in enumerate(self.agents): agent.start_round() agent.x, agent.y = self.replay['agents'][i][-1] agent.total_score = 0
def initialize_items(self): '''Function to initialize various objects on the game's map. ''' coins_pos = config.coins_pos firebeams_pos = config.firebeams_pos firebeams2_pos = config.firebeams2_pos magnets_pos = config.magnets_pos for coin in coins_pos: self.__coins.append( Coin(coin[0], coin[1], None, None, self.__screen)) for magne in magnets_pos: self.__magnets.append( magnet(magne[0], magne[1], None, None, self.__screen)) for firebeam in firebeams_pos: self.__firebeams.append( firebeams(firebeam[0], firebeam[1], None, None, self.__screen)) for firebeam2 in firebeams2_pos: self.__firebeams2.append( firebeams2(firebeam2[0], firebeam2[1], None, None, self.__screen)) self.__start_time = time.time()
def new_round(self): if self.running: self.logger.warning('New round requested while still running') self.end_round() self.round += 1 self.logger.info(f'STARTING ROUND #{self.round}') pygame.display.set_caption(f'BombeRLe | Round #{self.round}') # Bookkeeping self.step = 0 self.active_agents = [] self.bombs = [] self.explosions = [] self.round_id = f'Replay {datetime.now().strftime("%Y-%m-%d %H-%M-%S")}' # Arena with wall and crate layout self.arena = (np.random.rand(s.COLS, s.ROWS) < s.CRATE_DENSITY).astype(int) self.arena[:1, :] = -1 self.arena[-1:, :] = -1 self.arena[:, :1] = -1 self.arena[:, -1:] = -1 for x in range(s.COLS): for y in range(s.ROWS): if (x + 1) * (y + 1) % 2 == 1: self.arena[x, y] = -1 # Starting positions start_positions = [(1, 1), (1, s.ROWS - 2), (s.COLS - 2, 1), (s.COLS - 2, s.ROWS - 2)] random.shuffle(start_positions) for (x, y) in start_positions: for (xx, yy) in [(x, y), (x - 1, y), (x + 1, y), (x, y - 1), (x, y + 1)]: if self.arena[xx, yy] == 1: self.arena[xx, yy] = 0 # Distribute coins evenly self.coins = [] """coin_pattern = np.array([ [1, 1, 1], [0, 0, 1], ]) coins = np.zeros_like(self.arena) for x in range(1, s.COLS - 2, coin_pattern.shape[0]): for i in range(coin_pattern.shape[0]): for j in range(coin_pattern.shape[1]): if coin_pattern[i, j] == 1: self.coins.append(Coin((x + i, x + j), self.arena[x+i,x+j] == 0)) coins[x + i, x + j] += 1""" for i in range(3): for j in range(3): n_crates = (self.arena[1 + 5 * i:6 + 5 * i, 1 + 5 * j:6 + 5 * j] == 1).sum() while True: x, y = np.random.randint(1 + 5 * i, 6 + 5 * i), np.random.randint(1 + 5 * j, 6 + 5 * j) if n_crates == 0 and self.arena[x, y] == 0: self.coins.append(Coin((x, y))) self.coins[-1].collectable = True break elif self.arena[x, y] == 1: self.coins.append(Coin((x, y))) break # Reset agents and distribute starting positions for agent in self.agents: agent.start_round() self.active_agents.append(agent) agent.x, agent.y = start_positions.pop() self.replay = { 'round': self.round, 'arena': np.array(self.arena), 'coins': [c.get_state() for c in self.coins], 'agents': [a.get_state() for a in self.agents], 'actions': dict([(a.name, []) for a in self.agents]), 'permutations': [] } self.running = True
def new_round(self): global seed random.seed(seed) np.random.seed(seed) self.round += 1 # Bookkeeping self.step = 0 self.active_agents = [] self.bombs = [] self.explosions = [] # Arena with wall and crate layout self.arena = (np.random.rand(s.COLS, s.ROWS) < s.CRATE_DENSITY).astype(int) self.arena[:1, :] = -1 self.arena[-1:, :] = -1 self.arena[:, :1] = -1 self.arena[:, -1:] = -1 for x in range(s.COLS): for y in range(s.ROWS): if (x + 1) * (y + 1) % 2 == 1: self.arena[x, y] = -1 # Starting positions start_positions = [(1, 1), (1, s.ROWS - 2), (s.COLS - 2, 1), (s.COLS - 2, s.ROWS - 2)] random.shuffle(start_positions) for (x, y) in start_positions: for (xx, yy) in [(x, y), (x - 1, y), (x + 1, y), (x, y - 1), (x, y + 1)]: if self.arena[xx, yy] == 1: self.arena[xx, yy] = 0 # Distribute coins evenly self.coins = [] """coin_pattern = np.array([ [1, 1, 1], [0, 0, 1], ]) coins = np.zeros_like(self.arena) for x in range(1, s.COLS - 2, coin_pattern.shape[0]): for i in range(coin_pattern.shape[0]): for j in range(coin_pattern.shape[1]): if coin_pattern[i, j] == 1: self.coins.append(Coin((x + i, x + j), self.arena[x+i,x+j] == 0)) coins[x + i, x + j] += 1""" for i in range(3): for j in range(3): for _ in range(1): n_crates = (self.arena[1 + 5 * i:6 + 5 * i, 1 + 5 * j:6 + 5 * j] == 1).sum() while True: x, y = np.random.randint(1 + 5 * i, 6 + 5 * i), np.random.randint( 1 + 5 * j, 6 + 5 * j) if n_crates == 0 and self.arena[x, y] == 0: self.coins.append(Coin((x, y))) self.coins[-1].collectable = True break elif self.arena[x, y] == 1: self.coins.append(Coin((x, y))) break # Reset agents and distribute starting positions for agent in self.agents: agent.start_round() self.active_agents.append(agent) agent.x, agent.y = start_positions.pop() self.replay = { 'round': self.round, 'arena': np.array(self.arena), 'coins': [c.get_state() for c in self.coins], 'agents': [a.get_state() for a in self.agents], 'actions': dict([(a.name, []) for a in self.agents]), 'permutations': [] } self.running = True