[英]pygame.mouse.get_pos() doesn't work (Python IDLE 3)
我正在嘗試讓python檢測鼠標的位置。 我的代碼:
import pygames
pygame.init()
size = (700, 500)
screen = pygame.display.set_mode(size)
WHITE = (255,255,255)
screen.fill(WHITE)
pygame.mouse.set_pos([100,100]) #I didn't move my mouse so [0,100] should be the output
x_y = pygame.mouse.get_pos()
#I then run this program. Here is the output:
(0, 0)
我不知道出了什么問題。 為什么坐標錯了?
我不知道它為什么顯示錯誤的坐標,但似乎只有在pygame窗口前面有另一個窗口(在本例中為IDLE窗口)時才會發生。 如果選擇/聚焦pygame窗口並設置鼠標pos, pygame.mouse.get_pos()
將返回正確的坐標。 嘗試在事件循環中設置鼠標pos(按s鍵):
import pygame
pygame.init()
size = (800, 500)
screen = pygame.display.set_mode(size)
pygame.mouse.set_pos([100,100])
WHITE = (255,255,255)
clock = pygame.time.Clock()
done = False
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_s:
pygame.mouse.set_pos([100,100])
print(pygame.mouse.get_pos())
screen.fill(WHITE)
pygame.display.flip()
clock.tick(30)
pygame.quit()
或者在命令行/終端中運行該程序。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.