Pygame单推事件

时间:2013-10-21 21:50:53

标签: python pygame

在Pygame中我试图使用每个箭头键在每个方向上将图像翻译10%。现在,我正在使用的代码只要按下按键就会移动图像,我想要的是它只移动一次,无论按键是否仍被按下。

if event.type == KEYDOWN:
    if (event.key == K_RIGHT):
        DISPLAYSURF.fill((255,255,255)) #Clears the screen
        translation_x(100)
        draw(1)
    if (event.key == K_LEFT):
        DISPLAYSURF.fill((255,255,255)) #Clears the screen
        translation_x(-100)
        draw(2)
    if (event.key == K_UP):
        DISPLAYSURF.fill((255,255,255)) #Clears the screen
        translation_y(100)
        draw(3)
    if (event.key == K_DOWN):
        DISPLAYSURF.fill((255,255,255)) #Clears the screen
        translation_y(-100)
        draw(4)

除了使用time.sleep

之外,还有一种更简单的方法

1 个答案:

答案 0 :(得分:2)

您可以使用pygame.key.set_repeat()来禁用仍然关闭的键的重复事件。

但是,默认情况下应禁用它:

  

启用键盘重复时,按下的键将生成多个pygame.KEYDOWN事件。延迟是第一次重复发送pygame.KEYDOWN之前的毫秒数。之后,每隔一个毫秒发送另一个pygame.KEYDOWN。如果没有传递参数,则禁用密钥重复。

     

初始化pygame时,键重复被禁用