def __init__(self, width, height, title): super().__init__(width, height, title) self.size_block = 20 self.maze = make_maze(width//(self.size_block*3), height//(self.size_block*2)) self.agents = [] self.coins = [] self.player = None
def generatenewmaze(self): self.maze = mz.make_maze(self) # for (x, y), value in numpy.ndenumerate(self.maze): for (y, x), value in numpy.ndenumerate(self.maze): # value 2 represents player start point if value == 2: self.startx = x self.starty = y # print("startx, starty", x, y) break self.walls, self.roads, self.start, self.end = mz.define_maze( self, self.maze) # Choose the monster born place for i in range(len(self.roads)): if random.randint(1, 2) == 1: # if random.randint(1,100) == 1: self.monster_born_coordinate.append( [self.roads[i].rect.left, self.roads[i].rect.top]) # print("monster born place",self.monster_born_coordinate) # Chosse the Non_Player_Character born place index_max = len(self.roads) - 1 print("index_max: ", index_max) index = random.randint(0, index_max) self.npc_born_coordinate = self.roads[index].rect.left, self.roads[ index].rect.top
def generatenewmaze(self): self.maze = mz.make_maze(self) for (x, y), value in numpy.ndenumerate(self.maze): if value == 2: self.startx = x self.starty = y break self.walls, self.start, self.end = mz.define_maze(self, self.maze)
def __init__(self, canvas, width=5, height=5): self.canvas = canvas self.load_tiles() self.config = maze.make_maze(width, height) self.objects = []; self.topleft = None self.width = 64 * width self.height = 64 * height self.load_map() self.start = [64, 0] self.end = [self.width - 128, self.height - 64]
def main(): m = make_maze(20, 20) s = Solver(m, (0, 0), (19, 19)) print s while True: s.update() if s.stop == s.current: print 'yay' return True print s raw_input()
def render(w, h, out="a.png"): maze = make_maze(w, h) w, h = (i * 2 + 1 for i in (w, h)) bitmap = np.zeros((h * SIZE_OF_SQUARE, w * SIZE_OF_SQUARE), dtype="uint8") for x, row in enumerate(maze): for y, cell in enumerate(row): colour = 255 if cell else 0 bX, bY = (i * SIZE_OF_SQUARE for i in (x, y)) for i in range(bX, bX + SIZE_OF_SQUARE): for j in range(bY, bY + SIZE_OF_SQUARE): bitmap[i][j] = colour bitmap = np.array(bitmap, dtype="uint8") rendered_image = Image.fromarray(bitmap) rendered_image.save(out, "PNG")
def main(self): while True: real_cells = maze.parse_to_my_stupid_format( *maze.make_maze(maze.X, maze.Y)) targets_chain = [ (0, 0), (maze.X // 2, maze.Y // 2), (0, maze.Y - 1), (maze.X - 1, maze.Y - 1), (maze.X - 1, 0), (0, 0), ] maze.main_run(real_cells, targets_chain) input('\nPRESS ANY KEY TO START NEXT RUN FROM')
def render(w, h, out='a.png'): maze = make_maze(w, h) w, h = (i * 2 + 1 for i in (w, h)) bitmap = np.zeros((h * SIZE_OF_SQUARE, w * SIZE_OF_SQUARE), dtype='uint8') for x, row in enumerate(maze): for y, cell in enumerate(row): colour = 255 if cell else 0 bX, bY = (i * SIZE_OF_SQUARE for i in (x, y)) for i in range(bX, bX + SIZE_OF_SQUARE): for j in range(bY, bY + SIZE_OF_SQUARE): bitmap[i][j] = colour bitmap = np.array(bitmap, dtype='uint8') rendered_image = Image.fromarray(bitmap) rendered_image.save(out, "PNG")
pygame.display.flip() render() while True: if s.stop != s.current: s.update() render() clock.tick(10) def main(): m = make_maze(20, 20) s = Solver(m, (0, 0), (19, 19)) print s while True: s.update() if s.stop == s.current: print 'yay' return True print s raw_input() if __name__ == '__main__': #main() m = make_maze(20, 20) s = Solver(m, (0, 0), (19, 19)) game(s)
_t = find_tile_by_name(t['type']) _sz = _t['unit']['size'] sq_t = ((t['x'], t['y']), (t['x'] + _sz, t['y'] + _sz)) if _square_inside_square(sq_t, p, size): return False return True def stream_map(s, f): _p = s.p _p['x'] = s.width _p['y'] = s.height f.write(json.dumps(_p)) if __name__ == '__main__': import sys mz = make_maze(21,32) h = w = i = 0 for c in mz: if c == '\n': w = i i = 0 h += 1 continue i += 1 m = BaseMap(height=h, width=w) #m.add_b('start location', 1, (1,1)) for y in range(h): for x in range(w): if val_maz(mz, x, y, w) == '+': m.add_t('tree 1', (x, y)) if val_maz(mz, x, y, w) == '-': m.add_t('tree 2', (x, y)) if val_maz(mz, x, y, w) == '|': m.add_t('tree 3', (x, y))
def level(): global player player = mia.Mia(x=330,y=0) framework.space.add(player.body) for shape in player.shapes: framework.space.add(shape) player.add(framework.characters) player2 = cassie.Cassie(x=630,y=0) framework.space.add(player2.body) for shape in player2.shapes: framework.space.add(shape) player2.add(framework.characters) global playable playable = [player, player2] npcs = [doomimp.DoomImp(x=230, y=250, target=player), doomimp.DoomImp(x=630, y=150, target=player), doomimp.DoomImp(x=580,y=560,target=player)] # npcs = [] for npc in npcs: framework.space.add(npc.body) for shape in npc.shapes: framework.space.add(shape) npc.add(framework.characters) bricks = [] for i in range(1,8): bricks.append(brick.Brick(size=40, x=300 + 125*i, y=-400)) for i in range(1,20): bricks.append(brick.Brick(size=10, y=150, x=130+21*i)) for i in range(1,20): bricks.append(brick.Brick(size=10, y=250, x=130+21*i)) for i in range(1,20): bricks.append(brick.Brick(size=10, y=350, x=130+21*i)) for brickinstance in bricks: framework.primitives.append(brickinstance) framework.space.add(brickinstance.body) framework.space.add(brickinstance.shape) staticbricks = [] staticbricks.append(brick.StaticBrick(x=455, y=50, width=50, height=10)) staticbricks.append(brick.StaticBrick(x=380, y=270, width=10, height=420)) staticbricks.append(brick.StaticBrick(x=400, y=640, width=800, height=10)) for staticbrick in staticbricks: framework.primitives.append(staticbrick) framework.space.add(staticbrick.shape) maze.make_maze(130, 80, width=8, height=4, cellwidth=70, cellheight=65, wallthickness=10) maze.make_maze(570, 80, width=8, height=4, cellwidth=70, cellheight=65, wallthickness=10) maze.make_maze(870, 80, width=80, height=12, cellwidth=100, cellheight=75, wallthickness=20) framework.grabbables.append((brick.Balloon(radius=15, num_pts=6, x=330, y=500))) framework.grabbables[0].shape.layers = 0b1000 framework.grabbables[0].shape.group = 2 framework.primitives.append(framework.grabbables[0]) framework.space.add(framework.grabbables[0].body) framework.space.add(framework.grabbables[0].shape) framework.grabbables.append((brick.Balloon(radius=15, num_pts=10, x=145, y=525))) framework.grabbables[1].shape.layers = 0b1000 framework.grabbables[1].shape.group = 2 framework.primitives.append(framework.grabbables[1]) framework.space.add(framework.grabbables[1].body) framework.space.add(framework.grabbables[1].shape) candies = [] for i in range(100): candies.append(sprite.Basic_Sprite("images/candy.png",530,300,width=10,height=8,mass=.1)) for candy in candies: framework.space.add(candy.shape) framework.space.add(candy.body) candy.add(framework.characters) framework.grabbables.extend(candies) barrel = sprite.Basic_Sprite("images/barrel.png",460,0,width=15,height=33,mass=10,jumpable=1,elasticity=1,offset=(0,-1)) framework.space.add(barrel.shape) framework.space.add(barrel.body) barrel.add(framework.characters) global airship1 airship1 = airship.Airship() pygame.mixer.music.load("I-Want-Candy.wav") pygame.mixer.music.play(-1) pygame.mixer.music.set_volume(.15)
_sz = _t['unit']['size'] sq_t = ((t['x'], t['y']), (t['x'] + _sz, t['y'] + _sz)) if _square_inside_square(sq_t, p, size): return False return True def stream_map(s, f): _p = s.p _p['x'] = s.width _p['y'] = s.height f.write(json.dumps(_p)) if __name__ == '__main__': import sys mz = make_maze(21, 32) h = w = i = 0 for c in mz: if c == '\n': w = i i = 0 h += 1 continue i += 1 m = BaseMap(height=h, width=w) #m.add_b('start location', 1, (1,1)) for y in range(h): for x in range(w): if val_maz(mz, x, y, w) == '+': m.add_t('tree 1', (x, y)) if val_maz(mz, x, y, w) == '-': m.add_t('tree 2', (x, y)) if val_maz(mz, x, y, w) == '|': m.add_t('tree 3', (x, y))
for i in range(len(moves) - 1): plot_maze(test_maze, x1, y1) if moves[i][1] + 1 == moves[i + 1][1]: x1, y1 = go_down(x1, y1) elif moves[i][0] + 1 == moves[i + 1][0]: x1, y1 = go_right(x1, y1) elif moves[i][0] - 1 == moves[i + 1][0]: x1, y1 = go_left(x1, y1) elif moves[i][1] - 1 == moves[i + 1][1]: x1, y1 = go_up(x1, y1) test_maze = [] # size of maze x = int(input('Provide x complexity (1-50): ')) y = int(input('Provide y complexity (1-50): ')) # Maze generation test_maze = maze.make_maze(x, y) # add host x_host = 1.5 y_host = 0.5 # Find optimal path with all information best_path = bfs(test_maze, (math.floor(x_host), math.floor(y_host)), 2) # show path execute_path(test_maze, best_path, x_host, y_host)
for C in range(n * k): b = [] for R in range(n * k): b.append(block[C % k][R % k]) B.append(b) return np.array(B) def play_notes(notes, s=0.15): for n in notes: d = 0.3 + random.random() ps.play(70 + n, release=d) t = ps.random.choice([0.125, 0.25, 0, 0.2]) ps.sleep(t) if __name__ == '__main__': block = block_generation(21, 6) board = board_generation(block, 4) print(board.shape) n, _ = board.shape L = mz.make_maze(n, n) e1 = L[0][0] e2 = L[n - 1][n - 1] w = mz.find_path(e1, e2) for x in w: x['state'] = 'Path' # mz.show(L) notes = [board[x['c']][x['r']] for x in w] play_notes(notes)
parser.add_argument('--method', type=int, default=0, help='index of method: 0(QLEARN), 1(SARSA), 2(PG)') parser.add_argument( '--agent', type=int, default=0, help='index of agent: 0(QTable), 1(QAgent), 2(GAgent), 3(AC)') FLAGS, unparsed = parser.parse_known_args() #FLAGS.maze = 2 #FLAGS.agent= 1 #FLAGS.method = 0 #print(FLAGS) env = make_maze("space%d" % FLAGS.maze) if FLAGS.agent == 0: agent = Agent(env) elif FLAGS.agent == 1: agent = DAgent(env) elif FLAGS.agent == 2: agent = GAgent(env) else: agent = A2CAgent(env) FLAGS.method = 0 np.random.seed(0) num_episodes = 50 max_number_of_steps = 30 if FLAGS.maze >= 2: num_episodes *= 10
def level(): global player player = mia.Mia(x=330, y=0) framework.space.add(player.body) for shape in player.shapes: framework.space.add(shape) player.add(framework.characters) player2 = cassie.Cassie(x=630, y=0) framework.space.add(player2.body) for shape in player2.shapes: framework.space.add(shape) player2.add(framework.characters) global playable playable = [player, player2] npcs = [ doomimp.DoomImp(x=230, y=250, target=player), doomimp.DoomImp(x=630, y=150, target=player), doomimp.DoomImp(x=580, y=560, target=player) ] # npcs = [] for npc in npcs: framework.space.add(npc.body) for shape in npc.shapes: framework.space.add(shape) npc.add(framework.characters) bricks = [] for i in range(1, 8): bricks.append(brick.Brick(size=40, x=300 + 125 * i, y=-400)) for i in range(1, 20): bricks.append(brick.Brick(size=10, y=150, x=130 + 21 * i)) for i in range(1, 20): bricks.append(brick.Brick(size=10, y=250, x=130 + 21 * i)) for i in range(1, 20): bricks.append(brick.Brick(size=10, y=350, x=130 + 21 * i)) for brickinstance in bricks: framework.primitives.append(brickinstance) framework.space.add(brickinstance.body) framework.space.add(brickinstance.shape) staticbricks = [] staticbricks.append(brick.StaticBrick(x=455, y=50, width=50, height=10)) staticbricks.append(brick.StaticBrick(x=380, y=270, width=10, height=420)) staticbricks.append(brick.StaticBrick(x=400, y=640, width=800, height=10)) for staticbrick in staticbricks: framework.primitives.append(staticbrick) framework.space.add(staticbrick.shape) maze.make_maze(130, 80, width=8, height=4, cellwidth=70, cellheight=65, wallthickness=10) maze.make_maze(570, 80, width=8, height=4, cellwidth=70, cellheight=65, wallthickness=10) maze.make_maze(870, 80, width=80, height=12, cellwidth=100, cellheight=75, wallthickness=20) framework.grabbables.append((brick.Balloon(radius=15, num_pts=6, x=330, y=500))) framework.grabbables[0].shape.layers = 0b1000 framework.grabbables[0].shape.group = 2 framework.primitives.append(framework.grabbables[0]) framework.space.add(framework.grabbables[0].body) framework.space.add(framework.grabbables[0].shape) framework.grabbables.append((brick.Balloon(radius=15, num_pts=10, x=145, y=525))) framework.grabbables[1].shape.layers = 0b1000 framework.grabbables[1].shape.group = 2 framework.primitives.append(framework.grabbables[1]) framework.space.add(framework.grabbables[1].body) framework.space.add(framework.grabbables[1].shape) candies = [] for i in range(100): candies.append( sprite.Basic_Sprite("images/candy.png", 530, 300, width=10, height=8, mass=.1)) for candy in candies: framework.space.add(candy.shape) framework.space.add(candy.body) candy.add(framework.characters) framework.grabbables.extend(candies) barrel = sprite.Basic_Sprite("images/barrel.png", 460, 0, width=15, height=33, mass=10, jumpable=1, elasticity=1, offset=(0, -1)) framework.space.add(barrel.shape) framework.space.add(barrel.body) barrel.add(framework.characters) global airship1 airship1 = airship.Airship() pygame.mixer.music.load("I-Want-Candy.wav") pygame.mixer.music.play(-1) pygame.mixer.music.set_volume(.15)
if wall[1] == wall[3]: return 'horizontal' else: return 'vertical' def draw(walls, world, alex): number_of_walls = len(walls) walls_in_world = filter(lambda(e): e.startswith('wall_'), world['entities'].keys()) if len(walls_in_world) >= number_of_walls: return in_world = sorted(map(lambda(w): int(w.partition('al_')[2]), walls_in_world)) indexes = [i for i in range(number_of_walls) if i not in in_world][0:10] for index in indexes: draw_wall(index, walls[index], world['tick'], alex) def send_movement(position, orientation, index, tick, alex): entity = 'wall_%s' % orientation name = '%s_%d' % (entity, index,) movement = {'tick': tick, 'entity': entity, 'index': index, 'from': position, 'to': position} alex.pubsub.publish('movement.' + name, movement) alex = Alexandra(argv[1]) init(alex) walls = list(make_maze(alex.config['field_width']/CELL_WIDTH, alex.config['field_height']/CELL_WIDTH)) alex.pubsub.consume_topic('world', lambda w: draw(walls, w, alex))
return neighbors def sorted_neighbors(neighbors, goal): return sorted(neighbors, key=lambda neighbor: sqrt(pow( neighbor.x - goal.x, 2) + pow(neighbor.y - goal.y, 2))) if __name__ == '__main__': # Define window screen = pygame.display.set_mode((500, 500)) pygame.display.set_caption('Maze solver') clock = pygame.time.Clock() maze = make_maze(screen, 20) stack = [] goal = maze[-1][-1] goal.color = (255, 0, 0) start = maze[0][0] start.visited = True stack.append(start) found = False running = True while running: # Events for event in pygame.event.get(): if event.type == pygame.QUIT: # Close window
import maze, stack stack = stack.Stack() maze = maze.make_maze(10, 10, 2) def dfs(maze, stack): length = len(maze) start = maze[0][0] goal = (9, 9) stack.push(start) while stack.isEmpty() == False: node = stack.pop() node.searched = True for x, y in (node.x + 1, node.y), (node.x - 1, node.y), (node.x, node.y - 1), (node.x, node.y + 1): if (0 <= x < length and 0 <= y < length) and ( maze[x][y].wall == False) and (maze[x][y].searched == False): if (x, y) == goal: return True else: maze[x][y].parent = node stack.push(maze[x][y]) return False print(dfs(maze, stack))
def run(self): self.is_receiver = False try: while 1: data = self.socket.recv(1024) data = data.decode("utf-8") if not data: break else: if data == "receive": with clients_lock: self.is_receiver = True clients.add(self.socket) reply = "received:" reply += data print(data) if data == "feed_paper_in": reply += "feeding paper in" lego_printer.manual_paper_feed(1) elif data == "feed_paper_out": reply += "feeding paper out" lego_printer.manual_paper_feed(-1) elif data == "stop_feed": reply += "stop feeding paper" lego_printer.stop_paper_feed() elif data == "feed_paper_in_inc": lego_printer.manual_paper_feed_inc(1) elif data == "feed_paper_out_inc": lego_printer.manual_paper_feed_inc(-1) elif data == "feed_paper_stop_inc": lego_printer.manual_paper_feed_inc_stop() elif data == "move_right": lego_printer.manual_move_x(1) elif data == "move_left": lego_printer.manual_move_x(-1) elif data == "move_stop": lego_printer.manual_stop_x() elif data == "switch_pen_pos": lego_printer.switch_pen_pos() elif data == "move_pen_up": lego_printer.pen_up() elif data == "move_pen_down": lego_printer.pen_down() elif data == "set_pen_position": lego_printer.set_pen_position() elif data == "switch_pen_state": lego_printer.switch_pen_state() print("pen state is", lego_printer.pen_is_adjustable) elif data == "get_info": reply += brick_info.get_info() elif data == "calibrate": lego_printer.calibrate() elif data == "force_stop": lego_printer.force_stop() elif data.startswith( "draw_maze") and lego_printer.is_busy is False: lego_printer.stop_paper_feed() maze_data = data.split('|') rows = int(maze_data[1]) cols = int(maze_data[2]) grid_size = int(maze_data[3]) s, h, v = make_maze(rows, cols) reply += s draw_list = parse_maze(rows, cols, grid_size, s, h, v) lego_printer.draw(list(draw_list)) elif data.startswith( "draw_svg") and lego_printer.is_busy is False: svg_data = data.split('|') if os.path.isfile(svg_data[1]): svg_path = svg_data[1] else: svg_path = "../" + svg_data[1] reply += svg_path svg_parser = SvgParser() parser = xml.sax.make_parser() parser.setFeature(xml.sax.handler.feature_namespaces, 0) parser.setContentHandler(svg_parser) parser.parse(svg_path) lego_printer.draw(list(svg_parser.draw_list)) with clients_lock: if self.is_receiver is False: self.socket.sendall(bytes(reply, 'UTF-8')) for c in clients: c.sendall(bytes(reply, 'UTF-8')) finally: with clients_lock: if self.is_receiver is True: clients.remove(self.socket) self.socket.close()