Skip to content Skip to sidebar Skip to footer

Enemy Projectiles Attack Way To Fast Problem

I am trying to make my enemy bullets attack the player but its attacking way to fast I dont know why VIDEO my enemy bullets class # enemys bullets ksud = pygame.image.load(

Solution 1:

The problem is here:

start_x = round(enemyshoots1.x+enemyshoots1.width-107)
start_y = round(enemyshoots1.y + enemyshoots1.height-50)
target_x = playerman.x+playerman.width//2
target_y = playerman.y+playerman.width//2
dir_x, dir_y = target_x - start_x, target_y - start_y

If you print the dir_x and dir_y you'll see that the values are very high. You probably add this values each frame to the position of the bullet. That's why they leave the screen before you can really see them.

You could try this:

BULLET_SPEED = 5

delta_x, delta_y = target_x - start_x, target_y - start_y
distance = math.sqrt(delta_x ** 2 + delta_y ** 2)
dir_x = BULLET_SPEED * delta_x / distance
dir_y = BULLET_SPEED * delta_y / distance

Consider that this solution doesn't rounds the values and pygame might throw an exception because of coordinates with float values. You should round the coordinates before using them for drawing.


Post a Comment for "Enemy Projectiles Attack Way To Fast Problem"