如何在 PyGame 中获得键盘输入?
在使用Python 的pygame 模块时,我们有时需要使用键盘输入进行各种操作,例如向某个方向移动字符。为了实现这一点,我们必须看到所有发生的事件。 Pygame 会跟踪发生的事件,我们可以通过 events.get()函数查看这些事件。在本文中,我们将讨论如何在 pygame 中获取和使用各种键盘输入。
检测是否按下了某个键:
每当按下或释放一个键时, pygame.event() 队列方法 pygame.KEYDOWN 和 pygame.KEYUP 事件分别。
例如,如果我们想检测一个键是否被按下,我们将跟踪 pygame.KEYDOWN 的任何事件是否发生,因此,我们将知道是否有任何键被按下。检测是否有按键按下的代码可以写成:
Python
# importing pygame module
import pygame
# importing sys module
import sys
# initialising pygame
pygame.init()
# creating display
display = pygame.display.set_mode((300, 300))
# creating a running loop
while True:
# creating a loop to check events that
# are occuring
for 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:
# if keydown event happened
# than printing a string to output
print("A key has been pressed")
Python
# importing pygame module
import pygame
# importing sys module
import sys
# initialising pygame
pygame.init()
# creating display
display = pygame.display.set_mode((300, 300))
# creating a running loop
while True:
# creating a loop to check events that
# are occuring
for 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")
# checking if key "M" was pressed
if event.key == pygame.K_m:
print("Key M has been pressed")
输出:
运行此代码后,可以看到每当按下一个键时,终端上都会打印一个字符串“A key has been press”
检测按下了哪个键:
要知道按下了哪个键,我们必须检查 event.key 变量对应于哪些 pygame 键。例如,字母“A”的 pygame 键是“K_a”,那么我们将 event.Key 与 K a 进行比较,如果相同,则表示按下了键“A”。
各种键盘键和相应的 pygame 键是: pygamekey Description K_BACKSPACE backspace K_TAB tab K_CLEAR clear K_RETURN return K_PAUSE pause K_ESCAPE escape K_SPACE space K_EXCLAIM exclaim K_HASH hash K_QUOTEDBL quotedbl K_DOLLAR dollar K_AMPERSAND ampersand K_QUOTE quote K_LEFTPAREN left parenthesis K_RIGHTPAREN right parenthesis K_ASTERISK asterisk K_PLUS plus sign K_COMMA comma K_MINUS minus sign K_PERIOD period K_SLASH forward slash K_0 0 K_1 1 K_2 2 K_3 3 K_4 4 K_5 5 K_6 6 K_7 7 K_8 8 K_9 9 K_COLON colon K_SEMICOLON semicolon K_LESS less-than sign K_EQUALS equals sign K_GREATER greater-than sign K_QUESTION question mark K_AT at K_LEFTBRACKET left bracket K_BACKSLASH backslash K_RIGHTBRACKET right bracket K_CARET caret K_UNDERSCORE underscore K_BACKQUOTE grave K_a,b,c…….z A to Z Alphabet K_DELETE delete K_KP0, K_KP1, K_KP2….K_KP9 keypad 0 to 9 K_KP_PERIOD keypad period K_KP_DIVIDE keypad divide K_KP_MULTIPLY keypad multiply K_KP_MINUS keypad minus K_KP_PLUS keypad plus K_KP_ENTER keypad enter K_KP_EQUALS keypad equals K_UP up arrow K_DOWN down arrow K_RIGHT right arrow K_LEFT Left arrow K_INSERT Insert K_HOME Home K_END End K_PAGEUP Page Up K_PAGEDOWN Page Down K_F1, K_F2, K_F3……K_F15 F1 to F15 K_NUMLOCK Numlock K_CAPSLOCK Capsloack K_SCROLLOCK Scrollock K_RSHIFT Right shift K_LSHIFT Left shift K_RCTRL right control K_LCTRL Left control K_RALT Right alt K_LALT Left alt K_RMETA right meta K_LMETA left meta K_LSUPER left Windows key K_RSUPER right Windows key K_MODE mode shift K_HELP Help K_PRINT Print Screen K_SYSREQ sysrq K_BREAK Break K_MENU Menu K_POWER Power K_EURO Euro
例如,让我们创建一个代码来检查是否按下了键“A”或“J”或“P”或“M”。检查代码将是:
Python
# importing pygame module
import pygame
# importing sys module
import sys
# initialising pygame
pygame.init()
# creating display
display = pygame.display.set_mode((300, 300))
# creating a running loop
while True:
# creating a loop to check events that
# are occuring
for 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")
# checking if key "M" was pressed
if event.key == pygame.K_m:
print("Key M has been pressed")
输出:
当我们运行这段代码并按下给定的键时,相应的字符串将打印在终端上。