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
Newbie
Posts: 18
Joined: Tue Jul 17, 2018 10:32 am
Location: Philippines
Contact:

Fixated sprite with moving background using mouse hover

#1 Post by MisterHarold » 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

User avatar
Per K Grok
Miko-Class Veteran
Posts: 644
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
Projects: Defenders of Adacan Part 2
Deviantart: pekj
itch: per-k-grok
Location: Sverige
Contact:

Re: Fixated sprite with moving background using mouse hover

#2 Post by Per K Grok » Wed May 15, 2019 1:33 pm

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
Image Visual Story Telling, DeviantArt Group for animations, comics, games and illustrations https://www.deviantart.com/acgi

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 » Wed May 15, 2019 9:14 pm

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: Bing [Bot], Google [Bot]