Pygame:鼠标特定轴检测 [英] Pygame: Mouse Specific Axis Detection
问题描述
例如,如何仅检测X轴?
How can I detect ONLY the X axis, for example?
maus_x = 0
maus_y = 0
pygame.mouse.get_pos(maus_x, maus_y)
while not done:
for event in pygame.event.get():
if event.type == pygame.MOUSEMOTION:
if maus_x < wx_coord:
angle += 10
理论上,此"pygame.mouse.get_pos"返回一个元组(x,y).但是,我正在定义一个变量来表示该元组中的x和y.问题是,当我移动鼠标(pygame.MOUSEMOTION)时,当我执行写在"maus_x< wx_coord:"中的内容时,它也会执行Y轴功能.那根本没有道理.
In theory, this "pygame.mouse.get_pos" returns a tuple (x, y). But, I'm defining there a variable to represent the x and y in this tuple. The thing is, when I move the mouse (pygame.MOUSEMOTION), when I do what is written in "maus_x < wx_coord:", it executes the function with the Y axis too. And that makes no sense at all.
仅当我在x轴上移动鼠标时才必须执行"angle + = 10".任何人都知道发生了什么事吗? :)
"angle +=10" must be executed ONLY when I move the mouse in the x axis. Anyone have any idea of what is happening? :)
推荐答案
那不是函数调用的工作方式.在您的代码中,maus_x
始终为0
,因为没有修改过的内容.您想要:
That's not how function calls work. In your code, maus_x
is always 0
, since nothing ever modifies it. You want:
while not done:
for event in pygame.event.get():
if event.type == pygame.MOUSEMOTION:
mousex, mousey = pygame.mouse.get_pos()
if mousex < wx_coord:
angle += 10
实际上,您可能只想检查事件直接对象:
In fact, you probably just want to inspect the event object directly:
while not done:
for event in pygame.event.get():
if event.type == pygame.MOUSEMOTION:
mousex, mousey = event.pos
if mousex < wx_coord:
angle += 10
或更妙的是:
while not done:
for event in pygame.event.get():
if event.type == pygame.MOUSEMOTION:
relx, rely = event.rel
if relx != 0: # x movement
angle += 10
这篇关于Pygame:鼠标特定轴检测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!