2
votes

I have 2 type of balloons, one blue and one red (50% chance of eithera red of green balloon spawning). Player will score 2 points if it pops a green balloon and -2 if it pops a red one. -1 if green balloon flys away. I created the score counting but ony for one balloon, means that the score works (+2) if I pop both red and green balloons but I'm stuck here. I don't know if random.choice is the best way to get 50% of chance Here's one block of code:

class SpawnTime():
    def __init__(self, previous, time):
        self.time_previous = previous
        self.time= time


class Balloon():
    count = 0 

    def __init__(self):
        self.txr = random.choice(BALLOONS_TYPE)     
        self.width = self.txr.get_width()
        self.height = self.txr.get_height()        
        self.x = random.randint(0, SCREEN_WIDTH-self.width)
        self.y = SCREEN_HEIGHT
        self.rect = pygame.Rect(self.x, self.y, self.width, self.height)
        self.speed = random.randint(MIN_BALLOON_SPEED, MAX_BALLOON_SPEED)
        self.active = True
        Balloon.count +=1 # add 1 to balloon counter

    def update(self):
        self.rect.y -=  self.speed

        if self.rect.bottom <= 0:  # off top of screen
            self.active= False


    def draw(self):
        SCREEN.blit(self.txr, self.rect)


class Player():
    def __init__(self):
        self.x = 0 
        self.y = 0
        self.txr = pygame.image.load('assets/sprites/player.png')
        self.width = self.txr.get_width()
        self.height = self.txr.get_height()
        self.rect = pygame.Rect(self.x, self.y, self.width, self.height )
        self.score = 0

    def update(self):
        self.x, self.y = pygame.mouse.get_pos()
        self.rect.x= self.x
        self.rect.y= self.y


    def draw(self):
        SCREEN.blit(self.txr, self.rect)

    def clicked(self):
        # check if player has clicked on a balloon
        for b in balloons:
            if b.rect.collidepoint(self.x, self.y):
                self.score +=2
                b.active=False
                break # exit the for loop
1

1 Answers

0
votes

Do not use ranfom.choice, but use random.randrange to randomly selected an element by it's index (see random):

class Balloon():
    count = 0 

    def __init__(self):
        self.balloon_type = random.randrange(len(BALLOONS_TYPE))
        img = BALLOONS_TYPE[self.balloon_type]
        self.width = img.get_width()
        self.height = img.get_height()     
        # [...]
    
    def draw(self):
        SCREEN.blit(BALLOONS_TYPE[self.balloon_type], self.rect)

Now you can change the score dependent on the type of the balloon:

class Player():
    # [...]

    def clicked(self):
        # check if player has clicked on a balloon
        for b in balloons:
            if b.rect.collidepoint(self.x, self.y):
                
                if b.balloon_type == 0:
                    self.score += 2
                else
                    self.score -= 2

                b.active=False
                break # exit the for loop

(I don't know if the red or green balloon comes first, so you may need to swap +=2 and -=2)