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

Philipp

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.

[...]

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:
[...]

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?

hippozhipos

Here is one solution if you don't mind using 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()

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I set the position of my mouse cursor on the center of an object in pygame?

How can I position my cursor in an input field based on a keypress?

How can I set the initial scroll position of my list?

How can I set the proper scroll position my reactcomponent?

How can I set any view to the middle of my touch position?

How can I get the position of a cursor in Kivy?

How can I get cursor position with Gtkmm?

Can I position the cursor at the start of a textarea (HTA and VBScript)?

How can I set my default shell to start up tmux

how can I stop multiline commands from messing up my cursor position when scrolling through history?

How can I calculate the new cursor position relative to my canvas after zooming in?

How Can I Rotate My Sprite Towards The Player Even If Player Moves Position Pygame

How to set cursor position in a textarea where I last inserted the text?

How to set cursor position in EditText?

How can I implement pygame.sprite.spritecollide to my pygame

How can I set the start position of nodes in cytoscape.js with cy.load?

I have a problem with pygame blit image. Image doesn't get my wanted position and direction. How can I solve?

how do i set the mouse position around a circle in pygame

How to set the position of an image in pygame?

How can I quickly delete a line in VIM starting at the cursor position?

How can I use raycasthit according to the mouse cursor position?

how can i add entity on cursor position using aFrame

How can I smooth rotation which is based on cursor position?

How can I position an image element so it follows above the cursor?

How can I make my turtle move to my cursor?

How can I write something that changes my score in flappy bird every time the bird and the pipe x position are equal using pygame?

How can I delete from cursor to start in terminal (delete, not cut)

how can I set the position of the element?

How can I set the position that terminal opens at?