pygame - 按键被按下

pygame - Key pressed

我有点被一个应该是简单的代码所困扰,以检查用户是否按下了 "w" 或 "s"。

下面你可以看到我的代码:

import pygame
pygame.init()

while True:
    for event in pygame.event.get():
        if event.type == pygame.KEYDOWN and event.key == pygame.K_w:
            print('Forward')
        elif event.type == pygame.KEYDOWN and event.key == pygame.K_s:
            print('Backward')  

我是不是忘记了什么?

谢谢!

试试这个:

import pygame
pygame.init()
key = pygame.key.get_pressed()
while True:
    for event in pygame.event.get():
        if event.type == KEYDOWN and event.key == pygame.K_w:
            print('Forward')
        elif event.type == KEYDOWN and event.key == pygame.K_s:
            print('Backward')  

需要创建一个window来接收按键,下面的工作。

import pygame
import sys
pygame.init()

pygame.display.set_mode((100, 100))

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_w:
                print('Forward')
            elif event.key == pygame.K_s:
                print('Backward')

在我看来,这是最简单易懂的方法:

import pygame
pygame.init()
pygame.display.set_mode((300, 300))
running = True

while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
            pygame.quit()
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_w:
                print('Forward')
            elif event.key == pygame.K_s:
                print('Backward')

我更喜欢使用 pygame.quit()

而不是使用 sys.exit() 方法

要求玩家一直按 w 来获得响应是没有用的。如果要读取"pressed"状态。您可以考虑以下内容:

  from pygame import *
    import time
    flag = False # The flag is essential.
    DONE = False
    screen = display.set_mode((500,500))   # 1180, 216
    count=0
    while not DONE:
        event.pump() # process event queue
        keys = key.get_pressed() # It gets the states of all keyboard keys.
        #print("%d"%count,keys)
        count+=1
        if keys[ord('w')]: # And if the key is K_DOWN:
            print("%d w down"%count)
        if keys[ord('s')]: # And if the key is K_DOWN:
            print("%d s down"%count)
        time.sleep(0.1)

了解按键事件工作原理的示例代码。


import pygame
from pygame.locals import *

def main():
    pygame.init()
    pygame.display.set_caption("Move Box With Arrow Keys")
    wndsize = (320, 240)
    display = pygame.display.set_mode(wndsize)

    x = 5
    y = 5

    black = (0, 0, 0)
    white = (255, 255, 255)

    rectpos = (x, y)
    rectdim = (50, 50)

    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_RIGHT:
                    x += 5
                    print("K_RIGHT")
                if event.key == pygame.K_LEFT:
                    x -= 5
                    print("K_LEFT")
                if event.key == pygame.K_UP:
                    y -= 5
                    print("K_UP")
                if event.key == pygame.K_DOWN:
                    y += 5
                    print("K_DOWN")
                
                # Don't allow coords below 0
                if 0 > x:
                    x = 5
                if 0 > y:
                    y = 5
                
                # Don't allow overflow
                if x + rectdim[0] > wndsize[0]:
                    x = wndsize[0] - rectdim[0] - 5
                
                if y + rectdim[1] > wndsize[1]:
                    y = wndsize[1] - rectdim[1] - 5
                
                rectpos = (x, y)
    
        display.fill(white)
        rect = pygame.draw.rect(display, black, ((rectpos), (rectdim)))
        pygame.display.update()

main()

这可能有效:

from pygame import *

init()
if key.get_pressed()[K_w] == True:
    print('Forwards')
if key.get_pressed()[K_s] == True:
    print('Backwards')`