Here is an example that implements all that we learned in this chapter. It will check for two different types of keystrokes: the KEYUP (a key is let go of) and the KEYDOWN (a key is pressed). However, because pygame.KEYDOWN is blocked, the application won't be able to detect a key being pressed. It will only print "Up up up!" when the key is let go of.
import pygame
pygame.init()
flag = pygame.locals.RESIZABLE
window = pygame.display.set_mode((500, 400), flag)
pygame.event.set_blocked(pygame.KEYDOWN)
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
# if event is QUIT
run = False
if event.type == pygame.KEYUP:
# if event is KEYUP
print(“Up up up!”)
If event.type == pygame.KEYDOWN:
# this will never happen because KEYDOWN is blocked
print(“Down down down!”)
pygame.quit()
View code on Github
<aside> ⚖️ Copyright © 2021 Code 4 Tomorrow. All rights reserved. The code in this course is licensed under the MIT License.
If you would like to use content from any of our courses, you must obtain our explicit written permission and provide credit. Please contact [email protected] for inquiries.
</aside>