I have uploaded quite a few questions recently and I think people are getting tired of me but I am bad at programming and am trying to code a game for A-level coursework so I need all the help I can get to learn the language. Anyway, I'll show a few relevant bits of code below for reference. I use all_targets because I want to add delay using threshold so they don't all spawn simultaneously. I think this is causing the targets to be re-added to the sprite group but I can't resolve it. I want targets to be removed from target_sprites when hit with a bullet. I have tried using booleans to make it so that if destroyed == True:
all_bullets.remove(item) but it does not seem to work. Any help is appreciated.
class Target(pygame.sprite.Sprite):
def __init__(self, width, height, offset, threshold):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface([width, height])
self.image = target_img
self.rect = self.image.get_rect()
self.rect.center = (self.rect.x + 50, self.rect.y + offset)
self.threshold = threshold
target_sprites = pygame.sprite.Group()
target_1 = Target(100, 100, 100, 0)
target_2 = Target(100, 100, 300, 150)
target_3 = Target(100, 100, 200, 300)
target_4 = Target(100, 100, 100, 450)
target_5 = Target(100, 100, 400, 600)
target_6 = Target(100, 100, 250, 750)
#Function to add delay between targets spawning.
def target_delay():
global clock
clock += 1
for item in all_targets:
if clock >= item.threshold:
target_sprites.add(item)
#Function to make targets move each time screen refreshes.
def movement():
for item in target_sprites:
item.rect.x += 1
for item in all_bullets_keep:
if item['y']-30 < (target_1.rect.y) + 100 and item['y']+30 > target_1.rect.y:
if item['x']+10 > target_1.rect.x and item['x']-30 < (target_1.rect.x) + 100:
target_1.kill()
if item['y']-30 < (target_2.rect.y) + 100 and item['y']+30 > target_2.rect.y:
if item['x']+10 > target_2.rect.x and item['x']-30 < (target_2.rect.x) + 100:
target_2.kill()
if item['y']-30 < (target_3.rect.y) + 100 and item['y']+30 > target_3.rect.y:
if item['x']+10 > target_3.rect.x and item['x']-30 < (target_3.rect.x) + 100:
target_3.kill()
if item['y']-30 < (target_4.rect.y) + 100 and item['y']+30 > target_4.rect.y:
if item['x']+10 > target_4.rect.x and item['x']-30 < (target_4.rect.x) + 100:
target_4.kill()
if item['y']-30 < (target_5.rect.y) + 100 and item['y']+30 > target_5.rect.y:
if item['x']+10 > target_5.rect.x and item['x']-30 < (target_5.rect.x) + 100:
target_5.kill()
if item['y']-30 < (target_6.rect.y) + 100 and item['y']+30 > target_6.rect.y:
if item['x']+10 > target_6.rect.x and item['x']-30 < (target_6.rect.x) + 100:
target_6.kill()