vendredi 24 septembre 2021

Can't reposition images stored in a list correctly | Pygame

I am making a game based on balloons and a gun that's job is to shoot the balloons. The 7 balloon images are stored into a list called colors, and another list called balloon_list has all the randomly generated x values for the placement of the balloons. What should happen when you hit a balloon is it should disappear and then randomize the x-position of the balloon again. My problem is that when I hit a certain balloon, instead of randomizing the x position and blitting it to the screen, it continually does that and it shows the balloon popping up at different places at once. What I tried was shuffling the list (balloon_list) of all the x values. Here is my code:

import random as r
import sys


pg.init()


radius = 30
diameter = 2 * radius
num_balloons = 7

bullets_colors_ls = []
iterator = -1




def create_balloons():
    global balloon_list
    global colors

    for i in range(num_balloons):
        while True:
            candidate = r.randint(0, 500)
            if all(abs(candidate-x) >= diameter for x in balloon_list):
                break
        balloon_list.append(candidate)

def draw_balloons(y):
    for i in range(num_balloons):
        screen.blit(colors[i], (balloon_list[i] , y-50))


def check_collisions(x, y):
    global hit_var
    global hit
    global score
    global scoretext
    
    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):
            hit = True
            hit_var = i
            print(f'hit balloon: {i}')
            colors[i].fill((0,0,0))
            screen.fill((0,0,0))

            


            
        


        
# Vars #
x = 0
y = 250
velocity = 5
score = 0
hit = False
testvar1 = True
clock = pg.time.Clock()


screen = pg.display.set_mode((688 ,387)) # Size of the screen #
screen.fill('#ffffff')
caption = pg.display.set_caption("Remember") # Title of the window #

balloon_list = []
b1 = pg.image.load('balloons/1.png').convert_alpha()
b1 = pg.transform.scale(b1, (63,131))
b2 = pg.image.load('balloons/2.png').convert_alpha()
b2 = pg.transform.scale(b2, (63,131))
b3 = pg.image.load('balloons/3.png').convert_alpha()
b3 = pg.transform.scale(b3, (63,131))
b4 = pg.image.load('balloons/4.png').convert_alpha()
b4 = pg.transform.scale(b4, (63,131))
b5 = pg.image.load('balloons/5.png').convert_alpha()
b5 = pg.transform.scale(b5, (63,131))
b6 = pg.image.load('balloons/6.png').convert_alpha()
b6 = pg.transform.scale( b6, (63,131))
b7 = pg.image.load('balloons/7.png').convert_alpha()
b7 = pg.transform.scale(b7, (63,131))
colors = [b1, b2, b3, b4, b5, b6, b7]




gun = pg.image.load('game-gun.png').convert_alpha()
gun = pg.transform.scale(gun, (150,150))

create_balloons()



pg.display.flip() # Updating #

running = True # Game loop bool #

while running: # Game loop #
    clock.tick(60)
    
    if hit == True:
        r.shuffle(balloon_list)
        
        
        
    for event in pg.event.get():
        if event.type == pg.QUIT:
            pg.quit()
            sys.exit()
        if event.type == pg.KEYDOWN:
            if event.key == pg.K_ESCAPE:
                pg.quit()
                sys.exit()

    draw_balloons(y)
    keys = pg.key.get_pressed()

    x += keys[pg.K_RIGHT] - keys[pg.K_LEFT] * velocity
    x -= keys[pg.K_LEFT] - keys[pg.K_RIGHT] * velocity
   
    if keys[pg.K_SPACE]:
        check_collisions(x, y)
        print(balloon_list)
        

        



        
        
     
    screen.blit(gun, (x, y))
    pg.display.update()

The gun and balloon images can be downloaded here: IMAGES

I think the problem is related to blitting the images inside of the while loop so it is continually shuffling the x position in the balloon_list forever, but I'm not sure how to fix it.

Appreciate any help, Thank you




Aucun commentaire:

Enregistrer un commentaire