Layered Parallax Code
Posted: 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.
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.
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
Hope this helps others! I enjoyed this code a lot.
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.
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
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
Scene black
you have to go
Code: Select all
hide background_image
hide midground_image
hide sprite1
hide sprite2