The method of obtaining keyboard keys in Pygame

1 Events and queues

In Pygame, the user's operation on the game is called an "event". Keyboard key presses are one type of event, as are mouse clicks and gamepad input. These events are defined in the Pygame submodule locals. When the user operates the game through the keyboard, mouse or gamepad, these events will be placed in the queue.

2 The basic principle of obtaining keyboard keys

The basic principle of obtaining keys in Pygame is to take out these events from the queue. If it is a keyboard event, continue to judge which key the user pressed, and perform different processing according to different keys.

3 code implementation

3.1 Module import

The Pygame module and the locals submodule under the Pygame module need to be imported, the code is as follows:

import pygame
from pygame.locals import *

3.2 Get events from the queue

The code to get an event from the queue is as follows:

while True:
   for event in pygame.event.get():

Among them, the while True statement means that during the running of the game, the events generated by the user can be continuously obtained; the function of the pygame.event.get() function is to take out all events from the queue and save them in the variable event.

3.3 Determine whether it is a keyboard event

In the while loop, judge the acquired event event, the code is as follows:

if event.type == KEYUP:

Among them, event.type indicates the type of event, and KEYUP is defined in pygame.locals, indicating that a key in the keyboard is pressed.

3.4 Different processing according to different keys

After judging that it is a keyboard key event, it is judged which key in the keyboard the user pressed next, and different processing is performed according to different keys. The function we want to achieve is that the user presses the number 1 key, and the number 1 is displayed on the screen; when the user presses the number 2 key, the number 2 is displayed, and so on. The code looks like this:

if  event.key == pygame.K_1:
   number = '1'
elif event.key == pygame.K_2:
   number = '2'
elif event.key == pygame.K_3:
   number = '3'
elif event.key == pygame.K_4:
number = '4'

Among them, event.key represents the value corresponding to the key, and pygame.K_1 to pygame_K_4 represent the values ​​corresponding to the number 1 key to the number 4 key respectively. The variable number is what to display.

3.4 Create the screen and the content to be displayed

Please refer to "Pygame Display Text" .

4 Complete the code

The complete code to get keyboard input in Pygame looks like this: i

import pygame
from pygame.locals import *
import sys

pygame.init()
screen = pygame.display.set_mode((600,500))
font = pygame.font.Font(None, 100)
number = '0'

while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            sys.exit()
        elif event.type == KEYUP:
            if event.key == pygame.K_ESCAPE:
                sys.exit()
            elif event.key == pygame.K_1:
                number = '1'
            elif event.key == pygame.K_2:
                number = '2'
            elif event.key == pygame.K_3:
                number = '3'
            elif event.key == pygame.K_4:
                number = '4'
            elif event.key == pygame.K_RETURN:
                number = 'RETURN'

    screen.fill((0,0,200))
    imgText = font.render(number, True, (255,255,255))
    screen.blit(imgText, (300, 200))
    pygame.display.update()

After running the code, when the four number keys 1~4 are pressed, the corresponding number will be displayed on the screen, as shown in Figure 1.

Figure 1 shows the numbers corresponding to the keys

Guess you like

Origin blog.csdn.net/hou09tian/article/details/131499340