0
bgx = 0
bgx2 = bgx + width
def move():
    bgx -= 40
    bgx2 -= 40
While True:
    if bgx <= width *-1:
        bgx = width
    if bgx2 <= width*-1:
        bgx2 = width

display.blit(bg,[bgx,bgy])
display.blit(bc,[bgx2,bgy2])

I am trying to create a scrolling bg, whenever I click a key move function is called

When it's time for the second image with bgx2 to show up on screen ,a glitch like something happens like in the attached image

What is the reason for that Is the code drawing a new "second" image every loop instead of scrolling?

1:glitch that happens

Rabbid76
  • 202,892
  • 27
  • 131
  • 174

1 Answers1

0

In your code bgx is in range [-width, 0] and bbx2 is in range [0, width]. Therefore you have to increase bgx by width if bgx <= -width and bgx2 if bgx2 <= 0:

bgx = 0
bgx2 = bgx + width

def move():
    global bgx, bgx2 
    bgx -= 40
    bgx2 -= 40
    if bgx <= -width:
        bgx += width
    if bgx2 <= 0:
        bgx2 += width

However, I recommend to use the modulo (%) operator to calculate the coordinates:

bgx = 0
bgx2 = bgx + width

def move():
    global bgx, bgx2 
    bgx2 = (bgx2 - 40) % width
    bgx = bgx2 - width

See also Making the background move sideways in pygame

Rabbid76
  • 202,892
  • 27
  • 131
  • 174