Learn Before
Code
Executing Code Based on Keyboard Mods in Pygame
Say in your game that pressing the modifying key left shift in addition to the normal key 'w' calls a moveUpFast() method which moves the player very quickly (like sprinting). To do this, you would need to check both the keyboard EventType's type attribute and the mod attribute. An example is shown below.
for event in pygame.event.get() #this block iterates through the queue if event.EventType.type == pygame.KEYDOWN: #if the event has type keydown if event.EventType.type == pygame.K_w: #if event key attribute is equal to w if event.EventType.mod == pygame.KMOD_LSHIFT moveUpFast() #call moveUpFast()! else: moveUp()
0
1
Updated 2021-07-28
Tags
Python Programming Language
Data Science