1

I need to be able to track the mouse and display it on a pygame window even though the mouse is confined in a location such as 200, 200. My code is this

import pygame
import time
from pygame.locals import *
import pyautogui

pygame.init()

DISPLAY=pygame.display.set_mode((int(2560/3),int(1440/3)),0,32)

WHITE=(255,255,255)
BLUE=(0,0,255)

DISPLAY.fill(WHITE)

w = pyautogui.position()
x_mouse = w.x
y_mouse = w.y
oldx = x_mouse
oldy = y_mouse
div = 3
x = x_mouse/div
y = y_mouse/div

while True:
    DISPLAY.fill(WHITE)
    pygame.draw.rect(DISPLAY,BLUE,(x,y,50,50))
    w = pyautogui.position()
    x_mouse = w.x
    y_mouse = w.y

    if x_mouse > oldx:
        x+=(x_mouse-oldx)/div
    if x_mouse < oldx:
        x-=(oldx-x_mouse)/div

    if y_mouse > oldy:
        y+=(y_mouse-oldy)/div
    if y_mouse < oldy:
        y-=(oldy-y_mouse)/div

    oldx = x_mouse
    oldy = y_mouse
    
    for event in pygame.event.get():
        if event.type==QUIT:
            pygame.quit()
            
    pygame.display.update()

This works fine but it completely breaks when the mouse is confined in a location, any way to get around this issue?

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
Anik Patel
  • 143
  • 4
  • Can you please explain what a mouse being "confined in a location" means? Is it some operating system feature? Or something like "not in the pygame window" ? – Kingsley Oct 22 '20 at 23:45
  • So basically it means that the mouse is invisible and is confined in a location like 200, 200 – Anik Patel Oct 22 '20 at 23:55
  • 2
    PyGame only receives mouse movement events when the mouse is inside the PyGame window. – Kingsley Oct 23 '20 at 00:01
  • No, im using pygame just to display the rectabgle – Anik Patel Oct 23 '20 at 00:30
  • If you want to ensure you get an integer result from division, use `//`. It's called [integer division](https://docs.python.org/3.1/tutorial/introduction.html#numbers) and returns the floor of the result. – import random Oct 23 '20 at 02:00
  • 1
    What do you try to achieve? Why do you use _pyautogui_ instead of the [`pygame.mouse`](https://www.pygame.org/docs/ref/mouse.html) module (e.g. [`pygame.mouse.get_pos`](https://www.pygame.org/docs/ref/mouse.html))? – Rabbid76 Oct 23 '20 at 05:29
  • So pygame is used only to display the mouse position, I need to get the mouse movement even if the mouse CURSOR is static in one place, so basically I need the external movement of the MOUSE not the cursor – Anik Patel Oct 23 '20 at 15:35

1 Answers1

0

I recommend to use the pygame.mouse module and pygame.mouse.get_pos() instead of pyautogui instead of:

import pygame
from pygame.locals import *
pygame.init()

DISPLAY = pygame.display.set_mode((int(2560/3),int(1440/3)),0,32)
clock = pygame.time.Clock()

WHITE = (255, 255, 255)
BLUE = (0, 0, 255)

x_mouse, y_mouse = pygame.mouse.get_pos()
div = 10
x, y = x_mouse/div, y_mouse/div

while True:
    clock.tick(60)
    for event in pygame.event.get():
        if event.type==QUIT:
            pygame.quit()
    
    x_mouse, y_mouse = pygame.mouse.get_pos()
    x += (x_mouse - x) / div
    y += (y_mouse - y) / div
    
    DISPLAY.fill(WHITE)
    rect = pygame.Rect(0, 0, 50, 50)
    rect.center = round(x), round(y)
    pygame.draw.rect(DISPLAY, BLUE, rect)        
    pygame.display.update()

See also How to make smooth movement in pygame

Rabbid76
  • 202,892
  • 27
  • 131
  • 174