[英]How do I set the position of my mouse cursor on the center of an object in pygame?
[英]How can I set the position of my cursor at the start in pygame?
我必須為一個學校項目編寫一個 pygame 程序,我遇到了一些關於 mouse.set_pos 的問題。
我需要在程序開始時設置鼠標 position。 在這里,我將它設置在我的小 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])
時,我的 cursor 總是卡在給定的 Z4707FE07FD4688DEAZA8A
如何以某種方式使用pygame.mouse.set_pos()
以便我只能在開始時設置鼠標 position?
如果您不介意使用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()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.