AttributeError: 'Event' object has no attribute 'mouse_pos'

AttributeError: 'Event' object has no attribute 'mouse_pos'

我只是想保存每一个瞬间的鼠标位置。为什么这个简单的代码不起作用?

import pygame

screen = pygame.display.set_mode((500,500))
screen.fill((255,255,255))

while True:
    ev = pygame.event.get()
    for event in ev:
        pos = pygame.mouse.get_pos()
        if event.type != pygame.NOEVENT:
            mouse_pos = pygame.mouse.get_pos()      
            x, y = event.mouse_pos
            print(x, y)

可以在事件处理循环之外跟踪鼠标位置。

import pygame
import sys

screen = pygame.display.set_mode((500,500))
screen.fill((255,255,255))

last_pos = (-1,-1)

while True:
    # handle user events
    ev = pygame.event.get()
    for event in ev:
        if event.type == pygame.QUIT:
            sys.exit()

    # track the mouse co-ords
    mouse_pos = pygame.mouse.get_pos()
    if ( mouse_pos != last_pos ):
        x,y = mouse_pos
        print(x, y)
        last_pos = mouse_pos