def test_random_edge(): for _ in range(0, 100): edge_point = tools.random_edge_point(10, 10) is_edge = False if edge_point[0] == 0 or edge_point[0] == 9: is_edge = True if edge_point[1] ==0 or edge_point[1] == 9: is_edge = True assert is_edge
def create_river(tileset, drainage): river_tiles = [] start_location = tools.random_edge_point(tileset.width, tileset.height) scale = tileset.width * tileset.height # move a random distance towards the drainage point walk_length = random.randint(1, int(scale / 5)) if walk_length == 0: raise ValueError('Could not generate a river of length 0 at location %d, %d' % start_location) current_location = start_location step_count = 0 while current_location != drainage and step_count <tileset.width + tileset.height: # crate vector space between current point and drainage location vector = drainage - current_location # scale down to our step magnitude = int(tools.distance(current_location, drainage)) # magnitude = min(magnitude, int(distance(current_location, drainage))) if scale < magnitude: vector = Point(int(vector[0] * scale / magnitude), int(vector[1] * scale / magnitude)) #choose a random point in the space rand_vector = Point(tools.rand_to(vector.x), tools.rand_to(vector.y)) new_location = current_location + rand_vector # draw a line to that point river_tiles += get_river_section(tileset, current_location, new_location) current_location = new_location step_count += 1 return river_tiles