Learn Before
Code

Executing Code Based on Mouse Events in Pygame

Say you want to execute a method called shoot() when the player presses the left mouse button on the screen. To do this, you would need to look at the EventType object in an Event in the queue and determine if it's type is MOSUEBUTTONDOWN. Then, if it is, check the button attribute on it and make sure it corresponds to the left mouse button (which is 1).

for event in pygame.event.get() #this block iterates through the queue if event.EventType.type == pygame.MOUSEBUTTONDOWN: if event.EventType.button == 1 #if the button pressed is left mouse button shoot()

0

1

Updated 2021-07-28

Tags

Python Programming Language

Data Science