Python Forum
Infinitely repeating 2D map
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Infinitely repeating 2D map
#9
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.
Reply


Messages In This Thread
Infinitely repeating 2D map - by schmidtbag - Jan-18-2017, 08:36 PM
RE: Infinitely repeating 2D map - by ichabod801 - Jan-18-2017, 10:34 PM
RE: Infinitely repeating 2D map - by Mekire - Jan-18-2017, 10:39 PM
RE: Infinitely repeating 2D map - by schmidtbag - Jan-19-2017, 03:14 PM
RE: Infinitely repeating 2D map - by Windspar - Jan-19-2017, 04:52 PM
RE: Infinitely repeating 2D map - by schmidtbag - Jan-19-2017, 05:41 PM
RE: Infinitely repeating 2D map - by Windspar - Jan-19-2017, 06:45 PM
RE: Infinitely repeating 2D map - by schmidtbag - Jan-19-2017, 07:02 PM
RE: Infinitely repeating 2D map - by Windspar - Jan-19-2017, 07:15 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020