pygame实现键盘的连续监控
作者:money_wife 时间:2023-09-26 11:17:10
pygame对键盘的连续监听,供大家参考,具体内容如下
大家请看下面这一段代码:
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
print("右")
if event.key == pygame.K_UP:
print("上")
if event.key == pygame.K_DOWN:
print("下")
if event.key == pygame.K_LEFT:
print("左")
大家首次监控键盘事件一般都会写出这样的代码我们着重来看一下对键盘上下左右键的监控。我们运行代码块我们会发现这几行代码和我们的想法是不一样的,我们在开发游戏的时候,我们大部分时候希望是可以连续控制,这样可以大大减少我们的手部疲劳,对玩家们也会非常的友好,但是我们现在的代码块,是敲一下键盘响应一次事件。所以我们要对代码进行改进,改进的方法有以下两种,大家可以根据自己的理解进行选择。
方法一:设置中间变量
moving_r = False
moving_l = False
moving_u = False
moving_d = False
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
moving_r = True
if event.key == pygame.K_UP:
moving_u = True
if event.key == pygame.K_DOWN:
moving_d = True
if event.key == pygame.K_LEFT:
moving_l = True
elif event.type == pygame.KEYUP:
if event.key == pygame.K_RIGHT:
moving_r = False
if event.key == pygame.K_UP:
moving_u = False
if event.key == pygame.K_DOWN:
moving_d = False
if event.key == pygame.K_LEFT:
moving_l = False
if moving_r:
print("右")
elif moving_u:
print("上")
elif moving_d:
print("下")
elif moving_l:
print("左")
引入四个moving变量将四个变量的初值均设为Flase,用pygame的KEYDOWN和KEYUP方法来改变四个变量的值,以此来实现对键盘的连续监控,这种方法虽然很好理解,但是如果是小白对代码的重构不是很熟悉会显得非常缀余。
方法二:运用pygame中get_pressed()
# 键盘的连续监听
Key_pressed = pygame.key.get_pressed()
if Key_pressed[K_UP]:
print("上")
if Key_pressed[K_DOWN]:
print("下")
if Key_pressed[K_LEFT]:
print("左")
if Key_pressed[K_RIGHT]:
print("右")
这样也是能连续监听键盘事件,但是这里需要注意一下if半段语句后面是中括号并非是小括号,第二个要注意的地方便是键盘名称,如果直接输入K_UP等是会报错的,有两种解决办法一种是导入pygame的方法:
from pygame import *
这样就不会报错了,另一种是在K_UP 前面加上pygame如:
if Key_pressed[pygame.K_UP]:
print("上")
这样也是可行的。
来源:https://blog.csdn.net/epic_wife/article/details/115446156
标签:pygame,键盘,监控
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python爬取12306车次信息代码详解
2022-05-19 03:42:00
![](https://img.aspxhome.com/file/2023/3/103333_0s.png)
Vue路由应用详细讲解
2024-05-05 09:24:03
Python函数的迭代器与生成器的示例代码
2022-09-14 01:29:58
![](https://img.aspxhome.com/file/2023/4/121494_0s.png)
使用python生成杨辉三角形的示例代码
2023-04-03 18:42:31
![](https://img.aspxhome.com/file/2023/7/134587_0s.png)
关于pycharm中pip版本10.0无法使用的解决办法
2021-08-27 15:30:47
![](https://img.aspxhome.com/file/2023/9/85019_0s.jpg)
php 404错误页面实现代码
2023-11-15 07:58:31
go语言编程之select信道处理示例详解
2024-04-26 17:16:14
python自动保存百度盘资源到百度盘中的实例代码
2023-12-18 07:05:01
![](https://img.aspxhome.com/file/2023/6/97276_0s.png)
Chrome和firefox使用比较测评
2010-03-26 12:13:00
![](https://img.aspxhome.com/file/UploadPic/20103/26/2010326124036780s.gif)
JSQL SQLProxy 的 php 版本代码
2023-11-15 01:05:54
Python接口自动化浅析logging封装及实战操作
2022-12-04 12:21:44
![](https://img.aspxhome.com/file/2023/7/91867_0s.jpg)
讲解数据库管理系统必须提供的基本服务
2009-01-04 14:33:00
PHP计算字符串真正的宽度和高度像素(图片加文字水印示例)
2023-05-28 06:48:44
HTTP长连接与短连接使用方法及测试详解
2023-05-08 16:15:25
python中的插入排序的简单用法
2023-09-30 07:57:51
![](https://img.aspxhome.com/file/2023/3/63193_0s.png)
python列表切片和嵌套列表取值操作详解
2021-07-23 21:54:30
JS弹出可拖拽可关闭的div层完整实例
2024-04-19 09:50:56
go语言题解LeetCode1275找出井字棋的获胜者示例
2023-07-22 12:30:41
vue2.x集成百度UEditor富文本编辑器的方法
2024-05-28 15:47:59
![](https://img.aspxhome.com/file/2023/5/123165_0s.png)
如何让新安装的MySQL数据库变得更安全
2009-01-04 13:19:00