pygame.key.get_pressed() and event(KEYDOWN/KEYUP)

这里稍微记录一下key.get_pressed() 和 KEYDOWN, KEYUP的用法。

1. key.get_pressed()

返回的是一个布尔值列表,通过True记录该键被按下了,False 表示没有, 但是存在一个缺点是当键按的太快的时候是没办法准确的给定键按下的顺序,在处理text entry 这种我们就不推荐使用。但是想游戏中方向键移动,这种没问题。

eg:

list1 = pygame.key.get_perssed()

if list1[K_w]:

        print("key W is being pressed")

官方解释如下:

pygame.key.get_pressed()

get the state of all keyboard buttons

get_pressed() -> bools

Returns a sequence of boolean values representing the state of every key on the keyboard. Use the key constant values to index the array. A True value means that the button is pressed.

Note:

Getting the list of pushed buttons with this function is not the proper way to handle text entry from the user. There is no way to know the order of keys pressed, and rapidly pushed keys can be completely unnoticed between two calls to pygame.key.get_pressed(). There is also no way to translate these pushed keys into a fully translated character value. See the pygame.KEYDOWN events on the pygame.eventpygame module for interacting with events and queues queue for this functionality.

2. KEYUP/KEYDOWN event

这种是将键的输入作为event的形式记录,code会清楚的handle每一次的key down 和 up, 但是带来一个问题是当key被快速按下时,会产生大量的event,会影响code的处理速度,因此建议在比较少(偶尔)需要键盘操作的情况下,可以使用此种办法。通过event.type == pygame.KEYDOWN or event.type = pygame.KEYUP 来捕获。

 

  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值