Page 1 of 1

Fixated sprite with moving background using mouse hover

Posted: Wed May 15, 2019 12:10 pm
by MisterHarold
I want to ask how you could move the background while the character stays fixated just by hovering your mouse all over the screen. This effect makes it look like it's 3d

Re: Fixated sprite with moving background using mouse hover

Posted: Wed May 15, 2019 1:33 pm
by Per K Grok
MisterHarold wrote: Wed May 15, 2019 12:10 pm I want to ask how you could move the background while the character stays fixated just by hovering your mouse all over the screen. This effect makes it look like it's 3d
Sounds like you are looking for parallax scrolling.
You can find one thread on that subject here.
viewtopic.php?f=51&t=47482&p=499707&hil ... ax#p499707

Re: Fixated sprite with moving background using mouse hover

Posted: Wed May 15, 2019 9:14 pm
by lacticacid
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)