2

I am trying to implement a game using mouse position to see if the user clicks a button. Somehow the mouse position does not update for a couple seconds, and changes to a new position for another couple seconds, and repeat. I moved and pressed the mouse at different location in the screen, but the mouse position did not change at all. (Working on python3.5.1 and pygame 1.9.2, using IDE PyCharm)Any idea? Here is my code:

done = False
while not done:
   for event in pygame.event.get():
       if event.type == pygame.QUIT:
           done = True
   mouse = pygame.mouse.get_pos()
   click = pygame.mouse.get_pressed()
   if click[0]==1:
        print(mouse)
   pygame.display.update()
lanlan2271
  • 41
  • 1
  • 4

6 Answers6

3

The call

mouse = pygame.mouse.get_pos()

doesn't update the position unless the event MouseMotion is executed.

If you are executing the program in a window on a MAC, the mouse must be pressed, held, and moved (if you were to press, hold, then move the mouse, pygame.mouse.get_pos() would return the current mouse position).

sttan
  • 31
  • 2
  • Really? Why doesn't say anything about this behaviour in the docs? https://www.pygame.org/docs/ref/mouse.html#pygame.mouse.get_pos I can confirm it works as you describe. Is this behaviour different on a Windows machine? – allcaps Dec 28 '17 at 00:25
  • I am using an python online with Replit how would I get it to update? – Jacob Boughton Oct 13 '21 at 17:50
0

There is two ways of handling input events in pygame :

  • State Checking
  • Event Handling

For better understanding of how it works : http://www.pygame.org/docs/tut/newbieguide.html#managing-the-event-subsystem

You use both in your code, state checking :

mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()

Event handling :

for event in pygame.event.get():
       if event.type == pygame.QUIT:
           done = True

If you still want to use state checking to get the mouse position you can add:

clock=pygame.time.Clock()
clock.tick(60) # 60 frames per second

So the update of the position should be better.

Whysmerhill
  • 231
  • 1
  • 2
  • 7
  • Thanks! I added the clock, but the mouse position still changed extremely slowly. What else could be wrong? – lanlan2271 May 03 '16 at 15:58
0

If the button has a rect then you can use the rect.collidepoint() method with event checking like this:

mouse_pos = pygame.mouse.get_pos()

    if event.type == pygame.MOUSEBUTTONDOWN and pygame.mouse.get_pressed()[0] and button.rect.collidepoint(mouse_pos):
marienbad
  • 1,461
  • 1
  • 9
  • 19
0

This drove me crazy for hours. I had a similar problem. Using pygame in:

Mac OSX 10.13 (High Sierra) pygame 1.9.3 python 3.6 in a virtualenv

In this setup (specifically in the virtualenv), window focus is not properly tracked. Click and dragging will generate a MOUSEMOTION event, but simply moving the mouse around will not. If the MOUSEMOTION event is not generated, calling:

pos = pygame.mouse.get_pos()

will continue report the same value until another MOUSEMOTION event occurs.

Installing pygame outside of the virtualenv, all works as expected. Not really the answer I was hoping for, but at least it explains the behavior I was seeing.

Cole Howard
  • 343
  • 1
  • 8
-1

Your

mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()

only get the states at the time of the call. As per the documentation http://www.pygame.org/docs/ref/mouse.html:

to get all of the mouse events it is better to use either pygame.event.wait() or pygame.event.get() and check all of those events

That is you are missing the clicks because your program isn't storing them to process. You only see it when you get lucky and the program calls the function when the button is actually down.

xthestreams
  • 169
  • 5
  • 1
    I changed my code: for event in pygame.event.get(): if event.type == pygame.MOUSEBUTTONDOWN: pos = pygame.mouse.get_pos().. I am still having the same problem. – lanlan2271 May 03 '16 at 16:59
-1

This shows a basic pygame mouse getting program. Simply click anywhere in the window and the mouse coordinate are printed:

import pygame as py

py.init()

white = (255,255,255)

window = (400,400)
screen = py.display.set_mode(window)

clock = py.time.Clock()

done = False
while not done:
    for event in py.event.get():
        if event.type == py.QUIT:
            done = True
        elif event.type == py.MOUSEBUTTONDOWN:
            print py.mouse.get_pos()
    screen.fill(white)
    py.display.flip()
    clock.tick(30)

py.quit()

hope this helps :)

DCA-
  • 1,262
  • 2
  • 18
  • 33
  • 1
    Do you have any idea why this exact code would not work properly with PyGame 1.9.2a0 on Python 3.5.2 via Anaconda on OS X El Capitan? It 'works', but only updates the mouse position if I repeatedly click AND move the mouse around. Otherwise it keeps reporting the same position no matter where I click. See here for a gif of the behavior: [link](https://dl.dropboxusercontent.com/u/1049625/OddBehaviorPygameMouse.gif) – TSeymour Jul 25 '16 at 19:27