Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 204455
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T17:28:09+00:00 2026-05-11T17:28:09+00:00

After some tweaking of some code I got from someone to cause a characters

  • 0

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

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T17:28:09+00:00Added an answer on May 11, 2026 at 5:28 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

After some research and little afford from myself I got this: $query .= SELECT
After some stupid musings about Klingon languages, that came from this post I began
Originally, I was having some issues getting this code to function, but after a
I'm in the process of porting some code from Linux to Mac OS X.
After some code modifications, I suddenly noticed the project icon has a small red
After some find and replace refactoring I ended up with this gem: const class
After some time I wanted to update my git repo, and then something went
After some years of experience in the field, it seems to me that the
I'm after some good tips for fluent interfaces in C#. I'm just learning about
I remember first learning about vectors in the STL and after some time, I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.