def everything_within_given_distance_on(the_map, distance, position): """Iterates through everything on the_map.bldngs_n_rsrcs that is within the given distance of position. This portion of the_map is in the shape of a diamond. position must be of type Position distance must be a non-negative int""" if not type(distance) is int: print('distance must be an int') return if distance < 0: print('distance must not be negative') return # The following is the center of the diamond-shaped portion of the map. x0, y0 = position.value # The following iterates through the diamond shaped portion of the_map # from bottom to top, left to right. for y_delta in range(-1 * distance, distance + 1): sign_to_mult_by = 1 if y_delta <= 0 else -1 horizontal_radius = distance + y_delta * sign_to_mult_by # height is how far up or down the map, we need to go for x_delta in range(-1 * horizontal_radius, horizontal_radius + 1): x = x0 + x_delta y = y0 + y_delta this_position = Position(x, y) if this_position.is_on_the_map(the_map): yield the_map.bldngs_n_rsrcs[y][x]
for j in range(60, 61): game_map.bldngs_n_rsrcs[i][j] = Iron(Position(j, i)) if __name__ == '__main__': for tpl in ((60, 75), (62, 78), (64, 84), (50, 50)): position = Position(*tpl) print(position, game_map(position)) # print(game_map) # print('-------------------------------------------------------------------') # game_map.print_centered_at(Position(50, 60)) # print('-------------------------------------------------------------------') # game_map.print_centered_at(Position(90, 50)) # print_map(game_map) # The following code was run for various values of distance and Position(i, j): # When it was run, the code within the function everything_within_given_distance_on # was uncommented so that the map was modified. # for i in everything_within_given_distance_on(game_map, 20, Position(5, 92)): # pass # print_map(game_map) # pos1 = Position(5, 6) # vec1 = Vector(2, -1) # print(pos1 - vec1) # print(vec1.magnitude) if game_map.height == 100 and game_map.width == 100: for tpl in ((-1, 5), (100, 20), (90, 100), (120, 200)): position = Position(*tpl) assert not position.is_on_the_map(game_map)