Check if mouse is outside pygame window - python

I was running a game in pygame, and I discovered a flaw. The way I coded it, if the mouse went outside of the window, the game started to go around in circles. When you get pygame.mouse.get_pos(), it will return the last value the mouse was detected in the window, but other wise no indication that the mouse has left the window. So, is there any way to check if the mouse has stopped hovering over the pygame window, and has left it?

A simple answer to this would be to use the pygame.mouse.get_focused() function. This returns 0 when the mouse isn't focused on the screen. So if you wanted to check if the mouse was outside the window, you could simply do
mouseFocus = pygame.mouse.get_focused()
During the main loop and have an if statement checking if the mouse has left the screen.
if mouseFocus == 0:
print("mouse is out of screen.")
Hope this helps

Related

scrollhandrag keep on pressing after click

I added scrollhandrag for my pan tool function with self.setDragMode(QtWidgets.QGraphicsView.ScrollHandDrag). It is working just fine but when I added some in my mouseReleaseEvent to add Ellipse after releasing the mouse, the scrollhandrag function stop working. When I clicked the mouse, it will drag the screen but when I unclicked the mouse, the hand won't open/uncliked and it keeps on dragging the screen. How can I make the scrollhandrag to unclick when I am no longer pressing my mouse without deleting my mouseReleaseEvent?

How to get real mouse movement, not based on cursor position (python)

Im using windows and python
So I want to make a macro in a game, but the game always centers your mouse at the center of the screen. Every mouse movement algorithm only detects movements based on the difference between the previous cursor and the current cursor. Is there a way to detect mouse movement instead of cursor position?
Mouse movement is an event. So this event needs to be captured. Windows sends a WM_MOUSEMOVE message when the mouse moves. Various Python extensions can work with this event. E.g. tkinter does this as follows:
import tkinter as tk
root = tk.Tk()
def motion(event):
print('the mouse moved')
root.bind('<Motion>', motion)
root.mainloop()
When the mouse moves in the open window, it is reported that the mouse has moved. Are you looking for something like that?

i have glitching when pressing buttons with the turtle library

I have a pong game that uses a while loop to constantly move the ball to the new co-ordinates through out the game. a move of a few pixels each time. my issue is that when I press buttons to move the pong player paddle, the ball is glitching.
I know that when I press a button the code goes to the function I created to move up. In the split second this is happening, my ball stops moving though.
I was looking for a solution to this problem.
I didn't include my code as I think its a known problem but I was looking for a work around.
Thanks for any help. I can also post my code if needed

Right clicking on plot to zoom in Bokeh

I recently started using Bokeh. Overall I am very satisfied. One thing I find annoying though is that it seems I can only play with the left mouse button and need to manually switch tools everytime I want to pan/zoom/etc... It would be great if I could pan with left mouse buttom and zoom in with right mouse button. Is there a way to automatically 'switch' to zoom tool whenever right mouse button held, and switch back when left mouse button held?
Thanks!

Pygame - first person shooter "look" with mouse

I am not writing a game but a scientific renderer using Pygame. I'd like the controls to work just like in a first-person shooter, so that a user can navigate using a familiar set of controls.
I have tried to write the code to have the same properties as the 'look' feature in e.g. Skyrim or Half-Life but the mouse doesn't move the cursor - it lets you look around and around, in infinite circles. Clicking should have no effect.
The first attempt for the controls:
(code inside a game loop)
delta_y, delta_x = pygame.mouse.get_rel()
rotation_direction.x = float(delta_x)
rotation_direction.y = float(delta_y)
(don't ask me why, but y and x need to be reversed like this to get the expected look directions; must be something to do with the camera transform implementation, which isn't mine.)
This, however, leads to a cursor sitting on top of the window, and when the cursor gets to the edge of the screen the window stops rotating; i.e. the code is reporting the actual position on the screen.
I tried to 'reset' the mouse position every game loop (and, incidentally, hide the mouse):
pygame.mouse.set_pos([150, 150])
pygame.mouse.set_visible(False)
But this generates a symmetrical 'move back to start' delta in the next loop, meaning that you couldn't 'look' anywhere.
To summarize, I want to:
detect actual mouse motion reported from the device
not move/show any OS cursor
not clip at the 'edge of a screen'
What is the best way to do this, using Pygame or other Python hacks?
http://www.pygame.org/docs/ref/mouse.html :
If the mouse cursor is hidden, and input is grabbed to the current display the mouse will enter a virtual input mode, where the relative movements of the mouse will never be stopped by the borders of the screen. See the functions pygame.mouse.set_visible - hide or show the mouse cursor and pygame.event.set_grab (docs) - control the sharing of input devices with other applications to get this configured.
Try calling pygame.mouse.get_rel() once more immediately after the set_pos call to 'throw away' whatever relative movement the set_pos call has performed.
Since you are using pyOpenGL, try gluLookAt() example: How do I use gluLookAt properly?

Categories

Resources