You could try out: PyParallax looks like it does what you want plus more.
Vertical Scrolling Background in Pygame
-
03-04-2022 - |
Question
I am trying to make my background image list to reposition themselves as soon as they hit the bottom of my screen....
I have a Camera Object which updates its self.state to the position of my moving ball.
Here's my Background Class:
class Background(pygame.sprite.Sprite):
def __init__(self, x, y):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load('images/clouds.jpg').convert()
self.x = x
self.y = y
self.rect = self.image.get_rect()
self.rect.left, self.rect.top = self.x, self.y
def update(self,cam):
self.rect.left = self.x + (0.2*cam.state.left)
self.rect.top = self.y + (0.2*cam.state.top)
The 0.2* is to make it move slower than my actual camera shift.
Here's how I build the bg images before my big main loop:
all_backgrounds = pygame.sprite.Group()
#building bg images
for i in xrange((WIDTH+BG_WIDTH)/BG_WIDTH):
for j in xrange((HEIGHT+BG_HEIGHT)/BG_HEIGHT):
all_backgrounds.add(Background(BG_WIDTH*i,BG_HEIGHT*j))
Then in my main loop:
......
all_backgrounds.update(cam)
......
all_backgrounds.draw(screen)
I am looking for good suggestions on how to make it work smoothly. I tried using if statements in my Background.update() but it makes it laggy and issues with tear & overlap lines because of Int, float conversions.
Solution
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow