Code: Select all
init python:
# The dynamic_blink class is the core code for the animation itself
class dynamic_blink(renpy.display.layout.DynamicDisplayable):
def __init__(self, *args, **kwargs):
self.current_image_index = 0
self.blink_st = -1.0 # arbitrary number to force normal start
self.used_images = args
kwargs.update( {
'_predict_function' : self.predict_images } )
super(dynamic_blink, self).__init__( self.get_blink_image, *args, **kwargs )
def get_blink_image(self, st, at, *args, **kwargs):
# if time to change image
if st > self.blink_st:
self.current_image_index = (self.current_image_index + 1) % len(args)
# set the next change time based on current image
if self.current_image_index == 0: # back to open eyes, wait longer
self.blink_st = st + 2.0 + ( renpy.python.rng.random() * 5.0 )
else: # other frames, shorter delay
self.blink_st = st + 0.1
return args[self.current_image_index], 0
def predict_images(self):
return self.used_images
# The blinking_animation function will take the character's name as a parameter and use it to automatically fill in the file names
# This assumes your files are organizes and named as they are below. Alter the strings as needed to fit your organization methods
def blinking_animation(character):
return dynamic_blink(
# Define frames for the animation here. It should be able to take more or less frames
"chars/{}/{}_eyes_open.png".format(character, character),
"chars/{}/{}_eyes_half.png".format(character, character),
"chars/{}/{}_eyes_closed.png".format(character, character),
"chars/{}/{}_eyes_half.png".format(character, character)
)
image eileen_blinking = blinking_animation("eileen")
The last Python chunk the blinking_animation function, takes "character", as in probably the character's name, as a parameter and adds it into a template that matches my folder paths and file naming convention, so that I don't have to write out the entire file name repeatedly when defining the animations. As I mentioned in the code comments, you should be able to add or remove frames without issue, you don't have to stick to the same number of frames I use.
As a bonus, I use the following alternate function in the same init python block for blink animations with filenames that are exceptions to my usual format.
Code: Select all
def blinking_anim_special(character,*frames):
return dynamic_blink(
"chars/{}/{}.png".format(character, frames[0]),
"chars/{}/{}.png".format(character, frames[1]),
"chars/{}/{}.png".format(character, frames[2]),
"chars/{}/{}.png".format(character, frames[1]),
)
image eileen_red_blinking = blinking_anim_special("eileen","eileen_eyes_open_red","eileen_eyes_half_red","eileen_eyes_closed")