1

So while i'm trying to shoot all the targets, game closes randomly sometimes after shooting 5,6,12 targets instead of after shooting all of them, 20. I really don't know how to fix this, help. It maybe because of using random in generating different targets coords, but still i don't know what to do, so the game ends after shooting all of them, not randomly.

import pygame , sys, random

class Crosshair(pygame.sprite.Sprite):
    def __init__(self, picture_path):
        super().__init__()
        self.image=pygame.image.load(picture_path)
        self.rect=self.image.get_rect()
        self.gunshot=pygame.mixer.Sound("gun-gunshot-01.wav")
    def shoot(self):
        self.gunshot.play()
        pygame.sprite.spritecollide(crosshair, target_group, True)

    def update(self):
        self.rect.center=pygame.mouse.get_pos()

class Target(pygame.sprite.Sprite):
    def __init__(self, picture_path,pos_x,pos_y):
        super().__init__()
        self.image=pygame.image.load(picture_path)
        self.rect=self.image.get_rect()
        self.rect.center=[pos_x,pos_y]

pygame.init()
clock = pygame.time.Clock()

screen_width=1280
screen_height=720
screen=pygame.display.set_mode((screen_width, screen_height))
background=pygame.image.load("bggg.png")
pygame.mouse.set_visible(False)

crosshair=Crosshair("crosshair_red_large.png")
crosshair_group=pygame.sprite.Group()
crosshair_group.add(crosshair)

target_group=pygame.sprite.Group()
for target in range(20):
    new_target=Target("target_red2.png",random.randrange(0,screen_width),random.randrange(0,screen_height))
    target_group.add(new_target)

run=True
while run:
    for event in pygame.event.get():
        if event.type==pygame.QUIT:
            pygame.quit()
            sys.exit()
        if event.type==pygame.MOUSEBUTTONDOWN:
            crosshair.shoot()
    pygame.display.flip()
    screen.blit(background,(0,0))
    target_group.draw(screen)
    crosshair_group.draw(screen)
    crosshair_group.update()
    clock.tick(60)
    if new_target in target_group:
        run=True
    else:
        run=False```
modok3401
  • 11
  • 1

1 Answers1

2

The following instructions are not what you expected:

if new_target in target_group:
   run=True
else:
   run=False

new_target is the last target added to the list. In fact, you are checking that the last target added is still in the list. Once you shoot that target, the target will be removed from the list and the loop will end, even if there are other added targets left in the list.

You have to check if the list is not empty. Use len() to get the number of Sorites in a pygame.sprite.Group:

while run:
    # [...]

    run = len(target_group) > 0
Rabbid76
  • 202,892
  • 27
  • 131
  • 174