如何在pygame中获取键盘输入?

79

我正在使用pygame 1.9.2制作一个游戏。 这是一个相当简单的游戏,玩家控制一艘飞船在五列敌人之间移动,敌人会向下慢慢移动进行攻击。我正在尝试使用左右箭头键来使飞船向左或向右移动。以下是我的代码:

keys=pygame.key.get_pressed()
if keys[K_LEFT]:
    location-=1
    if location==-1:
        location=0
if keys[K_RIGHT]:
    location+=1
    if location==5:
        location=4

它的表现过于出色了,船移动得太快了。让它只移动一个位置,向左或向右都变得几乎不可能。我该如何做才能让船在每次按下键时只移动一次呢?

12个回答

119

你可以从pygame中获取事件,然后留意KEYDOWN事件,而不是查看get_pressed()返回的键(它会给你当前按下的键),而KEYDOWN事件可以显示在该帧中按下了哪些键。

现在你的代码发生的情况是,如果你的游戏以30fps渲染,并且你按住左箭头键半秒钟,你会更新位置15次。

events = pygame.event.get()
for event in events:
    if event.type == pygame.KEYDOWN:
        if event.key == pygame.K_LEFT:
            location -= 1
        if event.key == pygame.K_RIGHT:
            location += 1
为了在按住键时支持连续移动,您需要建立某种限制,可以基于游戏循环的强制最大帧速率,或者通过计数器,在循环的每几个刻度才允许移动。
move_ticker = 0
keys=pygame.key.get_pressed()
if keys[K_LEFT]:
    if move_ticker == 0:
        move_ticker = 10
        location -= 1
        if location == -1:
            location = 0
if keys[K_RIGHT]:
    if move_ticker == 0:   
        move_ticker = 10     
        location+=1
        if location == 5:
            location = 4

然后在游戏循环的某个地方,你会这样做:

if move_ticker > 0:
    move_ticker -= 1

这只允许你每10帧移动一次(所以如果你移动了,计时器会被设置为10,在10帧后才会再次允许你移动)


1
我尝试使用这个,但问题是只有在pygame寻找该事件的确切时刻我按下按钮它才会移动。当我想要它移动时它却不会动。 - Dan G.
2
你必须在每次游戏循环更新时检查事件。你有这样做吗?事件将保留在Pygame的事件队列中,直到你读取它们或推送它们。 - Haz
2
丹,这个答案正是你所要求的,但请记住,如果你能控制速度,使用事件将无法持续移动你的飞船。你可以保留原始代码,一旦检测到按键按下,你可以轻松地忽略按下的按钮N次循环。 - pmoleri
2
丹,我也更新了我的答案,向你展示了一种可以按住键并仍然控制玩家移动速度的方式。 - Haz
我限制了它移动的位置,但我的游戏窗口很小(仅有5个位置),所以不需要重复。如果我打算将其扩大,我会记住你的。 - Dan G.

21

pygame.key.get_pressed() 返回一个包含每个按键状态的列表。如果某个键被按下,则该键的状态为1,否则为0。它是当前时刻按键的快照。在每个帧中需要连续获取按键的新状态。使用pygame.key.get_pressed()来评估按钮的当前状态并实现连续运动:

while True:

    keys = pygame.key.get_pressed()
    if keys[pygame.K_LEFT]:
        x -= speed
    if keys[pygame.K_RIGHT]:
        x += speed
    if keys[pygame.K_UP]:
        y -= speed
    if keys[pygame.K_DOWN]:
        y += speed

这段代码可以通过将"left"从"right"中减去,将"up"从"down"中减去来简化:

while True:

    keys = pygame.key.get_pressed()
    x += (keys[pygame.K_RIGHT] - keys[pygame.K_LEFT]) * speed
    y += (keys[pygame.K_DOWN] - keys[pygame.K_UP]) * speed

键盘事件(参见pygame.event模块)仅在按键状态改变时发生一次。每当按下一个键时,KEYDOWN事件会发生一次。每当释放一个键时,KEYUP事件会发生一次。使用键盘事件进行单个动作或移动。
while True:

    for event in pygame.event.get():
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                x -= speed
            if event.key == pygame.K_RIGHT:
                x += speed
            if event.key == pygame.K_UP:
                y -= speed
            if event.key == pygame.K_DOWN:
                y += speed

另请参阅键盘和按键事件
连续移动的最简示例: replit.com/@Rabbid76/PyGame-ContinuousMovement

import pygame

pygame.init()
window = pygame.display.set_mode((300, 300))
clock = pygame.time.Clock()

rect = pygame.Rect(0, 0, 20, 20)
rect.center = window.get_rect().center
vel = 5

run = True
while run:
    clock.tick(60)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
        if event.type == pygame.KEYDOWN:
            print(pygame.key.name(event.key))

    keys = pygame.key.get_pressed()
    
    rect.x += (keys[pygame.K_RIGHT] - keys[pygame.K_LEFT]) * vel
    rect.y += (keys[pygame.K_DOWN] - keys[pygame.K_UP]) * vel
        
    rect.centerx = rect.centerx % window.get_width()
    rect.centery = rect.centery % window.get_height()

    window.fill(0)
    pygame.draw.rect(window, (255, 0, 0), rect)
    pygame.display.flip()

