Sprite animation class, help to improve.

98 views
Skip to first unread message

Arthur Alves

unread,
Aug 27, 2015, 10:09:36 AM8/27/15
to Kivy users support
Hello again,

Here my sprite animation class that uses atlas, just for learn kivy. I'm not good with games, but work well at now. But is slow yet, and I don't know the best way to make that.

What Do I need to improve for best perfomance. What's wrong? Draw with canvas is a good ideia or not?

See bellow a docstring  with usage example.


class Sprite(Widget):


    speed
= NumericProperty(0)
    gravity
= NumericProperty(1.5)
    jump_force
= NumericProperty(20)
    num_sprites
= NumericProperty(0)
    jumps
= NumericProperty(2)


   
def __init__(self, sprites_info, atlas, **kwargs):
       
super(Sprite, self).__init__(**kwargs)
       
# Sprite_sheet need to be reference of atlas keys and ids
       
self.sprite_sheet = sprites_info
       
# atlas to connect with sprite_sheet info
       
self.atlas = atlas
       
# to control animation speed
       
self.sprite_fps = 2
       
# to pause animation
       
self.pause_frame = False
       
# control flip but...
       
self.flip = False


   
def play(self, key, fps):
       
"""Play the animation selected by the key in dict passed in
        sprites args
        """

       
# Here use self.sprite_fps to control velocity of animation
       
if fps % self.sprite_fps == 0:
            animation
= self.sprite_sheet[key]
            sprite_len
= len(animation) - 1
           
if self.num_sprites > sprite_len:
               
self.flip = False
               
self.num_sprites = 0


           
if self.flip:
               
# flip all textures
               
for i in animation:
                   
self.atlas[str(i)].flip_horizontal()
               
# after that set flip to False to enable flip again
               
self.flip = False
           
self.canvas.clear()
           
with self.canvas:
               
Rectangle(
                    texture
=self.atlas[str(animation[self.num_sprites])],
                    pos
=self.pos, size=self.size
               
)


           
if not self.pause_frame:
               
self.num_sprites += 1


   
def flip_h(self):
       
self.flip = True
       
self.num_sprites = 0


   
def gravity_on(self, **kwargs):
       
self.speed += self.gravity
       
self.y -= self.speed
       
if self.y < 42:
           
self.jumps = 0
           
self.y = 42


"""
Usage:

         my_atlas = Atlas("imgs/my.atlas")
    my_run_animation = {"run": ["run_1", "run_2", "run_3", "run_4"]} #with atlas keys
    sprite_instance = Sprite(my_atlas, my_run_animation)

#In your Scene class:

    def update(dt):
        fps += 1
        sprite.play("run", fps)
        sprite.gravity_on()
        if fps > 30:
           fps = 0


# in your App class:
...
Clock.schedule_interval(scene.update, 1 / 30.0)

"""




Arthur Alves

unread,
Aug 31, 2015, 1:09:03 PM8/31/15
to Kivy users support
Nobody?

ZenCODE

unread,
Sep 1, 2015, 3:52:56 PM9/1/15
to Kivy users support
Okay, I think there are a few reasons for that.

1. Most of the high level kivy users and devs are professional developers with little spare time. It's one thing to help someone who is stuck by spotting an error. It's another to comb through someone's working code trying to guess optimizations when you don't know the context and can't run the code without constructing an app to put it in. And this class even requires constructing resources with it.

That is a lot of effort to help someone you don't know and the code works: they are hunting for possibly minimal optimizations. Also consider there are great tools for detecting inefficient code and bottlenecks. Have you tried profiling it?

http://kivy.org/docs/api-kivy.app.html#profiling-with-on-start-and-on-stop

2. Kivy widgets and really built for interaction, and become quite inefficient when using many of them as simple sprites. For that, you should look at Kivent, a framework for building performant, dynamic real-time scenes in Kivy.

https://github.com/kivy/kivent

So, hope that helps and explains the radio silence? We try to be a helpful bunch, but that confounded 24 hour limit thing is such a pain! :-)

Good luck

Arthur Alves

unread,
Sep 2, 2015, 11:51:16 AM9/2/15
to Kivy users support
Thank you, It will really help me.
Reply all
Reply to author
Forward
0 new messages