I have to write a pygame program for a school project and I run into some issues concerning mouse.set_pos.
I need to set the mouse position on the start of my program. Here I have set it to the middle of my little window, but everytime I run it, the mouse is not set to the middle of my window… probably because it is outside the while-loop.
JavaScript
x
17
17
1
[ ]
2
3
pygame.display.init()
4
5
running = True
6
positions = []
7
file_contents = {}
8
click_counter = 1
9
10
screen_y = 640
11
screen_x = 400
12
pygame.display.set_mode((screen_y, screen_x))
13
pygame.mouse.set_pos([screen_y / 2, screen_x / 2])
14
15
while running:
16
[ ]
17
On the other hand, when I´m trying to use the pygame.mouse.set_pos([screen_y / 2, screen_x / 2])
inside of my while loop, my cursor is always stuck at the given position.
How do I use pygame.mouse.set_pos()
in a way so I can only set the mouse position at the start?
Advertisement
Answer
Here is one solution if you don’t mind using pyautogui
.
JavaScript
1
18
18
1
import pygame
2
import os
3
import pyautogui
4
5
winPos = 100
6
os.environ["SDL_VIDEO_WINDOW_POS"]= "{},{}".format(winPos, winPos)
7
8
pygame.display.init()
9
10
running = True
11
screen_y = 640
12
screen_x = 400
13
pygame.display.set_mode((screen_y, screen_x))
14
pyautogui.moveTo(winPos + screen_y / 2, winPos + screen_x / 2)
15
16
while running:
17
pygame.event.get()
18