1

I am designing a target shooter game. Reffering to the below code, items in 'all_targets' should be added to target_sprites after the 'clock' has met each targets threshold (for the sake of delay). i then use another funtion to delete the target from all_targets when they are colliding with a bullet. I then use 'self.destroyed' to differentiate between which end game screen is shown, with the winner screen showing if all 6 targets are hit, the loser being shown if not all 6 are hit after the 6th has left the arena. This works for a bout a second but then they all reappear. I've tried putting the all_targets list inside the while loop but it still doesn't work.

clock = 0
py_clock = pygame.time.Clock()

#Using pygame.sprite to create a class for the player sprite.
player_img = pygame.image.load('santa.png')

class Player(pygame.sprite.Sprite):
    def __init__(self, width, height):
        pygame.sprite.Sprite.__init__(self, player_sprites)
        self.image = pygame.Surface([width, height])
        self.image = player_img
        self.rect = self.image.get_rect()
        self.rect.center = (800 / 2, 545)

player_sprites = pygame.sprite.Group()

player_sprite = Player(64,64)
player_sprites.add(player_sprite)

#Using pygame.sprite to create a class for the target sprites.
target_img = pygame.image.load('Christmas Target.png')

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
        self.destroyed = False
        self.spawned = False


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)

destroyed_targets = []

def wrecked():
  for item in all_targets:
    if item.destroyed == True:
      all_targets.remove(item)
      destroyed_targets.append(item)


def target_delay():
  global clock 
  clock += 1
  for item in all_targets:
    if clock >= item.threshold:
      target_sprites.add(item)

def movement():
  for item in target_sprites:
    item.rect.x += 1

#Creating a function which will deal with redrawing all sprites and updating the screen.
def refresh_window():
  if len(target_sprites) > 0:
   window.blit(bgr, (0,0))
  if len(all_targets) == 0 and len(destroyed_targets) == 6:
    window.blit(winner, (0,0))
  if len(target_sprites) > 0 and target_6.rect.x >= 575:
    window.blit(loser, (0,0))
  if len(target_sprites) > 0 and target_5.rect.x >= 575:
    window.blit(loser, (0,0))
  if len(target_sprites) > 0 and target_4.rect.x >= 575:
    window.blit(loser, (0,0))
  if len(target_sprites) > 0 and target_3.rect.x >= 575:
    window.blit(loser, (0,0))
  if len(target_sprites) > 0 and target_2.rect.x >= 575:
    window.blit(loser, (0,0))
  if len(target_sprites) > 0 and target_1.rect.x >= 575:
    window.blit(loser, (0,0))
  player_sprites.draw(window)
  target_sprites.draw(window)
  for item in all_bullets:
    pygame.draw.rect(window, BLUE, (item['x']-5, item['y']-5, 10, 10))
    b_hitbox = (item['x']-10, item['y']-10, 20, 20)
    pygame.draw.rect(window, BLUE, b_hitbox, 2)
  pygame.display.update()

#The game itself is contained in this while loop (game loop)
while exec:
  current_time = pygame.time.get_ticks() 

  for event in pygame.event.get():
    if event.type == pygame.QUIT:
            exec = False

  if event.type == pygame.MOUSEBUTTONDOWN:
        if event.button == 1:
            dx = event.pos[0] - (player_sprite.rect.x+ 100//2)
            dy = event.pos[1] - player_sprite.rect.y
            direction = pygame.math.Vector2(dx, dy).normalize()
            bullet = {'x': player_sprite.rect.x+42, 'y': player_sprite.rect.y, 'direction': direction}
            all_bullets.append(bullet)

  all_bullets_keep = []

  for item in all_bullets:
    item['x'] += item['direction'][0] # item['direction'][0] * 2
    item['y'] += item['direction'][1] # item['direction'][1] * 2

    if 0 < item['x'] < 800 and 0 < item['y'] < 575:
          all_bullets_keep.append(item)

  all_bullets = all_bullets_keep

  all_targets = [target_1, target_2, target_3, target_4, target_5, target_6]

  target_delay()

  movement()

  wrecked()
kungphu
  • 4,592
  • 3
  • 28
  • 37
j_yerbe
  • 111
  • 1
  • 9
  • I am voting to close this question as Stack Overflow is not a debug service. Please read [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask). – Rabbid76 Jan 02 '21 at 19:32

0 Answers0