Pygame:两层滚动背景,你能帮帮我吗? [英] 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()
我希望第一层快速滚动,第二层(在后台滚动缓慢),我在前 20 秒内工作得很漂亮,但在此之后它是随机的:一层消失或一层正在闪烁,太奇怪了...
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...
推荐答案
滚动的土地可以想象成一排无尽的瓷砖.如果要在某个位置绘制背景,则必须通过模 (%
) 运算符计算 tile 相对于屏幕的位置.第二部分瓦片的位置移动了地表的宽度(例如land_0.get_width()
).
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()
).
这意味着为每个背景land定义1个位置(偏移量)就足够了,因为第二个位置可以通过土地的宽度来计算.
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.
编写一个函数,将陆地移动一定的偏移量(speed
),绘制陆地表面并返回新位置:
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
在应用程序的主循环中为每个land调用函数:
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()
这篇关于Pygame:两层滚动背景,你能帮帮我吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!