我如何设置我的光标在pygame开始的位置?



我必须为学校的一个项目写一个pygame程序,我遇到了一些关于mouse.set_pos的问题。

我需要在程序开始时设置鼠标位置。这里我把它设置在我的小窗口的中间,但是每次我运行它,鼠标都没有设置在我的窗口的中间…可能是因为它在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()

最新更新