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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T22:11:58+00:00 2026-06-06T22:11:58+00:00

I am trying to move an object by checking if the mouse is colliding

  • 0

I am trying to move an object by checking if the mouse is colliding with the objects rect and checking if a mouse button is down.

here is my code:

class Unit(pygame.sprite.Sprite):
    def __init__(self, display,):
    pygame.sprite.Sprite.__init__(self,)

    self.master_image = pygame.Surface((50, 100))
    self.master_image.fill((000,255,000))
    self.image = self.master_image
    self.rect = self.image.get_rect()
    self.rect.centerx = 500
    self.rect.centery = 500

def move(self):
    mouse = pygame.Surface((5, 5))
    mouse_rect = mouse.get_rect()
    (mouseX, mouseY) = pygame.mouse.get_pos()
    mouse_rect.centerx = mouseX
    mouse_rect.centery = mouseY

    for event in pygame.event.get():
        if event.type == pygame.MOUSEBUTTONDOWN:
            if mouse_rect.colliderect(self.rect):
                self.rect.centerx = mouseX
                self.rect.centery = mouseY
                print "move"

def update(self,):
    self.move()

This works but I have to spam every button on my mouse and eventually pygame will pick up the mouse event and the object will move as intended but only for a split second then it will stop.

My goal is just to click a button on the mouse and if the mouse is colliding with the box then the box will move whilst the mouse button is down to the mouse x and y.

I hope I am being clear.

Thanks for any help

peace!

Here is how I got it to work:

#unit sprite class
class Unit(pygame.sprite.Sprite):
   def __init__(self, display,):
      pygame.sprite.Sprite.__init__(self,)

      self.master_image = pygame.Surface((50, 100))
      self.master_image.fill((000,255,000))
      self.image = self.master_image
      self.rect = self.image.get_rect()
      self.rect.centerx = 500
      self.rect.centery = 500

      #mouse stuff
      self.mouse = pygame.Surface((5, 5))
      self.mouse_rect = self.mouse.get_rect()
      (self.mouse_rect.centerx , self.mouse_rect.centery) = pygame.mouse.get_pos()


  def move(self):
      if pygame.MOUSEBUTTONDOWN:#check for mouse button down
          (button1, button2, button3,) = pygame.mouse.get_pressed()#get button pressed

          if button1 and self.mouse_rect.colliderect(self.rect):#check for collision between object and mouse
              (self.rect.centerx, self.rect.centery) = pygame.mouse.get_pos()#set object POS to mouse POS

  def update(self,):
      (self.mouse_rect.centerx , self.mouse_rect.centery) = pygame.mouse.get_pos()#update mouse RECT
      self.move()#check movement

thanks for the help!

peace!

  • 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-06T22:11:59+00:00Added an answer on June 6, 2026 at 10:11 pm

    You’ll get a MOUSEBUTTONDOWN event when it is first pressed, but not subsequently when it is held down. To determine when the user stops pressing it, you need to check for the MOUSEBUTTONUP event.

    Alternatively you can use pygame.mouse.get_pressed() to just enquire the current state of the mouse buttons. Mouse function documentation is here.

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

Sidebar

Related Questions

I'm trying to move an object on the click of a mouse while the
I'm trying to move some code into some class files to clean up my
Trying to move some divs using hover, this is my markup: <div class=item dark-grey>
I'm working with Graphics2D(java) I'm trying move a random shape with mouse drag. This
I'm trying to enable a user to pan up/down and left/right an object in
I am trying to move an object from one database into another. The mappings
I'm trying to move a marker on a GoogleMap to simulate real time object
I am trying to rotate a canvas with canvas.rotate and move an object on
I'm trying to get the position of a canvas from a mouse move handler
I am trying to move the following function listView_SelectionChanged away from code-behind and handle

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.