Click here to Skip to main content
15,881,561 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi there,
I am trying to create a top-down shooter game in pygame. I want the enemy to be removed once a collision between the enemy and the Player_bullet has been detected 4 times but am unsure how to do this. Hoping someone can help.

Thanks.

This is my code for the detection between the Player_bullet and the enemy.

Python
for enemy in enemies:
    for bullet in player_bullets:
        if pygame.Rect(enemy.hit_box).collidepoint(bullet.x, bullet.y):
            player_bullets.remove(bullet)
    enemy.main(display)


What I have tried:

I have tried a few tutorials but none of them are what I am looking for.
Posted
Updated 31-Aug-22 13:06pm
v2
Comments
Richard MacCutchan 31-Jul-22 3:46am    
First you need a hit count in the enemy class so you know when to remove it. When the hit count reaches 4 you remove that object from the enemies list.

That is something of a guess since we have no idea of the structure of your enemy class or enemies collection.
Maddox Styles 31-Jul-22 5:10am    
Thank you. I got it working

1 solution

after the enemy hit 4 times, just relocate the enemy position out of frame or any other place
enemyx[i] = random.randint(0,670)
enemyy[i] = random.randint(50, 150)

here you will get complete game
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900