【问题标题】:How to remove duplicate images in pygame while moving如何在移动时删除pygame中的重复图像
【发布时间】:2016-11-20 09:49:07
【问题描述】:

如何在移动时移除这个重复的气球

这是我的代码

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)

如果我先运行程序,气球循环会在那个时候首先执行,我无法控制箭头。

【问题讨论】:

  • 你不能使用while balloonR_y >= 50,因为这样你就不会删除旧图像的背景。

标签: python python-2.7 pygame


【解决方案1】:

您不能使用while 循环(以及任何长时间运行的函数或sleep())。

你只需要改变气球的位置,让 main while blits 背景和其他元素(以及处理事件、制作其他动画等其他事情)

或多或少:

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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-11-21
    • 1970-01-01
    • 1970-01-01
    • 2019-06-26
    • 1970-01-01
    • 2022-06-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多