Python 如何从pygame.event.get()中获取加速度?
我开始玩pygame,我面临一个问题。 我的代码是: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
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]