Question

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

Was it helpful?

Solution

(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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top