当背景沿着侧面滚动时,图像的末端会拉伸,而图像的其余部分并不会出现在相同的山丘上。然后突然重置。此外,图像的一部分(矩形)从图像的其余部分移开,使其不均匀并保持不变,直到它从视图中消失。
这是我用来侧滚的代码
import pygame, sys, time, random
from pygame.locals import *
class Background(pygame.sprite.Sprite): #Creates space background
def __init__(self, image_file, location):
pygame.sprite.Sprite.__init__(self) #call Sprite initializer
self.bgimage = pygame.image.load(image_file)
self.bgimage = pygame.transform.scale(self.bgimage, (1333, 600))
self.rectBGimg = self.bgimage.get_rect()
self.bgY1 = 0
self.bgX1 = 0
self.bgY2 = 0
self.bgX2 = self.rectBGimg.width
self.movingUpSpeed = 5
def update(self):
self.bgX1 -= self.movingUpSpeed
self.bgX2 -= self.movingUpSpeed
if self.bgX1 <= -self.rectBGimg.height:
self.bgX1 = self.rectBGimg.height
if self.bgX2 <= -self.rectBGimg.height:
self.bgY2 = self.rectBGimg.height
def render(self):
screen.blit(self.bgimage, (self.bgX1, self.bgY1))
screen.blit(self.bgimage, (self.bgX2, self.bgY2))
pygame.init()
FPS = 15 # frames per second setting
clock = pygame.time.Clock()
window_width = 1333
window_height = 600
# set up the window
screen = pygame.display.set_mode((window_width, window_height))
pygame.display.set_caption('Deterred Journey')
BackGround = Background('scrollingBackground.png', [0,0])
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
#Adds images and text
BackGround.render()
BackGround.update()
pygame.display.flip()
pygame.display.update()
clock.tick(30)
pygame.quit()
sys.exit()
答案 0 :(得分:0)
看起来我的更新功能正在检查bgX1和bgX2对高度导致位移,我设置self.bgY2 = self.rectBGimg.height
导致伸长。由于它需要垂直滚动,因此bgX2必须设置为宽度
更新的功能应该看起来像
def update(self):
self.bgX1 -= self.movingUpSpeed
self.bgX2 -= self.movingUpSpeed
if self.bgX1 <= -self.rectBGimg.width:
self.bgX1 = self.rectBGimg.width
if self.bgX2 <= -self.rectBGimg.width:
self.bgX2 = self.rectBGimg.width