Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
912 views
in Technique[技术] by (71.8m points)

python - Pygame : Two layered scrolling background, can you help me?

I would like create a two layered scrolling background but everytime I run the programm below it make everything wrong, and I don't know why :/ Can you help me ?

I've try the code below :

'bg scrolling try'

import pygame as pg
from pygame.locals import *
pg.init()

screen = pg.display.set_mode((1920, 1080), pg.NOFRAME)
a = True
land_0 = pg.image.load('sprites/land_0.png').convert_alpha()
land_1 = pg.image.load('sprites/land_1.png').convert_alpha()
bg = pg.image.load('sprites/bg.png').convert_alpha()

pos_l0_0 = 2
pos_l0_1 = 1920
pos_l1_0 = -1000
pos_l1_1 = 920
hight_land = 500
hight_land_1 = 400
s_speed = 2
while a:
     screen.blit(bg,(0, 0))
     pos_l1_0 = pos_l1_0 - s_speed
     pos_l1_1 = pos_l1_1 - s_speed
     if pos_l1_0 == - 1920:
          pos_l1_0 = 1920
     elif pos_l1_1 == - 1920:
          pos_l1_0 = 1920
     screen.blit(land_1,(pos_l1_0, hight_land_1))
     screen.blit(land_1,(pos_l1_1, hight_land_1))

     # 2nd
     pos_l0_0 = pos_l0_0 - s_speed/2
     pos_l0_1 = pos_l0_1 - s_speed/2
     if pos_l0_0 == - 1920:
          pos_l0_0 = 1920
     elif pos_l0_1 == - 1920:
          pos_l0_0 = 1920
     screen.blit(land_0,(pos_l0_0, hight_land))
     screen.blit(land_0,(pos_l0_1, hight_land))
     pg.display.update()

I would like the first layer scroll fast and the second ( in background scroll slow ), I working prettry during the first 20 seconds but after this it random : one layer diseappear or one is blitting, so strange...

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...