Condensing code for animated images

Discuss how to use the Ren'Py engine to create visual novels and story-based games. New releases are announced in this section.
Forum rules
This is the right place for Ren'Py help. Please ask one question per thread, use a descriptive subject like 'NotFound error in option.rpy' , and include all the relevant information - especially any relevant code and traceback messages. Use the code tag to format scripts.
Post Reply
Message
Author
User avatar
TellerFarsight
Veteran
Posts: 230
Joined: Sun Jun 04, 2017 8:09 pm
Projects: Vora, Secrets Untold
Location: Toronto, ON
Contact:

Condensing code for animated images

#1 Post by TellerFarsight » Sun May 27, 2018 10:00 am

So I have a lot of animations and have them organized so I can keep track of them all, but the code defining all the images is like 1000+ lines long. Is there a way to condense this?
I DON'T mean something like flipping "right" images to get "left" images. They are unique images, and that wouldn't get me fewer lines of code, only fewer .png files to worry about. I don't have any ideas for how exactly it could be condensed, but I just want to know if there is some way I don't know about.

Code: Select all

image joe right walk:
    "Joe_Sprites/joe_walk_right_1.png"
    0.1
    "Joe_Sprites/joe_walk_right_2.png"
    0.1
    "Joe_Sprites/joe_walk_right_3.png"
    0.1
    "Joe_Sprites/joe_walk_right_4.png"
    0.1
    "Joe_Sprites/joe_walk_right_5.png"
    0.1
    "Joe_Sprites/joe_walk_right_6.png"
    0.1
    "Joe_Sprites/joe_walk_right_7.png"
    0.1
    "Joe_Sprites/joe_walk_right_8.png"
    0.1
image joe left walk:
    "Joe_Sprites/joe_walk_left_1.png"
    0.1
    "Joe_Sprites/joe_walk_left_2.png"
    0.1
    "Joe_Sprites/joe_walk_left_3.png"
    0.1
    "Joe_Sprites/joe_walk_left_4.png"
    0.1
    "Joe_Sprites/joe_walk_left_5.png"
    0.1
    "Joe_Sprites/joe_walk_left_6.png"
    0.1
    "Joe_Sprites/joe_walk_left_7.png"
    0.1
    "Joe_Sprites/joe_walk_left_8.png"
    0.1
Current Project: Vora
Also Check Out: Devil Survivor [Reverse-Engineered]

User avatar
Remix
Eileen-Class Veteran
Posts: 1375
Joined: Tue May 30, 2017 6:10 am
Completed: None... yet (as I'm still looking for an artist)
Projects: An un-named anime based trainer game
Contact:

Re: Condensing code for animated images

#2 Post by Remix » Sun May 27, 2018 11:51 am

It really depends on the differences of the image animations...

If they are all just 0.1 second delay, cycle through all with same name and use no repeat then a DynamicDisplayable could be calculated and registered automatically.

For example, this code would automagically scamper through the folder and register the images as the same as yours (except the tag would be "joe walk right" rather than "joe right walk" as it only tests images):

Code: Select all


init python:
    def basic_anim(st, at, *args, **kwargs):
        f = kwargs.get('frames', [])
        d = kwargs.get('delay', 0.1)
        if st < d * float(len(f)):
            return f[int( st / d )], d
        return f[-1], 0

    def auto_anim(folder='images/', delay=0.1):
        sprites = {}
        for f in renpy.list_files():
            if f.startswith(folder) and f.endswith('.png'):
                try:
                    name_part = f[len(folder):].rsplit('_',1)
                    if name_part[0] not in sprites:
                        sprites[name_part[0]] = []
                    sprites[name_part[0]].append(name_part[1])
                except:
                    pass
        for name in [s for s in sprites if s.find('_')]:
            imgs = ["{}{}_{}".format(folder, name, k) for k in sprites[name]]
            if not len(imgs):
                continue
            renpy.image(
                name.lower().replace('_',' '), 
                DynamicDisplayable( basic_anim, frames = imgs ))

    auto_anim(folder='images/Joe_Sprites/')
    # note the images/ bit ... 
Awkwardly, to automate the lot, we'd need to know all animation versions, naming protocols you use and any other oddities.

So saying, you could still use DynamicDisplayables and register manually:

Code: Select all

image joe right walk = DynamicDisplayable(
    basic_anim, 
    frames = ["Joe_Sprites/joe_walk_right_{:1d}.png".format(k) for k in range(9) ] )
Automating the lot (so any new ones added just needs (intelligently named) images added to the game could still be done, would just need more info
Mad Scientist Todo List:
  • Finish Improved Event Handler
  • Implement DragonBones animation as a Creator Defined Container
  • Develop Cartoon Speech Bubble dialogue
  • Finish Bitmask collision and rebound vector system
  • Develop time based building mechanic
  • Others
  • *Find a superb artist and actually write a game*

Post Reply

Who is online

Users browsing this forum: No registered users