So I wanted to do a scrolling background that is fullscreen, no matter which monitor. I used a 7680x2160 background picture which will be scaled down to whatever monitor the user uses, in my case to a 2560x1440 monitor -> 5120x1440 picture For some reason, I get a black bar after every time the background starts again. the error:
I don't want to fix it just by moving it by an amount of pixels that I tested out but I want to find the actual source of the problem and fix it. Does anyone see the mistake in my code?
Also, I'm not sure whether I should use if abs(scroll) > bg.get_width():
or if abs(scroll) >= bg.get_width():
, what do you think?
import math
import pygame as py
py.init()
clock = py.time.Clock()
from screeninfo import get_monitors
for monitor in get_monitors():
FrameWidth = monitor.width
Top_Bar_Height = 70
FrameHeight = monitor.height - Top_Bar_Height
#PYGAME FRAME WINDOW
py.display.set_caption("Endless Scrolling in pygame")
screen = py.display.set_mode((FrameWidth, FrameHeight))
#IMAGE
bg = py.transform.scale(py.image.load("background.png").convert(),(FrameWidth*2, FrameHeight))
#DEFINING MAIN VARIABLES IN SCROLLING
scroll = 0
#CHANGE THE BELOW 1 TO UPPER NUMBER IF
#YOU GET BUFFERING OF THE IMAGE
#HERE 1 IS THE CONSTANT FOR REMOVING BUFFERING
tiles = math.ceil(FrameWidth / bg.get_width()) + 1
#MAIN LOOP
while 1:
# THIS WILL MANAGE THE SPEED OF
# THE SCROLLING IN PYGAME
clock.tick(60)
# APPENDING THE IMAGE TO THE BACK
# OF THE SAME IMAGE
i = 0
while(i < tiles):
screen.blit(bg, (bg.get_width()*i+scroll, 0))
i += 1
# FRAME FOR SCROLLING
scroll -= 6
# RESET THE SCROLL FRAME
if abs(scroll) > bg.get_width():
scroll = 0
# CLOSING THE FRAME OF SCROLLING
for event in py.event.get():
if event.type == py.QUIT:
quit()
py.display.update()
py.quit()
I tried changing scroll -=6
to scroll -=5
since my monitor_width
is dividable by 5 but not by 6 and changed if abs(scroll) > bg.get_width():
to if abs(scroll) >= bg.get_width():
, but that did not fix the problem.