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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T08:44:37+00:00 2026-06-04T08:44:37+00:00

I have created this to move a sprite. I set a sprite images cordinates

  • 0

I have created this to move a sprite. I set a sprite images cordinates to (spritex, 300) in a diffrent file. When run the program and press the right or left arrow, there is no movement. The value of spritex changes, in a print function

I have checked that the program is reading the key press’s by adding a print function. Am I setting up the program wrong?


——Movement Function File

import pygame
import os, sys
from itertools import *
from oryxsprites import *
from oryxdisplay import *
spritex = 300
screen = pygame.display.set_mode((640, 640))

def movementsprite():   

    global spritex

    keys = pygame.key.get_pressed()

    for event in pygame.event.get():
         if event.type == pygame.QUIT:
             running = False

    if keys[pygame.K_RIGHT]:
              spritex += 10
              print spritex

    elif keys[pygame.K_LEFT]:              
              spritex -= 10
              print spritex

——Display Function file—————

import pygame
import itertools
from oryxsprites import *
from McharMovement import *
screen = pygame.display.set_mode((640, 640))
spritex = 300
def backgroundmain():
    movementsprite()

    backdrop = pygame.Rect(0, 0, 640, 640) 
    screen.fill((50,50,50))
    playingfeildwidth = (32, 608)
    playingfeildheight = (32, 608)

screen.blit(warrior1, (spritex, 320))

pygame.display.flip()

——–Main File——————

import pygame
import os, sys
from itertools import *
from oryxsprites import *
from oryxdisplay import *
from McharMovement import *
running = True

while running:

    backgroundmain()
    pygame.display.set_caption('OryxGame')
    pygame.display.set_icon(grasstile)


    for event in pygame.event.get():
         if event.type == pygame.QUIT:
             running = False
             pygame.quit()
  • 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-04T08:44:39+00:00Added an answer on June 4, 2026 at 8:44 am

    Change your code to:

    spritex = 100
    
    def movementsprite():
        global spritex  #I added this line
        keys = pygame.key.get_pressed()
        for event in pygame.event.get():
             if event.type == pygame.QUIT:
                 running = False
    
        if keys[pygame.K_RIGHT]:
                  spritex += 10
                  print spritex
    
        elif keys[pygame.K_LEFT]:              
                  spritex -= 10
    

    That should work fine

    If the sprite is still not moving, then the problem is now when you call your function to move it. How are you moving it? You need to call that movement function from inside def movementsprite():

    And if you want to call the movement function from another file, use

    from other_file import movement_func

    or even

    from other_file import *

    UPDATE:
    Just add a return statement to your movement function, return spritex

    Then in the Display function, when you call the movement function change the line to
    spritex=movementfunction()

    That will alter the value of spritex between the files. And you dont have to make spritex global at the top of your movement function.

    Hope I could help! 🙂

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

Sidebar

Related Questions

so I have created this popup window with a custom move and sizing, pressing
At the moment, I have a sprite that I have arbitrarily set to move
We have created a portlet. We then went through some pain to move images
I have created a game which paints images to a JPanel. This runs with
I have created this code, and when I run it, don't get any errors
I have created this report using BIRT and phpjavabridge <?php header(Content-type: application/pdf); header(Content-Disposition: inline;
I have created this Fiddle with this code: This is my problem I want
I have created this function to check abecedarian with while loop (A word is
I'm a newbie starting to learn Ruby. I have created this code, however it
I have created something like this and this . In effect I have a

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.