1

EDIT: I asked a friend and he found a way, generate a list of possible squares the convoy can move to from the original square and choose the one closes to the end coordinates, then generate a list for that square and again choose the closest ect.

I am making a game where among other things you move a convoy. The map is basically a Cartesian coordinate system, the coordinates are a tuple (X, Y). What i need is to find a way to determine the sectors that the convoy passes through, since one only has the start point and chosen end point, to later be able to change the corresponding values of those sectors to simulate fog of war.

I tried creating a square around the two points and then a list for all possible Y and X coordinates (X/Y_movement_coords_loc). Now the problem is finding the shortest possible route through this square, but i cant figure out a formula, since there are quiet a lot of possibilities and what i tried includes a division which often leads to fractions or division by 0. Neither of which i can use.

y_per_x_coord = len(y_movement_coords_loc) / len(x_movement_coords_loc)

Also the code as is now, for some inexplicable reason hangs at the

for x_index in range(len(x_movement_coords_loc)): 

loop, it enters the if clause, appends to the list but 3 out of 5 times doesn't reach the res_index_list, i checked with prints. Anyone out there who can help me with my problem? Thanks in advance.

x_movement_loc = old_coord_loc[0] - new_coord_loc[0]
y_movement_loc = old_coord_loc[1] - new_coord_loc[1]
x_movement_coords_loc = []
y_movement_coords_loc = []
moved_tiles_coords = []
negative_movement = 1
if x_movement_loc < 0:
    x_movement_loc *= -1
    negative_movement = -1
if y_movement_loc < 0:
    y_movement_loc *= -1
    negative_movement = -1
for u in range(x_movement_loc):
    x_movement_coords_loc.append(old_coord_loc[0] + (u * negative_movement))
for u in range(y_movement_loc):
    y_movement_coords_loc.append(old_coord_loc[1] + (u * negative_movement))
counter = 0
y_per_x_coord = len(y_movement_coords_loc) / len(x_movement_coords_loc)
for x_index in range(len(x_movement_coords_loc)):
    for y_index in range(y_per_x_coord):
        moved_tiles_coords.append((x_movement_coords_loc[x_index], y_movement_coords_loc[y_index + counter]))
    counter += y_per_x_coord
    if (x_movement_coords_loc[x_index], y_movement_coords_loc[counter]) != new_coord_loc:
        moved_tiles_coords.append((x_movement_coords_loc[x_index], y_movement_coords_loc[counter]))
res_index_list = [1, 3, 5, 7, 9]
Kitube
  • 11
  • 2
  • It looks to me as shortest path between two nodes in a graph. If you don't want to implement.. http://stackoverflow.com/questions/2606018/path-between-two-nodes – Netro Dec 30 '15 at 01:39
  • Thanks, that would also be a solution, but i just found a shorter one, check the edit if your interested. – Kitube Dec 30 '15 at 01:45

0 Answers0