문제

When I run my game which has a scrolling background it periodically starts to glitch out the right side of the screen. The screen will do it even if the speed of the background is 4x slower than previously tested. After the glitchy part has moved for a while everything goes back to normal until it happens again.

The piece of code that controls the animation is this (got this somewhere off the Internet):

def background():  
    global screen, bgOne, bgTwo, bgOne_x, bgTwo_x
    screen.blit(bgOne, (bgOne_x, 0))
    screen.blit(bgTwo, (bgTwo_x, 0))


    bgOne_x -= 1
    bgTwo_x -= 1

    if bgOne_x == -1 * bgOne.get_width():           
        bgOne_x = bgTwo_x + bgTwo.get_width()       
    if bgTwo_x == -1 * bgTwo.get_width():           
        bgTwo_x = bgOne_x + bgOne.get_width()

Picture of the glitch:

enter image description here

도움이 되었습니까?

해결책

(Posted on behalf of the OP).

The cause turned out to be a simple oversight. For anyone else encountering this problem: please check the dimensions of the background picture you're using and the dimensions of the display which Pygame uses. In this case the width was shorter (807) than the screen itself (1024). I hope this helps beginners like me in the future.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top