Fixated sprite with moving background using mouse hover

Discuss how to use the Ren'Py engine to create visual novels and story-based games. New releases are announced in this section.
Forum rules
This is the right place for Ren'Py help. Please ask one question per thread, use a descriptive subject like 'NotFound error in option.rpy' , and include all the relevant information - especially any relevant code and traceback messages. Use the code tag to format scripts.
Post Reply
Message
Author
MisterHarold
Regular
Posts: 51
Joined: Tue Jul 17, 2018 10:32 am
Location: Philippines
Contact:

Fixated sprite with moving background using mouse hover

#1 Post 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

User avatar
Per K Grok
Miko-Class Veteran
Posts: 882
Joined: Fri May 18, 2018 1:02 am
Completed: the Ghost Pilot, Sea of Lost Ships, Bubbles and the Pterodactyls, Defenders of Adacan Part 1-3, the Phantom Flyer
itch: per-k-grok
Location: Sverige
Contact:

Re: Fixated sprite with moving background using mouse hover

#2 Post 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

lacticacid
Regular
Posts: 36
Joined: Fri Nov 23, 2018 6:44 pm
Contact:

Re: Fixated sprite with moving background using mouse hover

#3 Post 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)
~There is almost always a better, easier way to approach a problem.~

Post Reply

Who is online

Users browsing this forum: Google [Bot]