方法:让背景图像分别在(0,0)和(0,-img.heigh)两个位置向下移动它们,当其中一个位于(0,img.heigth)位置时,再次将其放置在(0,-img.heigh)位置。
具体代码:
import pygame
import sys
import pygame.sprite as sprite
theClock = pygame.time.Clock()
# 载入图片
background = pygame.image.load('background.gif')
background_size = background.get_size()
background_rect = background.get_rect()
screen = pygame.display.set_mode(background_size)
w,h = background_size
# 背景1 初始位置
x, y = 0, 0
# 背景2 初始位置
x1, y1 = 0, -h
running = True
while running:
screen.blit(background,background_rect)
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# 不断更新位置、实现背景滚动
y1 += 5
y += 5
screen.blit(background,(x,y))
screen.blit(background,(x1,y1))
if y > h:
y = -h
if y1 > h:
y1 = -h
pygame.display.flip()
pygame.display.update()
theClock.tick(10)