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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T20:19:09+00:00 2026-06-01T20:19:09+00:00

I have the following class: class Enemy(pygame.sprite.Sprite): def __init__(self): pygame.init() pygame.sprite.Sprite.__init__(self) # Basic variables

  • 0

I have the following class:

class Enemy(pygame.sprite.Sprite):
def __init__(self):
    pygame.init()
    pygame.sprite.Sprite.__init__(self)
# Basic variables
    self.speed = [2,2]

# Sets up the image and Rect
    self.bitmap = pygame.image.load("badguy.png")
    self.bitmap.set_colorkey((0,0,0))
    self.shipRect = self.bitmap.get_rect()
    self.shipRect.topleft = [100,200]

def move(self, x, y):
    self.shipRect.center = (x,y)

def render(self):
    screen.blit(self.bitmap, (self.shipRect))

Now, I want to have it move whenever I hit an arrow key. However, there’s a big problem with my move function. It simply moves the center of the ship to the coordinates fed into the function. In case you aren’t seeing the problem, it’s that I’m trying to call it like this:

if event.key == K_RIGHT:
    enemy.x += 5
if event.key == K_LEFT:
    enemy.move(-5,0)
if event.key == K_UP:
    enemy.move(0,5)
if event.key == K_DOWN:
    enemy.move(0,-5)

I was expecting it to move my five in the direction of the key that I hit. However, it shoots to the top left of the screen because I’m making the center set at zero on one axis and +- 5 on the other axis.

What method can be used to make it move in the correct direction and still give me the ability to put the sprites at a starting position on the screen that I choose.

Any help is much appreciated.

  • 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-01T20:19:10+00:00Added an answer on June 1, 2026 at 8:19 pm

    change move to:

    def move(self, x, y):
        self.shipRect.center[0] += x
        self.shipRect.center[1] += y
    

    This way it just increments the position by the move amount you specify.

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

Sidebar

Related Questions

I have following class public class ButtonChange { private int _buttonState; public void SetButtonState(int
I have following class (as seen through reflector) public class W : IDisposable {
I have following simple class: @interface Article: NSObject { NSString *title; } @property (copy,
I have following code class User attr_accessor :name end u = User.new u.name =
I have following event class. I have a question related to the Property method
I have following test class @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = {/services-test-config.xml}) public class MySericeTest { @Autowired
I have the following class public class UIControl { public string FName{ get; set;
I have the following class which handles my user logged in / logged out
I have the following class, internal class PageInformation { public string Name { get;
I have the following class (from a simple Spring tutorial) public class CarValidator implements

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.