Mouse control bugs out on different PCs - python

mousepos = mouse.get_position()
if mousecontrol: # mouse control can be toggled
CAMERAANGLE[0] -= (mousepos[1] - pastcoords[1])*SENS # camera x angle
CAMERAANGLE[1] -= (mousepos[0] - pastcoords[0])*SENS # camera y angle
mouse.move(960,540) # resetting mouse to center of screen
pastcoords = mouse.get_position() # resetting mouse for next frame to calculate the distance the mouse travelled in a frame
^ this code is in a while loop and runs once every time the loop runs
This code works fine for computers at my school, however when I run this code on my home PC, the mouse control is extremely jittery and doesn't work.
My school computers run windows 10 with an Intel i5 10500T, and my home computer runs windows 10 with a 12400f and an RTX 3060
I added a timer to slow down the framerate, however this didn't fix the problem. Does anyone know what's going on here?

It might be that the issue is with the mouse control implementation. The jitteriness might be caused by a difference in the processing power or mouse sensitivity between the two computers, or due to a difference in the mouse driver or hardware. It is also possible that there is a difference in the refresh rate or resolution between the two displays, which could be affecting the mouse position calculation. To solve this issue, you can try adjusting the mouse sensitivity, or try using a different mouse to see if the issue is with the hardware. You could also try running the code on a different display or resolution to see if that makes a difference.

The mouse would move a distance between mousepos = mouse.get_position() and pastcoords = mouse.get_position(), resulting in an inaccuracy in the mouse position - I fixed it by changing the code to this:
mousepos = mouse.get_position()
if mousecontrol: # mouse control can be toggled
CAMERAANGLE[0] -= (mousepos[1] - 540)*SENS # camera x angle
CAMERAANGLE[1] -= (mousepos[0] - 960)*SENS # camera y angle
mouse.move(960,540) # resetting mouse to center of screen

Related

Mouse movement % based but mouse still goes flying where it's not supposed to?

I need some help badly. I got all my mouse movement simulation in % so it can work for other resolutions like this:
win32api.mouse_event(win32con.MOUSEEVENTF_MOVE, int((300/1920)*width), int((-50/1080)*height))
Which is meant to make it move 300 pixels to the right and 50 pixels up and it works perfectly on my computer, however, people are trying to use my program, the mouse moves way too far away for some reason and it isn't going to the proper spot.
What could be the issue and what do I need to do to fix.
EDIT: Found the issue. I don't have "enhance mouse precision" in windows while they do, it essentially gives mouse acceleration.
Found the issue. I don't have "enhance mouse precision" in windows while they do, it essentially gives mouse acceleration.

Is there a way to make mouse move universal regardless of sensitivity?

So I dont have code because its not an issue with coding as I already have the program done and working however I noticed that:
In gaming menus mouse sensitivity doesnt matter because I set coords to click/move and since its not sensitivity related it works just fine.
However,
When moving a game camera(character view) if I do for example 'move 1000 pixels to the right' this will be affected by mouse sensitivity & ingame sensitivity is there no way to make this work in all resolutions / mouse sensitivity?
Essentially I need to be able to make my friends 'camera"/'character view' to move as much as mine while having a different mouse DPI
Instead of moving in a relative way move to a fixed point of your display for example:
import pyautogui
import time
time.sleep(3)
pyautogui.moveTo(100, 100)
This will move your mouse to 100 pixels along 100 pixels down on your monitor. Other examples:
# To make it drag along the screen
# (x, y, time taken)
pyautogui.moveTo(100, 100, 5)
# To make it hold while moving
# The 5 slows it
pyautogui.dragTo(100, 100, 5)
# To check if the next move pos is on display
# This will return false
pyautogui.onScreen(0, -1)
# To Move only x/y
pyautogui.moveTo(None, 500)
Allthough pyautogui should only move directional to the pixels irrespective of the DPI so may you please attach your code.

Pygame : Getting rid of strange gradient effect on displayed image

