Oh, I'm doing this too. The
most famous parallax background code is inflexible, glitchy, and hard to wrap your head around, so here's a slightly modified version of the one from
the most recent thread.
Some time ago, PyTom made some code for a displayable that follows the mouse cursor in response to a thread, and then Lena_Borodach modified it so that it moves slower in order for someone to be able to use it as a parallax background. I added a few changes of my own (separating "factor" into xfactor and yfactor, since i didn't want the horizontal and vertical movement to be the same speed.)
Code: Select all
init python:
class ParallaxBackground(renpy.Displayable):
def __init__(self, child):
super(ParallaxBackground, self).__init__()
self.child = renpy.displayable(child)
self.x = None
self.y = None
def render(self, width, height, st, at):
rv = renpy.Render(width, height)
if self.x is not None:
cr = renpy.render(self.child, width, height, st, at)
cw, ch = cr.get_size()
xfactor = -27 # this is how much your image will move horizontally. I added the negative because this way, the area the mouse moves to will be more visible to the player, rather than the opposite.
yfactor = -15 # this is how much your image will move vertically
csw,csh = (config.screen_width,config.screen_height)
cx = (self.x-csw/2)*xfactor/csw-cw/2
cy = (self.y-csh/2)*yfactor/csh-ch/2
rv.blit(cr, (cx,cy))
return rv
def event(self, ev, x, y, st):
if (x != self.x) or (y != self.y):
self.x = x
self.y = y
renpy.redraw(self, 0)