Normy Haddad
Normy Haddad

Reputation: 181

pygame.mouse.get_pos() not updating location in a while loop

I am trying to make a tool in python which returns your current cursor location, and the previous cursor location, but when I run this code, cur always remains the same as when the code was initialized. I tried holding down the cursor and moving it around, but nothing would make cur change.

import pygame
pygame.init()
gameDisplay = pygame.display.set_mode((400, 400))
white = (255, 255, 255)
gameDisplay.fill(white)
pygame.display.update()
event = True
cur = pygame.mouse.get_pos()
curList = []
while event:
    cur = pygame.mouse.get_pos()
    curList.append(cur)
    if len(curList) >= 2:
        curList.pop(0)
    print(curList)

Upvotes: 2

Views: 190

Answers (1)

Kingsley
Kingsley

Reputation: 14906

There's a couple of issues with the OP's code. For starters it's not handling the event queue, so (probably) this is why you're not getting the mouse updates. It also allows you to exit the program cleanly.

import pygame
pygame.init()
gameDisplay = pygame.display.set_mode((400, 400))
white = (255, 255, 255)

curList = [ (0,0) ]
done = False

while not done:
    # paint the screen
    gameDisplay.fill(white)
    pygame.display.update()

    # handle user interaction, at least exiting the window
    for event in pygame.event.get():
        if ( event.type == pygame.QUIT ):
            done = True

    cur = pygame.mouse.get_pos()
    # Append a new mouse position, iff it's moved
    if ( cur != curList[-1] ):
        curList.append(cur)
        if len(curList) >= 2:
            curList.pop(0)
        print(curList)

I modified the point-tracking to only update the list if the incoming point is not the same as the last one on the list already. But given the list is only a single item anyway, it's all a bit moot.

Upvotes: 1

Related Questions