The major problem I'm having is how to calculate and apply the smoothness to the displayable movement, I tried many different ways without much result, so I decided to remove the failed attemps and put the base code below
NOTE: I know there are a few already done parallax out there but my main focus is learning and not copy-pasting.
Code: Select all
init python:
import pygame as pyg
class Parallax(renpy.Displayable):
def __init__(self, child, dist=.05, xanchor=0.5, **kwargs):
super().__init__(**kwargs)
self.child = renpy.displayable(child)
self.dist = dist
self.xanchor = xanchor
def render(self, width, height, st, at):
left_click = pyg.mouse.get_pressed()[0]
xmouse, ymouse = renpy.get_mouse_pos()
child = renpy.render(self.child, width, height, st, at)
render = renpy.Render(width, height)
cw, ch = child.get_size()
pos = (width * self.xanchor)
xpos = (pos - (cw * .5))
off = (cw * self.dist)
if left_click:
xpos += (xmouse - pos) / off
x, y = (xpos, height - ch)
render.blit(child, (x, y))
renpy.redraw(self, 0)
return (render)
def visit(self):
return [ self.child ]
Two Boxes Gif Here an example on how it looks
any suggestion or improvement is welcome, criticism too.