2
class NPC(pg.sprite.Sprite): 
    def __init__(self,x,y,image):
        self.copyimg = pg.image.load(image).convert_alpha()
        pg.sprite.Sprite.__init__(self)
        self.image = self.copyimg.copy()
        #self.copyimg.fill(RED)
        self.rect = self.image.get_rect()
        self.radius = int(self.rect.width / 3)
        #pg.draw.circle(self.copyimg,RED,self.rect.center,self.radius)
        self.rect.center = (x,y)
        self.pos = vec(x,y)
        self.vel = vec(0,0)
        self.acc = vec(0,0)
        self.speed =  0.3
        self.friction = -0.02
        self.rot = 0
        self.chasex = True
        self.chasey = True
        self.directing = 1
        self.times = pg.time.get_ticks()
        self.mainmenu = True
    def rotate(self):
        self.rot = self.rot % 360
        newimage = pg.transform.rotate(self.copyimg,int(self.rot%360))
        
        #pg.draw.rect(screen,WHITE,self.rect,5)
        #old_center = self.rect.center
        self.image = newimage
        self.rect = self.image.get_rect()
        #self.rect.center = old_center   

    def shoot(self):
        bullet = BulletPlayer(self.pos.x,self.pos.y,self.rot,lasers['laserblue'][random.randint(0,len(lasers['laserblue'])-1)])
        bulletgroupenemy.add(bullet)
        pass

class Bullet(pg.sprite.Sprite):
    def __init__(self,x,y,rotation,image):
        pg.sprite.Sprite.__init__(self)
        self.cop = pg.image.load(image).convert_alpha()
        self.image = self.cop.copy()
        self.rect = self.image.get_rect()
        self.rect.bottom = y
        self.rect.centerx = x
        self.shootspeed = 10
        self.rotation = rotation

    def update(self):
        self.rotation = self.rotation % 360
        newimage = pg.transform.rotate(self.cop,int(self.rotation))
        oldcenter = self.rect.center
        self.image = newimage
        self.rect = self.image.get_rect()   
        self.rect.center = oldcenter
        
        keys = pg.key.get_pressed()
        
        if self.rect.x < 0 or self.rect.x >WIDTH or self.rect.y > HEIGHT or self.rect.y < 0:
            self.kill()
    

I have a NPC class and bullet class like that and ofcourse there is mainplayer that we can control. and as you can see there is shoot method with in the NPC class. this is calling automatically by npc it self how ever when i shoot npc with a bullet and call spritecollide function

 hitsenemy = pg.sprite.spritecollide(playerlist[i],bulletgroup,True)
 if hitsenemy:
           playerlist[i].kill()       

the npc get kills thats correct but somehow it is keeping to shoot. the shoot function still works how this can be !. i just killed by using this hitsenemy . and also i use this for loop to add spride group. How can i prevent that i dont want it to keep shooting

playerlist = [Player(300,200,'playerShip2_blue.png')]
for players in playerlist:
    allsprites.add(players)
    playergroup.add(players)

i have also this allsprites group allsprites = pg.sprite.Group()

this method belongs the player class which i didnt share because but this is how i shoot with player class.

def shoot(self):
        bullet = BulletPlayer(self.rect.centerx,self.rect.centery,self.rot,lasers['lasergreen'])
        bulletgroup.add(bullet)
        allsprites.add(bullet)
omneer
  • 93
  • 9

1 Answers1

0

playerlist is a list but not a pygame.sprite.Group. pygame.sprite.Sprite.kill

remove the Sprite from all Groups

Therefor when you call kill, the Sprite is removed from all Groups but it is still in the playerlist.

You have to remove the Sprite from the list. See How to remove items from a list while iterating?

hitsenemy = pg.sprite.spritecollide(playerlist[i],bulletgroup,True)
if hitsenemy:
    playerlist[i].kill()
    playerlist.pop(i)

Alternatively, consider using a group instead of a list. Note the Sprites in a Group can be iterated. And the list of Sprites in the Group can be obtained via pygame.sprite.Group.sprites().

Rabbid76
  • 202,892
  • 27
  • 131
  • 174