Layered Parallax Code

A place for Ren'Py tutorials and reusable Ren'Py code.
Forum rules
Do not post questions here!

This forum is for example code you want to show other people. Ren'Py questions should be asked in the Ren'Py Questions and Announcements forum.
Post Reply
Message
Author
User avatar
Geckos
Veteran
Posts: 460
Joined: Fri Aug 17, 2012 8:33 am
Completed: Brilliant Shadows, Perceptions of the Dead, The Phantom Icecream Truck
Projects: Embers of Magic, Pale Spectrum, Perceptions of the Dead
Organization: Ithaqua Labs
Tumblr: geckosart
Deviantart: sitaart
Contact:

Layered Parallax Code

#1 Post by Geckos » Tue Jan 09, 2018 5:07 pm

Thought I'd share the parallax code that's floating around the forums, but in one place with all the fiddly bits fixed.

This code was not written originally by me. I am just sharing what I found on the forums.
This code lets you use different layers to give a parallax effect to in-game elements with the movements of your mouse.
parallax_example_potd1.gif
Perceptions of the Dead clip

Code: Select all


init 800 python:
    class MouseParallax(renpy.Displayable):
        def __init__(self,layer_info):
            super(renpy.Displayable,self).__init__()
            self.xoffset,self.yoffset=0.0,0.0
            self.sort_layer=sorted(layer_info,reverse=True)
            cflayer=[]
            masteryet=False
            for m,n in self.sort_layer:
                if(not masteryet)and(m<41):
                    cflayer.append("master")
                    masteryet=True
                cflayer.append(n)
            if not masteryet:
                cflayer.append("master")
            cflayer.extend(["transient","screens","overlay"])
            config.layers=cflayer
            config.overlay_functions.append(self.overlay)
            return
        def render(self,width,height,st,at):
            return renpy.Render(width,height)
        def parallax(self,m):
            func = renpy.curry(trans)(disp=self, m=m)
            return Transform(function=func)
        def overlay(self):
            ui.add(self)
            for m,n in self.sort_layer:
                renpy.layer_at_list([self.parallax(m)],n)
            return
        def event(self,ev,x,y,st):
            import pygame
            if ev.type==pygame.MOUSEMOTION:
                self.xoffset,self.yoffset=((float)(x)/(config.screen_width))-0.5,((float)(y)/(config.screen_height))-0.5
            return
    MouseParallax([(40,"farback"),(20,"back"),(-20,"front"),(-40,"inyourface")])
            
    def trans(d, st, at, disp=None, m=None):
        d.xoffset, d.yoffset = int(round(m*disp.xoffset)), int(round(m*disp.yoffset))
        return 0
        
      
Note: for this line of code "if(not masteryet)and(m<41):" the number must be higher than your highest number. Such as, for this code, 41 (as it is greater than the 40 for 'farback'.)

Example of code in use.

Code: Select all

show background_image onlayer farback
show midground_image onlayer back
show sprite1 onlayer front
show sprite2 onlayer inyourface
Other things to note: When using this layered method you must hide each layer individually to display new images. You cannot just 'wipe' the scene by putting
Scene black

you have to go

Code: Select all

hide background_image 
hide midground_image 
hide sprite1 
hide sprite2 
Hope this helps others! I enjoyed this code a lot.
Image ImageImage

User avatar
Borisu
Newbie
Posts: 2
Joined: Fri Apr 29, 2016 2:26 am
Contact:

Re: Layered Parallax Code

#2 Post by Borisu » Sun Jan 14, 2018 5:38 pm

Really interesting dude! I'll see in what this can be used

dirtydonnie
Newbie
Posts: 5
Joined: Mon Jan 01, 2018 5:44 pm
Soundcloud: dirtydonnie
Contact:

Re: Layered Parallax Code

#3 Post by dirtydonnie » Fri Mar 09, 2018 6:08 pm

Can this be applied to screen elements such as choice buttons as well? If so, how?

Post Reply

Who is online

Users browsing this forum: No registered users