Back to Browse

Pygame Mouse Events

251 views
Mar 29, 2022
23:10

For more Pygame Tutorials visit www.codingschool4kids.com. This course introduces you to animations, keyboard events, and mouse events in pygame. Mouse Events Detecting Mouse Events: In this section, you will learn how to detect mouse events. We will do this by looping through the EventList same as the keyboard events. Inside the event loop we want to check for 3 mouse events: MOUSEBUTTONDOWN, MOUSEBUTTONUP, and MOUSEMOTION. Button Press: The MOUSEBUTTONDOWN event detects when the mouse button is pressed down, and the MOUSEBUTTONUP detects when the mouse button is released. Once any of these events is detected, we need to find out which button was pressed or released. We do that by checking the event.button attribute. Also, we can get the position of the mouse click using the event.pos attribute. Mouse Motion: The MOUSEMOTION event detects when the mouse is moved. We can check the position of the mouse by using event.pos attribute. We can also check if the user is pressing a button while moving the mouse using event.buttons attribute. Event.buttons returns a tuple with three values: (0, 0, 0), for left, middle, and right buttons. If any of the buttons is pressed down, the value will change to a 1 for that button. For more Pygame Tutorials visit www.codingschool4kids.com. Learn Python, Pygame, Raspberry Pi + GPIO, HTML, CSS, JavaScript, Web Development, and more.

Download

0 formats

No download links available.

Pygame Mouse Events | NatokHD