[英]pygame.mouse.get_pos() doesn't work (Python IDLE 3)
I'm trying to make python detect the position of the mouse. 我正在尝试让python检测鼠标的位置。 My code:
我的代码:
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)
I don't know what's wrong. 我不知道出了什么问题。 Why are the coordinates wrong?
为什么坐标错了?
I don't know why it shows the wrong coordinates, but it seems to happen only if there's another window (in this case the IDLE window) in front of the pygame window. 我不知道它为什么显示错误的坐标,但似乎只有在pygame窗口前面有另一个窗口(在本例中为IDLE窗口)时才会发生。 If the pygame window is selected/focussed and you set the mouse pos,
pygame.mouse.get_pos()
will return the correct coordinates. 如果选择/聚焦pygame窗口并设置鼠标pos,
pygame.mouse.get_pos()
将返回正确的坐标。 Try to set the mouse pos in an event loop (press the s key): 尝试在事件循环中设置鼠标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()
Or run the program in the command-line/terminal. 或者在命令行/终端中运行该程序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.