如何在pygame中制作滚动背景?

2024-09-29 01:31:57 发布

您现在位置:Python中文网/ 问答频道 /正文

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

下面是滚动背景的代码

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()

Tags: rungetmaindefpygamewinbg背景
1条回答
网友
1楼 · 发布于 2024-09-29 01:31:57

背景向下移动,因此条件必须为:

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() 

相关问题 更多 >