亦尘在努力学啦 发表于 2021-7-8 21:59:24

我这没有key属性应该怎么办

前面是这样输入的:

elif event.key.type == pygame.KEYDOWN:
            if event.key == 273 or event.key == 119:
                direct = 'up'
            elif event.key == 274 or event.key == 115:
                direct = 'down'
            elif event.key == 275 or event.key == 100:
                direct = 'right'
            elif event.key == 276 or event.key == 97:
                direct = 'left'

报错就是这样的:
Traceback (most recent call last):
File "C:\Users\亦尘\Desktop\python\贪吃蛇.py", line 48, in <module>
    elif event.key.type == pygame.KEYDOWN:
AttributeError: 'Event' object has no attribute 'key'

而我的pygame是pip************下载的,就是昨天在B站上一直找才找到我能看懂的下载教程

ba21 发表于 2021-7-8 22:13:42

贴全代码。

参考一下下面代码
>>> print = 1
>>> print('abc')
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
    print('abc')
TypeError: 'int' object is not callable

亦尘在努力学啦 发表于 2021-7-8 22:17:28

ba21 发表于 2021-7-8 22:13
贴全代码。

参考一下下面代码


pygame 2.0.1 (SDL 2.0.14, Python 3.9.2)
Hello from the pygame community. https://www.pygame.org/contribute.html
Traceback (most recent call last):
File "C:\Users\亦尘\Desktop\python\贪吃蛇.py", line 48, in <module>
    elif event.key.type == pygame.KEYDOWN:
AttributeError: 'Event' object has no attribute 'key'
这样吗?

亦尘在努力学啦 发表于 2021-7-8 22:35:26

Python 3.9.2 (tags/v3.9.2:1a79785, Feb 19 2021, 13:44:55) on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>>
================== RESTART: C:\Users\亦尘\Desktop\python\贪吃蛇.py ==================
pygame 2.0.1 (SDL 2.0.14, Python 3.9.2)
Hello from the pygame community. https://www.pygame.org/contribute.html
Traceback (most recent call last):
File "C:\Users\亦尘\Desktop\python\贪吃蛇.py", line 48, in <module>
    elif event.key.type == pygame.KEYDOWN:
AttributeError: 'Event' object has no attribute 'key'
>>>
全部粘贴过来了

Twilight6 发表于 2021-7-9 09:24:24



看下这边函数完整的代码

Jin_Yu 发表于 2021-7-9 18:00:37

这个要给完整的代码才能分析,如果你不想看到这个报错就用get去获取,如果要解决你没有key属性就得给完整代码哦
页: [1]
查看完整版本: 我这没有key属性应该怎么办