Powerups spawn with list but will not respawn

Viewed 54

I am making a driving game and I have been working on powerups that spawn. I have made a system in the car class (incoming traffic) that will make a random variable and if it lands between the right amount of numbers, it will append a powerup and a powerup spawns. But know if another car goes by and it prints (Hey it happened) and nothing will spawn. Any help is appreciated

Here is the powerups class.

class powerups(pygame.sprite.Sprite):
    def __init__(self):
        self.x_pos = -20
        self.KC = pygame.image.load('kyle coin3.png')
        self.rect = self.KC.get_rect(topleft= (self.x_pos, 0))
        self.random = int
        self.roadleft = 150
        self.roadright = 300
        self.fg = True
        self.rect.x = random.randint(self.roadleft, self.roadright)

    def draw(self, win):
        win.blit(self.KC, self.rect.topleft)
    def move(self):
        self.rect.y += 5
    def collisioncheck (self, anotherobject):
        if self.rect.colliderect(anotherobject):
            print("A collsion with a powerup has happend ")
            self.rect.x = 600
            self.grassdamage = 0
            player.damage1 =  False
            player.damage2 = False
            player.damage3= False
            player.damage4= False
            player.damage5 = False
            player.damage6 = False
            player.damage7 = False
            player.damage8 = False

Here is where the powerups are appeneded.

 if self.random > 5:
                print(' hey it happened')
                new_powerup = powerups()
                powerup_list.append(new_powerup)
            else:
                self.random = 10

And here is the where the powerups are being called.

    for new_powerup in powerup_list:
        powerup.draw(win)
        powerup.move()
        powerup.collisioncheck(player.rect)
1 Answers

The name of the loop variable is different from the name of the variable you used in the loop. In the for-loop statement you used the name new_powerup, but inside the loop you used the name powerup. Change the name of the loop variable:

for powerupin powerup_list:
    powerup.draw(win)
    powerup.move()
    powerup.collisioncheck(player.rect)
Related