简体   繁体   English

pygame.mouse.get_pos() 在 while 循环中不更新位置

[英]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.我正在尝试在 python 中创建一个工具,它返回您当前的光标位置和以前的光标位置,但是当我运行此代码时,cur 始终与代码初始化时相同。 I tried holding down the cursor and moving it around, but nothing would make cur change.我试着按住光标并移动它,但没有任何东西可以改变cur

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)

There's a couple of issues with the OP's code. OP 的代码有几个问题。 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.但鉴于该列表无论如何都只是一个项目,这一切都没有实际意义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM