Scrolling big sprite in background

574 views Asked by At

I'm trying to convert a simple game written in pygame in pyglet. I almost figure out everything, but now I'm facing this situation, I have a background sprite like this example:

enter image description here

In my pygame code I use to write this to let it scrolls smoothly in the background:

base_shift = self.graphic.IMAGES['base'].get_width() - self.COMMON_CONST['screenWidth']
while True:
      base_x = -((-base_x + 3) % base_shift)
      self.screen.blit(self.graphic.IMAGES['base'], (base_x, y))

Basically the image is longer than the actual screen and once it passes the delta value between image X and screen X, the image is set to 0 again.

I'm trying to do the same in pyglet inside the def update(self, dt): function but I have no idea exactly how to do it. I mean I understand the meaning of dt but still.. To move the image now I do something like this:

def update(self, dt):
    self.base.x -= 15 * dt

Of course this will continue to move and it does not reset his position. How can I replicate the behavior that I have done in pygame?

thanks

UPDATE

I try this code:

def update(dt):
   self.base.x =  -((-(self.base.x - 15 * dt)) % base_shift)

It seems to work but it's not smooth when is back to 0 is not perfect aligned.

UPDATE/2 TEMPORARY SOLUTION

I finally went to another completely different solution. I split the image in a single pattern something like this:

enter image description here

and then I repeat it many times to fill the screen width plus 2. Then I move all of them and when the first goes completely out of the screen size, I remove from the array and add a new one.

I don't know if there is a better solution so far is ok.

0

There are 0 answers