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

  • SEARCH
  • Home
  • 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 7925755
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T18:26:21+00:00 2026-06-03T18:26:21+00:00

earlier my sprite wouldn’t move at all so i posted the code and i

  • 0

earlier my sprite wouldn’t move at all so i posted the code and i got it fixed for the most part but now my up/down arrows work fine but my right key doesn’t work. (Also, when you press two keys, and then let go of one, the walk animation doesn’t work, but I’m not desperate right now to get that fixed.) Also i would prefer not to use user made classes. thanks in advance.
Here is the code:

from pygame.locals import *
import pygame._view

pygame.init()
clock = pygame.time.Clock()

height = 500
width = 500
screen = pygame.display.set_mode((width, height), 0, 32)
pygame.display.set_caption('placeholder text')

photo = 'grassbackground.png'
background = pygame.image.load(photo).convert()

rectexist = False

photo1 = 1

user = pygame.sprite.Sprite()

change = False

up = False
down = False
left = False
right = False

speed = 5

while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
        if event.type == KEYDOWN:
            if event.key == K_UP:
                up = True
                change = True
            if event.key == K_DOWN:
                down = True
                change = True
            if event.key == K_LEFT:
                left = True
                change = True
            if event.type == K_RIGHT:
                right = True
                change = True

        if event.type == KEYUP:
            if event.key == K_UP:
                up = False
                change = False
            if event.key == K_DOWN:
                down = False
                change = False
            if event.key == K_LEFT:
                left = False
                change = False
            if event.key == K_RIGHT:
                right = False
                change = False

    if down and user.rect.bottom < height:
        user.rect.top += speed
    if up and user.rect.top > 0:
        user.rect.top -= speed
    if left and user.rect.left > 0:
        user.rect.left -= speed
    if right and user.rect.right < width:
        user.rect.right += speed

   if change == True:
        pygame.time.wait(110)
        photo1 += 1

    if change == False:
        photo1 = 1

    if photo1 == 1:
        user.image = pygame.image.load("1.png").convert()
        if rectexist == False:
            user.rect = user.image.get_rect()
        rectexist = True
        screen.blit(user.image, user.rect)

    if photo1 == 2:
        user.image = pygame.image.load("2.png").convert()
        screen.blit(user.image, user.rect)

    if photo1 == 3:
        user.image = pygame.image.load("3.png").convert()
        screen.blit(user.image, user.rect)

    if photo1 >= 4:
        photo1 = 1

    thesprites = pygame.sprite.RenderPlain((user))
    thesprites.update()

    screen.blit(background, (0, 0))
    thesprites.draw(screen)

    pygame.display.update()
    clock.tick(60)
  • 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-03T18:26:35+00:00Added an answer on June 3, 2026 at 6:26 pm

    In your code it says:

    if event.type == K_RIGHT:
    

    it should be:

    if event.key == K_RIGHT:
    

    To keep animating you’d need to change the code a little more, add a:

    key_pressed = []
    

    in the beginning. Then for each key-press block do:

    key_pressed.append(event.key)
    

    and key-release do:

    key_pressed = [k for k in key_pressed if k != event.key]
    

    instead of the change=True and change=False respectively. Then after the two segments of checking what was pressed and released have add these lines:

    if len(key_pressed) > 0:
        change = True
    else:
        change = False
    

    That should fix most of your issues…

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

Sidebar

Related Questions

earlier i posted a question about cin skipping input, and I got results to
Earlier, I had a button control on my web page. but now I changed
Earlier today I asked a question about configuring log4net from code and got an
Earlier I posted a problem about implementing wait and notify, but I wasn't very
I asked this question earlier. I am intrigued by std::set but I have another
[An earlier version of this post got absolutely no response, so, in case this
Earlier i had posted this: What should be the better way for localizing iOS
Earlier I made a question about creating seo friendly urls. And I got the
earlier today i posted a question on how to open an image from a
Earlier I have downloaded Linux kernel from the git repository. Now I am trying

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.