Jan-19-2017, 07:15 PM
Here another you might find more readable.
class Point2D: def __init__(self, pyint_x, pyint_y): self.x = pyint_x self.y = pyint_y def __abs__(self): return Point2D(abs(self.x), abs(self.y)) def __repr__(self): return "Point2D({}, {})".format(self.x, self.y) def __sub__(self, lhs): return Point2D(self.x - lhs.x, self.y - lhs.y) WORLD_SIZE = Point2D(500, 400) # width, height # world wrap coordinate def distance_to_player(enemy, player): distance = abs(enemy - player) real_distance = Point2D(0, 0) direction = Point2D(0, 0) if(WORLD_SIZE.x / 2 < distance.x): real_distance.x = WORLD_SIZE.x - distance.x direction.x = -1 else: real_distance.x = distance.x direction.x = 1 if(WORLD_SIZE.y / 2 < distance.y): real_distance.y = WORLD_SIZE.y - distance.y direction.y = -1 else: real_distance.y = distance.y direction.y = 1 return real_distance, direction def calculation(): player = Point2D(5, 100) enemy = Point2D(400, 58) distance, direction = distance_to_player(enemy, player) print(distance) print(direction) # W, N calculation()
99 percent of computer problems exists between chair and keyboard.