我怎样才能让加速度从pygame.event.get获取()?

2024-04-20 12:57:10 发布

您现在位置:Python中文网/ 问答频道 /正文

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

for event in pygame.event.get():
  print(event)

我得到了这个

<Event(4-MouseMotion {'rel': (1, 0), 'buttons': (0, 0, 0), 'pos': (92, 366)})>

有人能告诉我怎样才能得到rel后面的两个数字(1,0)吗? 我试过:

real = event.rel
x = real[0]
y = real[1]

我只想要rel后面的数字,应该不会太难,但我拿不到。你知道吗

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

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

Tags: 代码inposeventforget数字real
2条回答

我认为你的问题是你没有检查事件的类型。不是每个事件都有所有可能的属性。只有MOUSEMOTIONJOYBALLMOTION事件具有rel属性。你知道吗

有关完整列表,请参见documentation。你知道吗

您的代码应该如下所示:

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]

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

相关问题 更多 >