How to make the enemy not turn around behind the player so quickly. When a player flies close to an enemy plane, it changes its angle very quickly. Can this be limited so that the enemy cannot turn around so quickly?
class EnemyPlane(pygame.sprite.Sprite):
def __init__(self, x, y):
pygame.sprite.Sprite.__init__(self)
self.x = x
self.y = y
self.image = pygame.image.load('4.png')
self.rect = self.image.get_rect(center=(x, y))
self.speed = 6
self.shoot_cooldown = 0
self.angle = 0.0 # Current angle in radians
self.velocity = pygame.Vector2(0, 0) # Velocity vector
self.speed = 6.0 # Current speed
self.acceleration = 5 # Acceleration rate
self.rotation_speed = 0.02 # Rotation speed in radians
self.DECELERATION = 0.05
self.gravity = 1.5
self.current_sprite = 0
self.last_direction = pygame.Vector2(0, 0)
self.player = player
def ai_move(self):
dx = player.rect.x - self.rect.x
dy = player.rect.y - self.rect.y
self.angle = math.atan2(dx, dy)
if math.degrees(self.angle) > 360:
self.angle = 0
elif math.degrees(self.angle) < -360:
self.angle = 0
self.speed += self.acceleration
self.speed = max(-6.0, min(self.speed, 6.0))
self.velocity.x = math.cos(self.angle) * self.speed
self.velocity.y = -math.sin(self.angle) * self.speed
# Update position
self.rect.x += self.velocity.x
self.rect.y += self.velocity.y
def update(self):
self.ai_move()
I tried to limit it somehow with if statements but it didn’t work.
New contributor
Drift Udacznik is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.