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.
renpy.
Displayable
linkBase class for creator-defined displayables.
__init__
(**properties) linkA subclass may override the constructor, perhaps adding new parameters. If it does, it should pass all unknown keyword arguments to the renpy.Displayable constructor, with the call:
super(MyDisplayable, self).__init__(**properties)
render
(self, width, height, st, at) linkSubclasses 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.
event
(self, ev, x, y, st) linkThe 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. If the event method returns None, the event is passed on to other displayables.
To ignore the event without returning None, raise renpy.IgnoreEvent
.
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.
per_interact
(self) linkThis 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.
visit
(self) linkIf 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.
renpy.
Render
(width, height) linkCreates a new Render object.
blit
(source, pos, main=True) linkDraws another render object into this render object.
place
(d, x=0, y=0, width=None, height=None, st=None, at=None, render=None, main=True) linkRenders d and places it into the rectangle defined by the x, y, width, and height, using Ren'Py's standard placement algorithm.
canvas
() linkReturns a canvas object. A canvas object has methods corresponding to the pygame.draw functions, with the first parameter (the surface) omitted.
Canvas objects also have a get_surface() method that returns the pygame Surface underlying the canvas.
get_size
() linkReturns a (width, height) tuple giving the size of this render.
subsurface
(rect) linkReturns a render consisting of a rectangle cut out of this render.
zoom
(xzoom, yzoom) linkSets the zoom level of the children of this displayable in the horitzontal and vertical axes. Only the children of the displayable are zoomed – the width, height, and blit coordinates are not zoomed.
The following attributes and methods are only used when model-based rendering is enabled:
mesh
linkThis field enables model-based rendering for this Render. If true:
If set to True:
The model will then be drawn in a single operation.
add_shader
(shader) linkThis causes the shader part shader to be used when this Render or its children are drawn. The part should be a string, or can be a string beginning with "-" to prevent a shader from being drawn.
add_uniform
(name, value) linkCauses the uniform name to have value when this Render or its children are drawn.
add_property
(name, value) linkCauses the GL property name to have value when this Render or one of its children are drawn.
renpy.
displayable
(d, scope=None) linkThis 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.
renpy.
end_interaction
(value) linkIf value is not None, immediately ends the current interaction, causing the interaction to return value. If value is None, does nothing.
This can be called from inside the render and event methods of a creator-defined displayable.
renpy.
load_image
(im) linkLoads the image manipulator im using the image cache, and returns a render.
renpy.
load_surface
(im) linkLoads the image manipulator im using the image cache, and returns a pygame Surface.
renpy.
map_event
(ev, keysym) linkReturns true if the pygame event ev matches keysym
One of:
config.keymap
.renpy.
render
(d, width, height, st, at) linkCauses 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.
renpy.
timeout
(seconds) linkCauses an event to be generated before seconds seconds have elapsed. This ensures that the event method of a user-defined displayable will be called.
renpy.
redraw
(d, when) linkCauses the displayable d to be redrawn after when seconds have elapsed.
renpy.
IgnoreEvent
linkThis is an exception that, if raised, causes Ren'Py to ignore the event. To raise this inside the event method, write:
raise renpy.IgnoreEvent()