Introduction
We know that the gaming industry is one of the major developing industries in the current world using current developing technologies. On the parallel side, Python is also one of the famous programming languages that are stretching its hands in all fields due to its simplicity and a full-fledged pack of libraries. Gaming is really a fun way of learning things. Game programming also includes many concepts such as math, computer programming, logic development, and many more. In the current developing technology, AI is also showing some support for game programming. Python can be used for Game Programming by using the Pygame library. Pygame library is one of the Python programming libraries which has very efficient functionalities such as building custom events, taking input from the user through the keyboard, etc. You can learn about how to add custom events in pygame using this link. This article will try to discuss how to take or get keyboard input in our pygame application. Hope You will enjoy the article!
To get Keyboard input
All the gamers will like the games that are more simple and more interactive. Simple in the sense that the game must be available on all devices in an easy fashion, And Interactive in the sense that the game is such that the involvement of the user in the game should be more like pressing the keyboard, moving through the mouse, etc.
To know whether a key is pressed in a game, we will use a method called pygame.event(), as whenever a key is pressed or released, this method will queue all the methods such as pygame.KEYDOWN, pygame.KEYUP, etc., respectively. Another important function is pygame.event.get(), which returns all the events that are happening in the game at the current moment.
So, we will make use of these methods to keep track of whether a key is pressed or not as follows:
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
print(“A key is pressed down”)
Here, we are comparing each event type as to whether it is a keyboard input like KEYDOWN, KEYUP, etc., or not. If it is, then we will do some action on the game logic.
Here KEYDOWN, KEYUP, K_a, K_backspace, etc., are some of the predefined key types. There are a lot of key types which are available in their official documentation, or you can get them through this link.
Each key type can be identified as an event.key type.
For example:
for an event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
# checking if keydown event happened or not
if event.type == pygame.KEYDOWN:
# checking if key "A" was pressed
if event.key == pygame.K_a:
print("Key A has been pressed")
# checking if key "J" was pressed
if event.key == pygame.K_j:
print("Key J has been pressed")
# checking if key "P" was pressed
if event.key == pygame.K_p:
print("Key P has been pressed")
Here, the logic flow is as follows:
For every event in the get() queue, we will check the event.type for keydown event, etc., and we will check the event.key for every keyboard input type such as K_a, K_j, etc. If any check satisfies, we will write their corresponding logic for them.
In this way, we will take care of how to get keyboard input in the pygame application.