如何将光标位置设置在 pygame 的开头?

How can I set the position of my cursor at the start in pygame?

我必须为一个学校项目编写一个 pygame 程序并且我 运行 涉及一些关于 mouse.set_pos 的问题。

我需要在程序开始时设置鼠标位置。这里我把它设置到我的小window的中间,但是每次我运行它的时候,鼠标都没有设置到我的window的中间...可能是因为它在外面while 循环。

[...]

pygame.display.init()

running = True
positions = []
file_contents = {}
click_counter = 1

screen_y = 640
screen_x = 400
pygame.display.set_mode((screen_y, screen_x))
pygame.mouse.set_pos([screen_y / 2, screen_x / 2])

while running:
[...]

另一方面,当我尝试在我的 while 循环中使用 pygame.mouse.set_pos([screen_y / 2, screen_x / 2]) 时,我的光标总是停留在给定位置。

如何使用 pygame.mouse.set_pos() 让我只能在开始时设置鼠标位置?

如果您不介意使用 pyautogui,这里有一个解决方案。

import pygame
import os
import pyautogui

winPos = 100
os.environ["SDL_VIDEO_WINDOW_POS"]= "{},{}".format(winPos, winPos)

pygame.display.init()

running = True
screen_y = 640
screen_x = 400
pygame.display.set_mode((screen_y, screen_x))
pyautogui.moveTo(winPos + screen_y / 2, winPos + screen_x / 2)

while running:
    pygame.event.get()