Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/api/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何从pygame.event.get()中获取加速度?_Python_Pygame - Fatal编程技术网

Python 如何从pygame.event.get()中获取加速度?

Python 如何从pygame.event.get()中获取加速度?,python,pygame,Python,Pygame,我开始玩pygame,我面临一个问题。 我的代码是: for event in pygame.event.get(): print(event) 我得到了这个 <Event(4-MouseMotion {'rel': (1, 0), 'buttons': (0, 0, 0), 'pos': (92, 366)})> 我只想要rel后面的数字,应该不会太难,但我找不到 顺便说一句,我忘了说。。。我的问题是,它一直在告诉我 AttributeError: 'Event' obje

我开始玩pygame,我面临一个问题。 我的代码是:

for event in pygame.event.get():
  print(event)
我得到了这个

<Event(4-MouseMotion {'rel': (1, 0), 'buttons': (0, 0, 0), 'pos': (92, 366)})>
我只想要rel后面的数字,应该不会太难,但我找不到

顺便说一句,我忘了说。。。我的问题是,它一直在告诉我

AttributeError: 'Event' object has no attribute 'rel'

我认为你的问题在于你没有检查事件的类型。并非每个事件都具有所有可能的属性。只有
MOUSEMOTION
JOYBALLMOTION
事件具有
rel
属性

有关完整列表,请参阅

您的代码应该如下所示:

for e in  pygame.event.get():
    # check the type of the event before accessing .rel
    # otherwise an exception will be raised
    if e.type == pygame.MOUSEMOTION:
        x, y = e.rel
        # now do something with x and y

您可以使用
event.rel[0]
event.rel[1]
来访问它们。

它一直告诉我AttributeError:“event”对象没有属性“rel”AttributeError:“event”对象没有属性“rel”AttributeError:“event”对象没有属性“rel”@Stv确切地说。。。这就是我要说的。@Stv,sloth已经说明了为什么会收到属性错误,以及如何解决它。试一试
for e in  pygame.event.get():
    # check the type of the event before accessing .rel
    # otherwise an exception will be raised
    if e.type == pygame.MOUSEMOTION:
        x, y = e.rel
        # now do something with x and y
if event.type == pygame.MOUSEMOTION:  # check for mousemotion event
    if event.buttons[0]: # check for left button down
        if event.rel[1] >0 (or whatever) : #  get the rel[1]