Pygame: Sprite Animation Theory - Need Feedback

After some configuration of some code that I received from someone in order to call up images of the characters in the direction of his direction and up the left right input, I put this together: (I hope that the code is not too dirty)

Character Movement Code + IMG

A sprite sheet works only in length, so basically every sprite section is a different action. Now would there be a way to make code that works with the current one to cycle through the action set to make an animation?

For example: "Run to the left" is sprite 3. So, after we designate this column, it would be possible to loop how many frames of the run animation (say 4) to make the animation?

Example image: alt text http://animania1.ca/ShowFriends/dev/example.jpg http://animania1.ca/ShowFriends/dev/example.jpg p>

+3
source share
1 answer

It should be easy.

If you write the frame number in a variable, you can modulo this with the number of frames that you must get in order to display the frame number of the animation.

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 frames, you will have to update the animation frames when updating image_number.

, , , . , , , , :

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

. , , , , , , , keyup, , .

, , , / , pygame.key.get_pressed.

, , , , . , , , .

2D . - 60 , , , , . , 2D-, Metal Slug, , ?

. , .

+4
source

Source: https://habr.com/ru/post/1706230/


All Articles