The scrolling land can be imagined as a endless row of tiles. If you want to draw the background at a certain position, then you have to calculate the position of the tile relative to the screen by the modulo (%
) operator. The position of the 2nd part tile is shifted by the width of the land surface (e.g. land_0.get_width()
).
This means it is sufficient to define 1 position (offset) for each background land, because the 2nd position can be calculated by the width of the land.
Write a function which shifts the land by a certain offset (speed
), draws the land surface and returns the new position:
def drawLand(pos, height, speed, land):
width = land.get_width()
pos = (pos + speed) % width
screen.blit(land, (pos, height))
pos_2 = pos + width if pos < 0 else pos - width
screen.blit(land, (pos_2, height))
return pos
Call the function for each land in the main loop of the application:
pos_l0, hight_land = 2, 500
pos_l1, hight_land_1 = -1000, 400
s_speed = 2
while a:
# [...]
screen.blit(bg,(0, 0))
pos_l1 = drawLand(pos_l1, hight_land_1, -s_speed, land_1)
pos_l0 = drawLand(pos_l0, hight_land, -s_speed // 2, land_0)
pg.display.update()
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…