游戏的背景在运动时会留下痕迹或痕迹

问题描述 投票:0回答:1

所以我最近一直在尝试在游戏中实现滚动背景,但是当我向前移动时,游戏背景移到左侧会留下很大的痕迹,例如:

enter image description here

代码在下面:

#Game background
background = pg.transform.scale(pg.image.load('splash.jpg'), (WIDTH,HEIGHT))#background size being adjusted to fit a particular screen size
background_xpos = 0 #the x position of the background is at 0 of (0,0)
background_xpos2 = background.get_width() #obtains the width of the background at a certain coordinate 

def UpdateGame():    
    window.blit(background,(background_xpos,0))
    window.blit(background,(background_xpos,0))
    pg.display.update()

run = True  #while the game is running
while run:
    clock.tick(FPS) #determines the amount of frames per second
    background_xpos = background_xpos - 2
    background_xpos2 = background_xpos - 2

    if background_xpos < background.get_width() * -1:
        background_xpos = background.get_width()

    if background_xpos2 < background.get_width() * -1:
       background_xpos2 = background.get_width()
python pygame horizontal-scrolling
1个回答
0
投票
您必须先清除屏幕。

#Game background background = pg.transform.scale(pg.image.load('splash.jpg'), (WIDTH,HEIGHT))#background size being adjusted to fit a particular screen size background_xpos = 0 #the x position of the background is at 0 of (0,0) background_xpos2 = background.get_width() #obtains the width of the background at a certain coordinate def UpdateGame(): window.blit(background,(background_xpos,0)) window.blit(background,(background_xpos,0)) pg.display.update() run = True #while the game is running while run: screen.fill((0,0,0)) clock.tick(FPS) #determines the amount of frames per second background_xpos = background_xpos - 2 background_xpos2 = background_xpos - 2 if background_xpos < background.get_width() * -1: background_xpos = background.get_width() if background_xpos2 < background.get_width() * -1: background_xpos2 = background.get_width()

© www.soinside.com 2019 - 2024. All rights reserved.