Continuous Monitoring of Keyboard Based on pygame

  • 2021-10-25 07:03:32
  • OfStack

pygame continuous monitoring of the keyboard, for your reference, the specific content is as follows

Please look at the following 1 code:


for event in pygame.event.get():
  if event.type == pygame.QUIT:
   exit()
  elif event.type == pygame.KEYDOWN:
   if event.key == pygame.K_RIGHT:
    print(" Right ")
   if event.key == pygame.K_UP:
    print(" Upper ")
   if event.key == pygame.K_DOWN:
    print(" Under ")
   if event.key == pygame.K_LEFT:
    print(" Left ")

For the first time, everyone will write such a code when monitoring keyboard events. We will focus on monitoring the next key on the keyboard. When we run the code block, we will find that these lines of code are different from our ideas. When we develop the game, we mostly hope that we can control it continuously, which can greatly reduce our hand fatigue and be very friendly to players. However, our current code block is to type one keyboard to respond to one event. Therefore, we want to improve the code. There are two ways to improve it. We can choose according to our own understanding.

Method 1: Set intermediate variables


moving_r = False
moving_l = False
moving_u = False
moving_d = False
while True:
 for event in pygame.event.get():
  if event.type == pygame.QUIT:
   exit()
  elif event.type == pygame.KEYDOWN:
   if event.key == pygame.K_RIGHT:
    moving_r = True
   if event.key == pygame.K_UP:
    moving_u = True
   if event.key == pygame.K_DOWN:
    moving_d = True
   if event.key == pygame.K_LEFT:
    moving_l = True
  elif event.type == pygame.KEYUP:
   if event.key == pygame.K_RIGHT:
    moving_r = False
   if event.key == pygame.K_UP:
    moving_u = False
   if event.key == pygame.K_DOWN:
    moving_d = False
   if event.key == pygame.K_LEFT:
    moving_l = False
 if moving_r:
  print(" Right ")
 elif moving_u:
  print(" Upper ")
 elif moving_d:
  print(" Under ")
 elif moving_l:
  print(" Left ")

Four moving variables are introduced to set the initial values of the four variables as Flase, and KEYDOWN and KEYUP methods of pygame are used to change the values of the four variables, so as to realize continuous monitoring of the keyboard. Although this method is easy to understand, it will be very redundant if Xiaobai is not familiar with code reconstruction.

Method 2: Use get_pressed () in pygame


#  Continuous monitoring of keyboard 
 Key_pressed = pygame.key.get_pressed()
 if Key_pressed[K_UP]:
  print(" Upper ")
 if Key_pressed[K_DOWN]:
  print(" Under ")
 if Key_pressed[K_LEFT]:
  print(" Left ")
 if Key_pressed[K_RIGHT]:
  print(" Right ")

In this way, you can continuously monitor keyboard events, but it should be noted here that the brackets are not brackets after the half statement of if under 1, and the second place to pay attention to is the keyboard name. If you directly enter K_UP, etc., you will report an error. There are two solutions. One is to import pygame:


from pygame import *

In this way, no error will be reported. The other one is to add pygame before K_UP, such as:


if Key_pressed[pygame.K_UP]:
  print(" Upper ")

This is also feasible.


Related articles: