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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T09:31:33+00:00 2026-06-03T09:31:33+00:00

im trying to get a sprite to move around the screen with a walk

  • 0

im trying to get a sprite to move around the screen with a walk animation and all it will do is stay stationary but it still goes through the animation.
here’s 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()

photo1 = 1

user = pygame.sprite.Sprite()

change = False

x, y = (0, 0)

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

speed = 3

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
                y += 1
                change = True
            if event.key == K_DOWN:
                down = True
                y -= 1
                change = True
            if event.key == K_LEFT:
                left = True
                x -= 1
                change = True
            if event.type == K_RIGHT:
                right = True
                x += 1
                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()
    user.rect = user.image.get_rect()
    screen.blit(user.image, user.rect)

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

if photo1 == 3:
    user.image = pygame.image.load("3.png").convert()
    user.rect = user.image.get_rect()
    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)

(p.s. im not sure why i have the x and y coordinates in there, i guess i just put that there in case i decided to use it)
im not using any defined class and would prefer not to. thanks in advance

  • 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-03T09:31:35+00:00Added an answer on June 3, 2026 at 9:31 am

    I can’t tell for sure, because you haven’t included the image files necessary to run your app.

    I can’t comment on posts because I haven’t been active enough, but it looks like you’re recreating the rect here:

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

    even though you adjust it a few lines above:

    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
    

    You have to store the rect that is used for the player’s position, and then re-apply it to the rect after you create the new image.

    Beyond that, look into creating and using functions, the code here could benefit greatly from having them. Take a look here for more information on that: http://www.penzilla.net/tutorials/python/functions/

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

Sidebar

Related Questions

trying to get my head around Feedzirra here. I have it all setup and
I am trying to run a Walk style animation on my main game sprite.
I am trying get all html links within a string and replace them using
I am trying get all html links within a string and replace them using
Trying to get my mind around google protobuf. I found some implementation of protobuf
I'm basically trying to get all touch event data from something like a system
I'm trying to write a simple pygame program where some boxes move around on
Trying to get the following sprite to work http://reversl.net/sprites/ on hover. Here's my styling
I am trying to get the bitmapdata from a MovieClip and create a Sprite
I'm trying to make a basic (Mario style) game but my sprite(plumber) doesn't appear,

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.