Skip to content Skip to sidebar Skip to footer

How To Display The Correct Amount Of Images After Each Collision?

I am trying to display the right amount of balloons (the images) each time a collision is detected. Note the collision works perfectly fine, but I am having trouble figuring out ho

Solution 1:

The answer is hidden in the question:

[...] the number of balloons should be like 7,7 6,6 5,5 4,4 3,3 2,2 1,1

Create a list with the number of balloons and a variable that states the current index in the list:

num_balloon_list = [7, 7, 6, 6, 5, 5, 4, 4, 3, 3, 2, 2, 1, 1, 0]
balloon_list_index = 0
num_balloons = num_balloon_list[balloon_list_index]

If a collision is detected, increment balloon_list_index and get the new number from the list:

def check_collisions(x, y):
    global hit_var
    global hit
    global score
    global scoretext
    global bg_bool
    global num_balloons, balloon_list_index 
    
    for i in range(num_balloons):
        gun_rect = gun.get_rect(topleft = (x,y))
        gun_mask = pg.mask.from_surface(gun)

        balloon_rect = colors[i].get_rect(topleft = (balloon_list[i], y-100))
        balloon_mask = pg.mask.from_surface(colors[i])

        offset = (balloon_rect.x - gun_rect.x), (balloon_rect.y - gun_rect.y)
        if gun_mask.overlap(balloon_mask, offset):
            
            bg_bool = not bg_bool
            balloon_list_index += 1
            if balloon_list_index < len(num_balloon_list):
                num_balloons = num_balloon_list[balloon_list_index]

            hit_var = i
            print(f'hit balloon: {i}')
            break

If num_balloons is 0, the game is over.


Post a Comment for "How To Display The Correct Amount Of Images After Each Collision?"