pkarthicbz pkarthicbz - 16 days ago 5
Python Question

How to remove duplicate images in pygame while moving

How to remove this duplicate of balloon while moving

enter image description here

Here's my code

lead_x = 0
lead_y = 400
balloonR_x = 400
balloonR_y = 400
balloonP_x = 500
balloonP_y = 400
balloonR_move = 50
#balloonB_x = 550
#balloonB_Y = 400

lead_y_change = 0

gameExit = False



while not gameExit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
gameExit = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
lead_y_change -= 10
elif event.key == pygame.K_DOWN:
lead_y_change += 10
if event.type == pygame.KEYUP:
lead_y_change = 0



lead_y += lead_y_change
gamedisplay.blit(bg, [0,0])
gamedisplay.blit(bow,[lead_x, lead_y])
while balloonR_y >= 50:
gamedisplay.blit(red_balloon, [balloonR_x, balloonR_y])
pygame.display.update()
pygame.event.clear(balloonR_y)
clock.tick(1)
balloonR_y -= balloonR_move
#gamedisplay.blit(red_balloon, [balloonR_x, balloonR_y])
gamedisplay.blit(purple_balloon, [balloonP_x, balloonP_y])
#gamedisplay.blit(blue_balloon, [balloonB_x, balloonB_y])
pygame.display.update()

clock.tick(20)


and also if i run the program first the balloon loop gets executed first in that time i cannot able to control the arrow.

Answer

You can't use while loop (and any long-running functions or sleep()).

You have to only change baloon position and let main while blits background and other elements (and does other things like handle events, make other animations, etc.)

More or less:

while not gameExit:

    # --- events ---

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            gameExit = True
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_UP:
                lead_y_change -= 10
            elif event.key == pygame.K_DOWN:
                lead_y_change += 10
        if event.type == pygame.KEYUP:
            lead_y_change = 0

    # --- updates (without draws) ---

    lead_y += lead_y_change

    if balloonR_y >= 50:
        balloonR_y -= balloonR_move

    # --- draws (without updates) ---

    gamedisplay.blit(bg, [0,0])
    gamedisplay.blit(bow,[lead_x, lead_y])

    gamedisplay.blit(red_balloon, [balloonR_x, balloonR_y])

    gamedisplay.blit(purple_balloon, [balloonP_x, balloonP_y])
    #gamedisplay.blit(blue_balloon, [balloonB_x, balloonB_y])
    pygame.display.update()

    clock.tick(20)