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 8939731
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T10:52:57+00:00 2026-06-15T10:52:57+00:00

I found a solution to make a sprite move when you hold a key

  • 0

I found a solution to make a sprite move when you hold a key down. The problem is that it forces writing ugly duplicated code. The current solution I found is:

       for event in pygame.event.get():
            if event.type == KEYDOWN:
                keystate = pygame.key.get_pressed()
                while keystate[K_RIGHT]:
                    screen.fill((255,255,255))
                    pygame.event.get()

                    for sprite in sprites:
                        rimage = sprite[1].getimage()

                        if sprite[2] is None:
                            x+=3
                            sprite[1].update(time)
                            screen.blit(rimage, (x,y))
                            if sprite[1].isfinished() == True:
                                sprite[1].reset()
                            last_dir = "right"
                            if x >= screen_width - rimage.get_width():
                                x = screen_width - rimage.get_width()

                    #update player sprite movement
                    #update player sprite animation
                    #update rest of game map

                    keystate = pygame.key.get_pressed()
                    time = pygame.time.get_ticks()


                    pygame.display.update()

The problem is that the while keystate block. It has to be repeated for each direction and the game world needs to be updated in each while block. That is five places where the same code needs to be duplicated….4 directions plus if a key is not pressed. I could wrap it in a function but I was wondering if there was a better way to handle holding down a button in pygame.

  • 1 1 Answer
  • 0 Views
  • 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-06-15T10:52:58+00:00Added an answer on June 15, 2026 at 10:52 am

    The usual way program in pygame is use the events to update the direction, then write the update position code outside events, that way you don’t need replicated code.

    clock = pygame.time.Clock()
    
    direction = (0,0)
    
    while True:    # main loop
    
       for event in pygame.event.get():
    
            if event.type == KEYDOWN:
                if event.key == K_RIGHT:
                    direction = (3, 0)
                elif event.key == K_LEFT:
                    direction = (-3, 0)
                elif event.key == K_UP:
                    direction = (0, 3)
                elif event.key == K_DOWN:
                    direction = (0, -3)
                else:
                    print "Unrecognized key"
    
            if event.type == KEYUP:
                direction = (0, 0)
    
        screen.fill((255,255,255))
    
        for sprite in sprites:
            rimage = sprite[1].getimage()
    
            if sprite[2] is None:
    
                # Check if new position is inside the screen
                new_pos = x + direction[0], y + direction[1]
                if new_pos[0] + rimage.get_width() < screen_width:
                    x = new_pos[0]
                if new_pos[1] + rimage.get_height() < screen_height:
                    y = new_pos[1]
    
                # Draw the sprite
                sprite[1].update(time)
                screen.blit(rimage, (x,y))
                if sprite[1].isfinished() == True:
                    sprite[1].reset()
                    last_dir = direction
    
        #update player sprite movement
        #update player sprite animation
        #update rest of game map
    
        time = pygame.time.get_ticks()
    
        pygame.display.update()
    
        clock.tick(40)  # Keep game running at 40 fps
    

    If you want you can achieve the same with keystate, in such case you don’t need to process key events.

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

Sidebar

Related Questions

I haven't found any solution about my current problem yet: I can't make the
problem euler #5 i found the solution but i don't know why this first
My answer: After getting annoyed, I have found a solution. The problem was indeed
i searched for a solution to make an asynchronous page request and found these
I discovered this problem today and haven't found any solution yet, I'm kind of
I've been trying to solve this problem all day, and haven't found a solution
I'm trying to make an iPhone app, but i found some issue that auto-rotate
I've been looking for a solution to make my NSTextField bottom-aligned and I've found
Seems, it's common task, but I haven't found solution. I need to calculate number
Edited: SOLUTION FOUND. This is strange and not the best solution, but I just

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.