Question

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: alt text http://animania1.ca/ShowFriends/dev/example.jpg

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top