For example, if I’m in some game and I want to check whether I’m moving or not. So, for that, is there a way to detect the motion of the player using python? Please Suggest some ideas if possible. Thanks in advance.
Advertisement
Answer
You can do it using pynput
JavaScript
x
12
12
1
from pynput.mouse import Controller
2
3
mouse = Controller()
4
5
before = mouse.position
6
7
while True:
8
current = mouse.position
9
if before != current:
10
print('Movement detected')
11
before = current
12
The variable before
references to the movement that’s been before the current loop, and I basically check if the current position is different than it was the iteration before
You can also check for the keyboard presses
Answering to your comment, it’s also possible, this time using pyautogui
JavaScript
1
10
10
1
import pyautogui
2
3
before = pyautogui.screenshot()
4
5
while True:
6
current = pyautogui.screenshot()
7
if before != current:
8
print('Screen is different')
9
before = current
10