首页 > 解决方案 > 移动游戏背景不重绘本身

问题描述

所以我试图制作一个移动的背景,但是背景并没有连续移动。它显示黑屏,背景不会连续闪烁,给人的印象是背景在移动。

在此处输入图像描述

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

标签: pythonbackgroundpygame

解决方案


中有一个错字UpdateGame。更改background_xposbackground_xpos2第 2 行UpdateGame

window.blit(background,(background_xpos,0))

window.blit(background,(background_xpos2,0))

推荐阅读