SoFunction
Updated on 2024-10-29

pygame implements continuous monitoring of the keyboard

pygame's continuous listening to the keyboard, for your reference, is as follows

Please take a look at the following piece of code:

for event in ():
  if  == :
   exit()
  elif  == :
   if  == pygame.K_RIGHT:
    print("Right.")
   if  == pygame.K_UP:
    print("Up.")
   if  == pygame.K_DOWN:
    print("Down.")
   if  == pygame.K_LEFT:
    print("Left.")

The first time you monitor the keyboard events are generally written in this code we focus on the monitoring of the keyboard up, down, left and right keys. We run the code block we will find that these lines of code and our idea is not the same, we are in the development of the game, we most of the time want to be able to continuous control, so that we can greatly reduce our hand fatigue, and will be very friendly to the players, but our current block of code, is to hit the keyboard responds to an event once. So we need to improve the code. There are two ways to improve it, you can choose according to your own understanding.

Method I:Setting intermediate variables

moving_r = False
moving_l = False
moving_u = False
moving_d = False
while True:
 for event in ():
  if  == :
   exit()
  elif  == :
   if  == pygame.K_RIGHT:
    moving_r = True
   if  == pygame.K_UP:
    moving_u = True
   if  == pygame.K_DOWN:
    moving_d = True
   if  == pygame.K_LEFT:
    moving_l = True
  elif  == :
   if  == pygame.K_RIGHT:
    moving_r = False
   if  == pygame.K_UP:
    moving_u = False
   if  == pygame.K_DOWN:
    moving_d = False
   if  == pygame.K_LEFT:
    moving_l = False
 if moving_r:
  print("Right.")
 elif moving_u:
  print("Up.")
 elif moving_d:
  print("Down.")
 elif moving_l:
  print("Left.")

The introduction of four moving variables will be the initial value of the four variables are set to Flase, with pygame's KEYDOWN and KEYUP method to change the value of the four variables, in order to realize the continuous monitoring of the keyboard, this method is very good to understand, but if it is a white man is not very familiar with the refactoring of the code will be very embellished Yu.

Method II:Using get_pressed() in pygame

# Continuous listening on the keyboard
 Key_pressed = .get_pressed()
 if Key_pressed[K_UP]:
  print("Up.")
 if Key_pressed[K_DOWN]:
  print("Down.")
 if Key_pressed[K_LEFT]:
  print("Left.")
 if Key_pressed[K_RIGHT]:
  print("Right.")

This is also able to continuously listen to keyboard events, but here you need to pay attention to the if half of the statement is followed by parentheses rather than parentheses, the second place to pay attention to is the name of the keyboard, if you directly enter the K_UP and so on will be reported as an error, there are two solutions to one is to import the pygame method:

from pygame import *

That way it won't report an error, the other is to put pygame in front of K_UP like:

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

This is also feasible.

This is the whole content of this article.