Problem with enemy movement towards player in pygame library
Question:
I ran into a problem with the pygame library while building my app.
A part of my code is as follows: at the beginning of the class in the init method
I create my enemies.
self.create_enemy
And then, that method is like this
def create_enemy(self):
self.available_space_x = 10
for enemy_number in range(self.sets.enemy_number):
self.enemy = Enemy(self)
self.available_space_x += 150
self.enemy.x = self.available_space_x
self.enemy.rect.x = self.enemy.x
self.enemys.add(self.enemy)
The work of this method is that it creates 5 enemies in different places and then adds it to the group of my enemies.
Next, part of the main method is as follows:
self.enemys.draw(self.screen)
self.checkenemymove()
self.update_enemys()
That is, it creates my enemies and then executes this function:
def checkenemymove(self):
if self.human.human_rect.x >= self.enemy.rect.x:
self.goenemleft = False
self.goenemright = True
elif self.human.human_rect.x < self.enemy.rect.x:
self.goenemright = False
self.goenemleft = True
This method makes the enemy move to the right if the player is ahead of the enemy (player side) or the enemy moves to the left if the player is behind the enemy.
And finally this method is executed:
def update_enemys(self):
if self.goenemright:
self.enemy.image = self.enemy.enemy_images[0]
self.enemy.x += self.sets.enemy_speed
self.enemy.rect.x = self.enemy.x
if self.goenemleft:
self.enemy.image = self.enemy.enemy_images[1]
self.enemy.x -= self.sets.enemy_speed
self.enemy.rect.x = self.enemy.x
The problem is that if I run the program, only the last enemy will do this, and wherever the player goes, the enemy will also move in that direction.
Please help me, thank you.
I wanted the enemy to move towards me, but only one enemy moves towards me.
Answers:
Remove the self.enemy
attribute from the class. You don’t need it because all enemies are contained in the self.enemys
pygame.sprite.Group
. Therefore, you need only the group of enemies:
def create_enemy(self):
available_space_x = 10
for enemy_number in range(self.sets.enemy_number):
enemy = Enemy(self)
available_space_x += 150
enemy.x = available_space_x
enemy.rect.x = enemy.x
self.enemys.add(enemy)
Use a for
-loop to move all enemies in the self.enemys
list:
def update_enemys(self):
for enemy in self.enemys:
if self.goenemright:
enemy.image = self.enemy.enemy_images[0]
enemy.x += self.sets.enemy_speed
enemy.rect.x = enemy.x
if self.goenemleft:
enemy.image = self.enemy.enemy_images[1]
enemy.x -= self.sets.enemy_speed
enemy.rect.x = enemy.x
Thank you very much, I have come to the conclusion that I should do this:
def checkenemymove(self):
for enemy in self.enemys:
if self.human.human_rect.x >= enemy.rect.x:
enemy.image = self.enemy.enemy_images[0]
enemy.x += self.sets.enemy_speed
enemy.rect.x = enemy.x
elif self.human.human_rect.x < enemy.rect.x:
enemy.image = self.enemy.enemy_images[1]
enemy.x -= self.sets.enemy_speed
enemy.rect.x = enemy.x
I ran into a problem with the pygame library while building my app.
A part of my code is as follows: at the beginning of the class in the init method
I create my enemies.
self.create_enemy
And then, that method is like this
def create_enemy(self):
self.available_space_x = 10
for enemy_number in range(self.sets.enemy_number):
self.enemy = Enemy(self)
self.available_space_x += 150
self.enemy.x = self.available_space_x
self.enemy.rect.x = self.enemy.x
self.enemys.add(self.enemy)
The work of this method is that it creates 5 enemies in different places and then adds it to the group of my enemies.
Next, part of the main method is as follows:
self.enemys.draw(self.screen)
self.checkenemymove()
self.update_enemys()
That is, it creates my enemies and then executes this function:
def checkenemymove(self):
if self.human.human_rect.x >= self.enemy.rect.x:
self.goenemleft = False
self.goenemright = True
elif self.human.human_rect.x < self.enemy.rect.x:
self.goenemright = False
self.goenemleft = True
This method makes the enemy move to the right if the player is ahead of the enemy (player side) or the enemy moves to the left if the player is behind the enemy.
And finally this method is executed:
def update_enemys(self):
if self.goenemright:
self.enemy.image = self.enemy.enemy_images[0]
self.enemy.x += self.sets.enemy_speed
self.enemy.rect.x = self.enemy.x
if self.goenemleft:
self.enemy.image = self.enemy.enemy_images[1]
self.enemy.x -= self.sets.enemy_speed
self.enemy.rect.x = self.enemy.x
The problem is that if I run the program, only the last enemy will do this, and wherever the player goes, the enemy will also move in that direction.
Please help me, thank you.
I wanted the enemy to move towards me, but only one enemy moves towards me.
Remove the self.enemy
attribute from the class. You don’t need it because all enemies are contained in the self.enemys
pygame.sprite.Group
. Therefore, you need only the group of enemies:
def create_enemy(self):
available_space_x = 10
for enemy_number in range(self.sets.enemy_number):
enemy = Enemy(self)
available_space_x += 150
enemy.x = available_space_x
enemy.rect.x = enemy.x
self.enemys.add(enemy)
Use a for
-loop to move all enemies in the self.enemys
list:
def update_enemys(self):
for enemy in self.enemys:
if self.goenemright:
enemy.image = self.enemy.enemy_images[0]
enemy.x += self.sets.enemy_speed
enemy.rect.x = enemy.x
if self.goenemleft:
enemy.image = self.enemy.enemy_images[1]
enemy.x -= self.sets.enemy_speed
enemy.rect.x = enemy.x
Thank you very much, I have come to the conclusion that I should do this:
def checkenemymove(self):
for enemy in self.enemys:
if self.human.human_rect.x >= enemy.rect.x:
enemy.image = self.enemy.enemy_images[0]
enemy.x += self.sets.enemy_speed
enemy.rect.x = enemy.x
elif self.human.human_rect.x < enemy.rect.x:
enemy.image = self.enemy.enemy_images[1]
enemy.x -= self.sets.enemy_speed
enemy.rect.x = enemy.x