CDD Parallax Effect w/ Smoothness
Posted: Sat Apr 27, 2024 5:17 pm
I've messing with CDD's for a while and I'm trying to make a parallax effect with some sort of smoothness when you click and drag the mouse around the screen (left to right)
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
This works as expected by positioning the display in the correct position and calculating where it should be, but it just sets the position abruptly, and I want this to happen easily, I thought about changing it to a Transform() but I don't know if that's valid
Two Boxes Gif Here an example on how it looks
any suggestion or improvement is welcome, criticism too.
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.