pygame.quit()
exit()

单个操作的最简示例: replit.com/@Rabbid76/PyGame-ShootBullet

import pygame
pygame.init()

window = pygame.display.set_mode((500, 200))
clock = pygame.time.Clock()

tank_surf = pygame.Surface((60, 40), pygame.SRCALPHA)
pygame.draw.rect(tank_surf, (0, 96, 0), (0, 00, 50, 40))
pygame.draw.rect(tank_surf, (0, 128, 0), (10, 10, 30, 20))
pygame.draw.rect(tank_surf, (32, 32, 96), (20, 16, 40, 8))
tank_rect = tank_surf.get_rect(midleft = (20, window.get_height() // 2))

bullet_surf = pygame.Surface((10, 10), pygame.SRCALPHA)
pygame.draw.circle(bullet_surf, (64, 64, 62), bullet_surf.get_rect().center, bullet_surf.get_width() // 2)
bullet_list = []

run = True
while run:
    clock.tick(60)
    current_time = pygame.time.get_ticks()
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

        if event.type == pygame.KEYDOWN:
            bullet_list.insert(0, tank_rect.midright)

    for i, bullet_pos in enumerate(bullet_list):
        bullet_list[i] = bullet_pos[0] + 5, bullet_pos[1]
        if bullet_surf.get_rect(center = bullet_pos).left > window.get_width():
            del bullet_list[i:]
            break

    window.fill((224, 192, 160))
    window.blit(tank_surf, tank_rect)
    for bullet_pos in bullet_list:
        window.blit(bullet_surf, bullet_surf.get_rect(center = bullet_pos))
    pygame.display.flip()

pygame.quit()
exit()

1
没有猜测,这个人总是帮助我 :) - RixTheTyrunt

12
import pygame
pygame.init()
pygame.display.set_mode()
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit(); #sys.exit() if sys is imported
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_0:
                print("Hey, you pressed the key, '0'!")
            if event.key == pygame.K_1:
                print("Doing whatever")

请注意,K_0和K_1不是唯一的键,如果想查看所有键,请参阅pygame文档。否则,在输入后按tab键。

pygame.

(注意在pygame后面有一个点号)在空闲程序中。请注意,K必须大写。另外请注意,如果您没有为pygame提供显示尺寸(不传递参数),那么它将自动使用计算机屏幕/监视器的大小。祝编码愉快!


2

我认为你可以使用:

pygame.time.delay(delayTime)

delayTime 是毫秒为单位的延迟时间,在事件之前使用。


1
这是因为pygame窗口以60帧每秒的速度运行,当您按下键约1秒时,它会根据事件块的循环更新60帧。
clock = pygame.time.Clock()
flag = true
while flag :
    clock.tick(60)

请注意,如果您的项目中有动画,则图像数量将定义 tick() 中的值数量。假设您有一个角色,需要20组图像进行行走和跳跃,那么您必须制作tick(20)以正确移动角色。

1

试试这个:

keys=pygame.key.get_pressed()
if keys[K_LEFT]:
    if count == 10:
        location-=1
        count=0
    else:
        count +=1
    if location==-1:
        location=0
if keys[K_RIGHT]:
    if count == 10:
        location+=1
        count=0
    else:
        count +=1
    if location==5:
        location=4

这意味着您只移动了1/10的时间。如果它仍然移动得太快,您可以尝试增加您设置的“count”值。

0

做一个类似这样的东西,但基于时间延迟。我立即调用我的函数第一次,然后启动计时器,在按钮被按下的同时,每隔button_press_delta秒调用一次它。

from time import time
before main loop:
button_press_delta = 0.2
right_button_pressed = 0
while not done:
    keys = pygame.key.get_pressed()
    if keys[pygame.K_RIGHT]:
        if not right_button_pressed:
            call_my_function()
            right_button_pressed = 1
            right_button_pressed_time_start = time()
        if right_button_pressed:
            right_button_pressed_time = (
                time() - right_button_pressed_time_start)
            if right_button_pressed_time > button_press_delta:
                call_my_function()
                right_button_pressed_time_start = time()
    else:
        right_button_pressed = 0

0

只是提醒一下,如果你想确保船不会离开屏幕

location-=1
if location==-1:
    location=0

你可能更好地使用

location -= 1
location = max(0, location)

这样,如果跳过-1,您的程序就不会崩溃。


0
Pygames的tick函数(clock.tick(5))返回自上一帧以来的时间(deltatime)。您可以将所有移动乘以这个时间,以便角色无论帧率如何都以相同的速度移动。您还需要再次乘以一个数字,例如50,以确保速度不会太慢。

-2

你应该使用在文档中提到的clock.tick(10)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接