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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T23:54:01+00:00 2026-06-15T23:54:01+00:00

Alright I have an issue with sprite movement on screen. I have a certain

  • 0

Alright I have an issue with sprite movement on screen. I have a certain way I want things to happen as follows:

  • When I press the left movement key, and while holding it, press the right movement key, I want the sprite to go RIGHT until i take my finger off the right key, then it will continue left until i also remove my finger from the left movement key.
  • I also want this method for the up/down keys
  • To add to this, when i press one of the up or down keys, and i then press, one of the left/right keys, i want to see the sprite move left or right until one of them are unpressed as well.

To summarize, I want the most recent directional key press to take movement priority, and when it is unpressed I want the next most recent key pressed to take movement priority. I also so not want diagonal movement, but that’s not an issue so far.

here is my code so far

m=pygame.key.get_pressed()
if m[K_d]:
    movey=0
    movex=speed
elif m[K_a]:
    movey=0
    movex=-speed
elif m[K_w]:
    movex=0
    movey=-speed            
elif m[K_s]:
    movex=0
    movey=speed 

This does 50% of what I want it to. The problem is, the code itself take priority in what is happening, so the result is when I just press the ‘A’ key, I move left, and then at the same time press the ‘D’ key, i move right. Then if i just press the ‘D’ key, I move right, but then when I also press the ‘A’ key I keep moving right when I want to be moving left at that point. I’ve tried if/not/and nested different ways but have failed so far.

How do i fix this?

Edit: Here is what I came up with with the help of Bartlomiej.

    if e.type==KEYDOWN:     
        if e.key==pygame.K_a:              
            keylist.append(1)
            #keylist.reverse()
            print keylist[-1:]
        if e.key==pygame.K_d:
            keylist.append(2)
            #keylist.reverse()
            print keylist[-1:]

    if e.type==pygame.KEYUP:
        if e.key==pygame.K_a:                
            keylist.remove(1)
            print keylist                
        if e.key==pygame.K_d:
            keylist.remove(2)
            print keylist

    if keylist[-1:]==[1]:
        walk=[-spd, 0]
        print 'WTF MOVE LEFT'
    elif keylist[-1:]==[2]:
        walk=[spd, 0]
        print 'WTF MOVE RIGHT'
    else:
        print 'WTFFFFFF'
        walk=[0, 0]

This works how I want and all i need to do now is minimize it and make it more elegant as a function but i got the idea down now. Thanks again Bartlomiej for helping me understand python and coding in general a bit better. Really appreciate it.

  • 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-15T23:54:02+00:00Added an answer on June 15, 2026 at 11:54 pm

    I always solved the problem like this: (a partial solution):

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
        if event.type == KEYDOWN:     
            if event.key == K_ESCAPE:
                pygame.quit()
                sys.exit()
            if event.key == pygame.K_LEFT:                
                x_speed=3    
            if event.key == pygame.K_RIGHT:
                player.moveright()
                x_speed=-3             
            if event.key == pygame.K_UP:                
                y_speed=3               
            if event.key == pygame.K_DOWN:                
                y_speed=-3
    
        if event.type == pygame.KEYUP:
            if event.key == pygame.K_LEFT and x_speed == 3:                
                x_speed=0                
            if event.key == pygame.K_RIGHT and x_speed == -3:
                x_speed=0                
            if event.key == pygame.K_UP and y_speed == 3:
                y_speed=0                
            if event.key == pygame.K_DOWN and y_speed == -3:
                y_speed=0
    

    This doesn’t stop movement when the previous key has been unpressed.

    EDIT: It is not impossible, its just not that useful. You could for example have 2 lists that would imitate a stack. When the list is empty your character will not move. With each keydown, you append the key to the end of list. Every keyup, you remove the key from the list. That way, the most recent key pressed will be at the end of the list, and if you unpress the last recent will also be at the end of the list.

    A small example:

    keylist = []
    for event in pygame.event.get():
        if event.type == KEYDOWN:     
            if event.key == pygame.K_LEFT:                
                keylist.append(-1)    
            if event.key == pygame.K_RIGHT:
                keylist.append(1)            
    
        if event.type == pygame.KEYUP:
            if event.key == pygame.K_LEFT:                
                keylist.reverse().remove(-1)                
            if event.key == pygame.K_RIGHT:
                keylist.reverse().remove(1)
    

    the in the move method you try this:
    You get the last element in the list if it exists, move the character in the right direction.

    if(not keylist[-1:]):
        character.move(direction=keylist[-1])
    else:
        character.move(direction=0)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Alright so I have a loader screen which fetches files and what not, things
Alright, so I'm running into an issue with my code. What I have done
Alright, So I have an issue that is a bit weird. I am using
Alright, so I have the strangest issue here. I'm taking the mean of a
Check the bottom for revised edition Alright, here's the issue. I have a li
Alright ladies and gents. I have quite the puzzling issue. I have WAMP server
Alright have been working to switch to jQuery 1.7's new and improved .on() function
Alright i have a for loop in php and it generates a group of
Alright so I have no idea how to even begin doing this But basically
Alright so I have been looking around (on SO and Google) to see if

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.