Pygame Trigonometry: Following The Hypotenuse?
Solution 1:
Moving on the hypotenuse will most likely require your object to move less than one pixel each frame in either the y or x-axis, and since rects
only can hold integers you'd need a new attribute position
which contains the position of the sprite in float precision. You can use pygame.math.Vector2
to create a vector with useful methods such as normalize()
and adding, subtracting, multiplying with other vectors etc.
Assuming you've created an attribute self.position = pygame.math.Vector2(0, 0)
(or whatever position you want it to start on) you could do something like this:
defhunt_player(self, player):
player_position = pygame.math.Vector2(player.rect.topleft)
direction = player_position - self.position
velocity = direction.normalize() * self.speed
self.position += velocity
self.rect.topleft = self.position
By subtracting the player's position with the enemy's position, you'll get a vector that points from the enemy to the player. If we would to add the direction vector to our position we would teleport to the player immediately. Instead we normalize the vector (making it to length 1 pixel) and multiply our speed attribute. The newly created vector will be an vector pointing towards the player with the length of our speed.
Full example
import pygame
pygame.init()
SIZE = WIDTH, HEIGHT = 720, 480
FPS = 60
BACKGROUND_COLOR = pygame.Color('white')
screen = pygame.display.set_mode(SIZE)
clock = pygame.time.Clock()
classHunter(pygame.sprite.Sprite):
def__init__(self, position):
super(Hunter, self).__init__()
self.image = pygame.Surface((32, 32))
self.image.fill(pygame.Color('red'))
self.rect = self.image.get_rect(topleft=position)
self.position = pygame.math.Vector2(position)
self.speed = 2defhunt_player(self, player):
player_position = player.rect.topleft
direction = player_position - self.position
velocity = direction.normalize() * self.speed
self.position += velocity
self.rect.topleft = self.position
defupdate(self, player):
self.hunt_player(player)
classPlayer(pygame.sprite.Sprite):
def__init__(self, position):
super(Player, self).__init__()
self.image = pygame.Surface((32, 32))
self.image.fill(pygame.Color('blue'))
self.rect = self.image.get_rect(topleft=position)
self.position = pygame.math.Vector2(position)
self.velocity = pygame.math.Vector2(0, 0)
self.speed = 3defupdate(self):
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
self.velocity.x = -self.speed
elif keys[pygame.K_RIGHT]:
self.velocity.x = self.speed
else:
self.velocity.x = 0if keys[pygame.K_UP]:
self.velocity.y = -self.speed
elif keys[pygame.K_DOWN]:
self.velocity.y = self.speed
else:
self.velocity.y = 0
self.position += self.velocity
self.rect.topleft = self.position
player = Player(position=(350, 220))
monster = Hunter(position=(680, 400))
running = Truewhile running:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
player.update()
monster.update(player)
screen.fill(BACKGROUND_COLOR)
screen.blit(player.image, player.rect)
screen.blit(monster.image, monster.rect)
pygame.display.update()
Result
Solution 2:
Since we want to move along the hypotenuse we can use Pythagoras theorem. Here's a brief snippet the should give you the general idea.
I'll use p.x, p.y
for the player's position and e.x, e.y
for the enemy's position.
# Find the horizontal & vertical distances between player & enemy
dx = p.x - e.x
dy = p.y - e.y
#Get the hypotenuse
d = sqrt(dx*dx + dy*dy)
#Calculate the change to the enemy position
cx = speed * dx / d
cy = speed * dy / d
# Note that sqrt(cx*cx + cy*cy) == speed# Update enemy position
e.x += cx
e.y += cy
You need to add some extra code to this to make sure that d
isn't zero, or you'll get a division by zero error, but that only happens when the enemy reaches the player, so I assume you want to do something special when that happens anyway. :)
I should mention that this technique works best if the positions are floats, not integer pixel coordinates.
Solution 3:
Just calculating the distance based on the hypotenuse is not enough. You must pass the coordinates of the enemy into the function and calculate the slope or pass the slope also into the function by value. Then you should move to one of 8 pixels around about your current position where the one you move to is most representative of the path of direction to the enemy. Essentially you move diagonally if the tan of the angle is less than 2 or great that 1/2 otherwise you move in a vertical or horizontal direction. You need to draw a 3x3 set of pixels to see what is actually going on if you can't visualise it.
Post a Comment for "Pygame Trigonometry: Following The Hypotenuse?"