2012-11-18 18 views
2

例如,我如何檢測X軸?Pygame:鼠標特定軸檢測

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)時,當我執行寫在「max_x < wx_coord:」中的內容時,它也使用Y軸執行函數。這完全沒有意義。

只有當我在x軸上移動鼠標時才必須執行「angle + = 10」。任何人都知道發生了什麼? :)

回答

2

這不是函數調用的工作方式。在你的代碼中,maus_x總是0,因爲沒有任何修改它。你想:

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 

事實上,你可能只是想檢查直接event對象:

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 
+0

我已經試過了已經,它不行。 如果event.type == pygame.MOUSEMOTION: maus_x,mouse_y = pygame.mouse.get_pos() 如果maus_x> 0: 角度+ = 10 它執行 「角度+ = 10」,在y軸如果「get_pos」函數只返回一個值(鼠標移動,無論在哪個軸上) –

+0

@EricsonWillians您不希望'如果maus_x> 0:',您想要第三個示例中的內容 – Xymostech

+0

'maus_x > 0'幾乎總會是真的,因爲鼠標的座標不應該是負數 – Eric