【问题标题】:How do you make a scrolling background in pygame? [duplicate]你如何在pygame中制作滚动背景? [复制]
【发布时间】:2020-06-20 00:37:12
【问题描述】:

我正在尝试制作一个小型汽车游戏,但我遇到了一个问题,背景从上到下滚动两次,然后冻结,在此过程中形成了奇怪的形状。由于某种原因,如果背景从下向上滚动,则不会发生错误。

这里是滚动背景的代码

def main():
    run = True
    FPS = 60
    clock = pygame.time.Clock()

    BGY = 0
    BGY2 = -BG.get_height()

    def redraw():
        win.blit(BG, (0,BGY-100))
        win.blit(BG, (0,BGY2-100))

        pygame.display.update()
    while run:
        clock.tick(FPS)
        redraw()

        BGY += 2.5
        BGY2 += 2.5
        
        if BGY < BG.get_height() * -1:
            BGY = -BG.get_height()
        if BGY2 < BG.get_height() * -1:
            BGY2 = -BG.get_height()

main()

【问题讨论】:

    标签: python python-3.x pygame


    【解决方案1】:

    背景向下移动,所以条件必须是:

    if BGY > BG.get_height():
        BGY = -BG.get_height()
    if BGY2 > BG.get_height():
        BGY2 = -BG.get_height() 
    

    如果背景必须向上移动,那么你必须递减BGYBGY2

    BGY -= 2.5
    BGY2 -= 2.5
    
    if BGY < BG.get_height() * -1:
        BGY = BG.get_height()
    if BGY2 < BG.get_height() * -1:
        BGY2 = BG.get_height() 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-10-05
      • 2019-08-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-07
      • 1970-01-01
      相关资源
      最近更新 更多