Hello fellow programmers,
I wrote a little python program which is use to launch random games on a retrograming distribution, and I use pygame to display the image of the game before launching it
I use a background and my issue is that the background image is clean but when displaying it and the cover of the game over it, it appears with a strange ugly gradient effect as you can see there : https://imgur.com/a/BnNdoqn
It appears mostly in the corner and the cover itself is entirely unaffected.
Here is my pygame code displaying both images :
log('showPic %s' %file)
# INITS
pygame.init()
pygame.mouse.set_visible(0)
backgroundPicture = pygame.image.load(backgroundFile)
picture = pygame.image.load(file)
# # CREATE FULLSCREEN DISPLAY. X = 1920- Y = 1080
fullscreen = pygame.display.set_mode((1920,1080), FULLSCREEN)
fullscreen.blit(backgroundPicture, (0,0))
# # PASTE PICTURE ON FULLSCREEN
x = (1920 - picture.get_width()) /2
y = (1080 - picture.get_height()) /2
fullscreen.blit(picture, (x,y))
# # SHOW FULLSCREEN
pygame.display.flip()
# # WAIT 5 SECONDS (need import time)
time.sleep(5)
# # EXIT PYGAME (Not needed but recommanded)
pygame.display.quit()
pygame.quit()
backgroundPicture is the background image and picture is the cover of the game, I combined the too like it appears in second capture.
So mainly I don't know much at all about display, images, graphical libraries and all that.
I think that this might be related to transparency or alpha layer or compression format of the image but I have no knowledge at all about that either.
The code is launched on a raspberry pi with a linux distribution, don't know much more about it.
Also strangely, one of my users said the strange gradient effect seems to disappear after ten or so launches of the script, but I couldn't reproduce that.
So what am I missing to get rid of that ugly effect ?
Here is the background image here if its characteristic might be related to the problem :
Thank you for your help !
The effect you are seeing is called "banding", see Wikipedia article. It is caused by not having enough bit-depth to represent fine gradations of colour and is most noticeable in large, untextured areas.
There are not many things you can do about it. Your options are basically:
to go to a 16-bit setup instead of 8-bit, if pygame can do that, or
add a small amount of random noise, or dithering to break it up.

Pygame Tile Based Movement Speed

Thanks for taking the time to read this.
Right now I'm making a really basic tile based game. The map is a large amount of 16x16 tiles, and the character image is 16x16 as well. My character has its own class that is an extension of the sprite class, and the x and y position is saved in terms of the tile position.
To note I am fairly inexperienced with pygame.
My question is, I am planning to have character movement restricted to one tile at a time, and I'm not sure how to make it so that, even if the player hits the directional key dozens of time quickly, (WASD or arrow keys) it will only move from tile to tile at a certain speed. How could I implement this generally with pygame? (Similar to game movement of like Pokemon or NexusTk). One movement would result in a player being in a tile. They couldn't stop halfway between tiles for example.
Thanks for your time! Ryan
You store your characters location as a grid coordinate. So if he's at (2,0) he is rendered at (32,0). The game then animates him moving between tiles, but, he's either on one or the other. While in the move state, you render an (x,y) offset between 0 to tilewidth.
It sounds like you want one move per keypress, if time elapsed / animation has completed. So:
On keypress, toggle to: animating state
Set destination tile coordinate
draw offset, between 0 and tilewidth, depending on time elapsed. offset = (elapsed_ms / 1000.) * tile_w would scale between 0 to 16 if time is less<= 1 second.
Once time elapsed is >= animation length (I chose 1000. above), switch to stationary state.
If keypress happens while in animation state, ignore it.
Pygame example: using numpy for map array.

Mouse click command does not work on certain GUIs

The script below works perfectly when I want to click, for example, the Start button on Windows, but when I try to click a button in a certain GUI program it does not have any effect.
Is it possible that this program has disabled virtual mouse clicks?
If so, can I circumvent this somehow?
import win32api, win32con
Po=win32api.GetCursorPos()
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,Po[0],Po[1],0,0)
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,Po[0],Po[1],0,0)
mouse_event (and SendInput, which is the preferred API to use for input) have a couple of tricky bits, it's a good idea to
read the MSDN page for mouse_event fully and carefully before using it - pay attention to the small print: in particular, the x and y values are not pixels, so you can't just put in values you get from GetCursorPos.
It just happens that 0,0 is the bottom-left corner, so points near the bottom left will be roughly in the same area of the screen, but the further away from that you get, the more the pixel values diverge from the actual units that this API uses: so it can appear to work for positions near the start button (assuming it's in the bottom left of the screen), but for other values, it may appear to be clicking somewhere else, which sounds similar to what you are seeing.
From MSDN:
dx [in]
Type: DWORD
The mouse's absolute position along the x-axis or its amount of motion since the last mouse event was generated, depending on the setting of MOUSEEVENTF_ABSOLUTE. Absolute data is specified as the mouse's actual x-coordinate; relative data is specified as the number of mickeys moved. A mickey is the amount that a mouse has to move for it to report that it has moved.
So first of all, you need the MOUTEVENTF_ABSOLUTE flag. But that's not all:
Remarks
...
If MOUSEEVENTF_ABSOLUTE value is specified, dx and dy contain normalized absolute coordinates between 0 and 65,535. The event procedure maps these coordinates onto the display surface. Coordinate (0,0) maps onto the upper-left corner of the display surface, (65535,65535) maps onto the lower-right corner.
...so you'll need to scale your target coordinates appropriately before passing them to this API.
Mouse events generated by programs have an "injected" property that an app can filter if they want - for example MMO clients often filter those to avoid bots
Those zeros in the mouse_event let you set some properties, you might want to research if those will let you overcome the injected flag, although I can't find a way immediately
below code worked for me
pyautogui.mouseDown(fromx, fromy)
time.sleep(2)
pyautogui.mouseUp(tox, toy)
pyautogui.mouseUp(1000,400)

Categories

Resources