Sunday, April 3, 2011

Pygame: Sprite animation Theory - Need Feedback

After some tweaking of some code I got from someone to cause a characters images to move in regards to its direction and up down left right input I've put this together: (hope the code isn't too messy)

Character Move Code + IMG

The Sprite sheet only runs lengthwise, so basically each sprite section is a different action. Now would there be a way I could make a code that functions with the current one to cycle down from a set 'action' in order to make an animation?

For example: 'Run Left' is sprite 3. So then after we designate that column would it be possible to loop down how ever many frames of the run animation (lets say 4) in order to make an animation?

Example Picture: http://animania1.ca/ShowFriends/dev/example.jpg

From stackoverflow
  • It should be easy.

    If you record the frame number in a variable, you can modulo this with the number of frames you have to get an animation frame number to display.

    frame_count = 0
    animation_frames = 4
    while quit == False:
        # ...
        # snip
        # ...
        area = pygame.Rect(
            image_number * 100,
            (frame_count % animation_frames) * 150,
            100,
            150
        )
        display.blit(sprite, sprite_pos, area)
        pygame.display.flip()
        frame_count += 1
    

    If different actions have different numbers of frames, you'll have to update animation_frames when you update image_number.

    Also, this assumes that it's ok to play the animation starting at any frame. If this is not the case, you'll need to record what the frame count was when the action started, and take this away from frame count before the modulo:

        area = pygame.Rect(
            image_number * 100,
            ((frame_count - action_start_frame) % animation_frames) * 150,
            100,
            150
        )
    

    A note about your event handling. If you hold down, say, left, and tap right but keep holding down left, the sprite stops moving because the last event you processed was a keyup event, despite the fact that I'm still holding left.

    If this is not what you want, you can get around it by either keeping a record of the up/down states of the keys you are interested in, or by using the pygame.key.get_pressed interface.

    On another note, you appear to be aiming for a fixed frame rate, and at the same time determining how far to move your sprite based on the time taken in the last frame. In my opinion, this probably isn't ideal.

    2D action games generally need to work in a predictable manner. If some CPU heavy process starts in the background on your computer and causes your game to no longer be able to churn out 60 frames a second, it's probably preferable for it to slow down, rather then have your objects start skipping huge distances between frames. Imagine if this happened in a 2D action game like Metal Slug where you're having to jump around avoiding bullets?

    This also makes any physics calculations much simpler. You'll have to make a judgement call based on what type of game it is.

    RamonLion : thanks very much for your input. I agree with everything you've said about the performance and whatnot. I'm learning all these thing from references of free codes and advice given to me. Is there anything you could direct me to, to learn the things that would function much better for what I need?
    SpoonMeiser : Unfortunately, I've not found any really good tutorials. I'd suggest read any that seem appropriate, but keep in mind that just because people have published their work on the Internet, they're not necessarily experts. The main thing is just to get something working, you can clean it up later.
    RamonLion : so very true. so what would I have to change to take out the old and add in the pygame.key.get_pressed. also how do I set the framerate to be variable?
    SpoonMeiser : Do you want variable framerate or fixed framerate? I'd suggest fixed is probably better in most 2D games. Instead of finding out how long the last frame took to render, ignore the return value of Clock.ticks and assume each frame takes the same time.
    RamonLion : my plans for my project is to make a megaman X style side scroller/platformer. a fixed framerate is probably fine seeing as there won't be millions of things on the screen. and how would I go along ignoring the ticks? but setting the framerate?
    SpoonMeiser : You're already setting a framerate of 60 frames a second by passing the value 60 to Clock.ticks.
    RamonLion : oh so I don't need to change anything.
    SpoonMeiser : When you update the position of the sprite, don't do it depending on the time the last frame took, assume it took 1/60 of a second.
    RamonLion : so would that mean I'd take off: time_passed = clock.tick(60) time_passed_seconds = time_passed / 1000.0 sprite_pos += key_direction * sprite_speed * time_passed_seconds the time passed seconds?

0 comments:

Post a Comment