The most complex, but most powerful, way of customizing Ren'Py's behavior is to use a creator-defined displayable. A creator-defined displayable is allowed to take arbitrary pygame events. It can also render other displayables, and place them at arbitrary locations on the screen. This makes it suitable for creating 2D mini-games that cannot be expressed with the tools Ren'Py gives you. (But see also the section sprites, which describes a higher-level way of accomplishing many of the same things.)
Creator-defined displayables are programmed entirely in Python, and we encourage you to have a reasonable degree of skill at object-oriented Python programming before you begin creating one.
Here's an example of a creator-defined displayable. This displayable changes renders its child with an alpha that is determined by the distance of the mouse pointer from the center of the child.
init python:
import math
class Appearing(renpy.Displayable):
def __init__(self, child, opaque_distance, transparent_distance, **kwargs):
# Pass additional properties on to the renpy.Displayable
# constructor.
super(Appearing, self).__init__(**kwargs)
# The child.
self.child = renpy.displayable(child)
# The distance at which the child will become fully opaque, and
# where it will become fully transparent. The former must be less
# than the latter.
self.opaque_distance = opaque_distance
self.transparent_distance = transparent_distance
# The alpha channel of the child.
self.alpha = 0.0
# The width and height of us, and our child.
self.width = 0
self.height = 0
def render(self, width, height, st, at):
# Create a transform, that can adjust the alpha channel of the
# child.
t = Transform(child=self.child, alpha=self.alpha)
# Create a render from the child.
child_render = renpy.render(t, width, height, st, at)
# Get the size of the child.
self.width, self.height = child_render.get_size()
# Create the render we will return.
render = renpy.Render(self.width, self.height)
# Blit (draw) the child's render to our render.
render.blit(child_render, (0, 0))
# Return the render.
return render
def event(self, ev, x, y, st):
# Compute the distance between the center of this displayable and
# the mouse pointer. The mouse pointer is supplied in x and y,
# relative to the upper-left corner of the displayable.
distance = math.hypot(x - (self.width / 2), y - (self.height / 2))
# Base on the distance, figure out an alpha.
if distance <= self.opaque_distance:
alpha = 1.0
elif distance >= self.transparent_distance:
alpha = 0.0
else:
alpha = 1.0 - 1.0 * (distance - self.opaque_distance) / (self.transparent_distance - self.opaque_distance)
# If the alpha has changed, trigger a redraw event.
if alpha != self.alpha:
self.alpha = alpha
renpy.redraw(self, 0)
# Pass the event to our child.
return self.child.event(ev, x, y, st)
def visit(self):
return [ self.child ]
To use the creator-defined displayable, we can create an instance of it, and add that instance to the screen.
screen alpha_magic:
add Appearing("logo.png", 100, 200):
xalign 0.5
yalign 0.5
label start:
show screen alpha_magic
"Can you find the logo?"
return
A creator-defined displayable is created by subclassing the renpy.Displayable class. A creator-defined displayable must override the render method, and may override other methods as well.
A displayable object must be pickleable, which means it may not contain references to objects that cannot be pickled. Most notably, Render objects cannot be stored in a creator-defined displayable.
Since we expect you to override the methods of the displayable class, we'll present them with the self parameter.
Base class for creator-defined displayables.
A subclass may override the constructor, perhaps adding new parameters. If it does, it should pass all unknown keyword arguments to the renpy.Displayable constructor, using code like:
super(MyDisplayable, self).__init__(properties)
Subclasses must override this, to return a renpy.Render object. The render object determines what, if anything, is shown on the screen.
The render method is called when the displayable is first shown. It can be called again if renpy.redraw() is called on this object.
The event method is called to pass a pygame event to the creator-defined displayable. If the event method returns a value other than None, that value is returned as the result of the interaction.
The event method exists on other displayables, allowing the creator-defined displayable to pass on the event.
An event is generated at the start of each interaction, and renpy.timeout() can be used to cause another event to occur.
This method is called at the start of each interaction. It can be used to trigger a redraw, and probably should be used to trigger a redraw if the object participates in rollback.
If the displayable has child displayables, this method should be overridden to return a list of those displayables. This ensures that the per_interact methods of those displayables are called, and also allows images used by those displayables to be predicted.
creator-defined displayables work with renpy.Render objects. Render objects are returned by calling the renpy.render() function on a displayable. A creator-defined displayable should create a Render object by calling renpy.Render from its render method.
Since the render object isn't intended to be subclassed, we will omit the implicit self parameter.
Creates a new Render object.
Draws another render object into this render object.
Returns a canvas object. A canvas object has methods corresponding to the pygame.draw functions, with the first parameter (the surface) omitted.
Returns a (width, height) tuple giving the size of this render.
Returns a render consisting of a rectangle cut out of this render.
These function manage the rendering process.
This takes d, which may be a displayable object or a string. If it's a string, it converts that string into a displayable using the usual rules.
Causes a displayable to be rendered, and a renpy.Render object to be returned.
Renders returned by this object may be cached, and should not be modified once they have been retrieved.
Causes an event to be generated before seconds seconds have elapsed. This ensures that the event method of a user-defined displayable will be called.
Causes the displayable d to be redrawn after when seconds have elapsed.