- Pygame - Discussion
- Pygame - Useful Resources
- Pygame - Quick Guide
- Pygame - Errors and Exception
- Pygame - PyOpenGL
- Pygame - The Sprite Module
- Pygame - Access CDROM
- Pygame - Load cursor
- Pygame - Using Camera module
- Pygame - Playing Movie
- Pygame - Playing music
- Pygame - Mixer channels
- Pygame - Sound objects
- Pygame - Transforming Images
- Pygame - Use Text as Buttons
- Pygame - Moving Rectangular objects
- Pygame - Moving with mouse
- Pygame - Moving with Numeric pad keys
- Pygame - Moving an image
- Pygame - Displaying Text in Window
- Pygame - Loading image
- Pygame - Drawing shapes
- Pygame - Mouse events
- Pygame - Keyboard events
- Pygame - Event objects
- Pygame - Color object
- Pygame - Locals module
- Pygame - Display modes
- Pygame - Hello World
- Pygame - Overview
- Pygame - Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Pygame - Mouse Events
Pygame recongnizes three mouse events, namely, MOUSEMOTION, MOUSEBUTTONUP, and MOUSEBUTTONDOWN. The corresponding event object returns the coordinates of position at which mouse is pressed/released and the button number.
For example, a MOUSEBUTTONDOWN event object will display following result −
<Event(1025-MouseButtonDown { pos : (398, 328), button : 1, window : None})>
Example
To obtain the coordinates of position of button down, we can use get_pos() function associated with event object.
import pygame, sys pygame.init() screen = pygame.display.set_mode((640, 480)) pygame.display.set_caption("Hello World") while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() if event.type == pygame.MOUSEBUTTONDOWN: pos=pygame.mouse.get_pos() btn=pygame.mouse print ("x = {}, y = {}".format(pos[0], pos[1]))
Output
Run above code and press the mouse button at random positions on the game window.
x = 192, y = 160 x = 419, y = 245 x = 204, y = 405 x = 449, y = 17 x = 12, y = 15
The MOUSEMOTION event object captures instantaneous position of moving mouse location.
if event.type == pygame.MOUSEMOTION: pos=event.pos print ("x = {}, y = {}".format(pos[0], pos[1]))
Other important functions and attributes in pygame.mouse module are as follows −
pygame.key.get_pressed | get the state of the mouse buttons |
pygame.mouse.get_pos | get the mouse cursor position |
pygame.mouse.get_rel | get the amount of mouse movement |
pygame.mouse.set_pos | set the mouse cursor position |
pygame.mouse.set_visible | hide or show the mouse cursor |
pygame.mouse.get_visible | get the current visibipty state of the mouse cursor |
pygame.mouse.get_focused | check if the display is receiving mouse input |
pygame.mouse.set_cursor | set the image for the mouse cursor |
pygame.mouse.set_system_cursor | set the mouse cursor to a system variant |
Pygame defines following system cursors −
pygame.SYSTEM_CURSOR_ARROW | arrow |
pygame.SYSTEM_CURSOR_IBEAM | i-beam |
pygame.SYSTEM_CURSOR_WAIT | wait |
pygame.SYSTEM_CURSOR_CROSSHAIR | crosshair |
pygame.SYSTEM_CURSOR_SIZENWSE | double arrow pointing northwest and southeast |
pygame.SYSTEM_CURSOR_SIZENESW | double arrow pointing northeast and southwest |
pygame.SYSTEM_CURSOR_SIZEWE | double arrow pointing west and east |
pygame.SYSTEM_CURSOR_SIZENS | double arrow pointing north and south |
pygame.SYSTEM_CURSOR_SIZEALL | four pointed arrow |
pygame.SYSTEM_CURSOR_NO | slashed circle or crossbones |
pygame.SYSTEM_CURSOR_HAND | hand |
Following statement will set the game window cursor to crosshair.
pygame.mouse.set_system_cursor(pygame.SYSTEM_CURSOR_CROSSHAIR)Advertisements