我正在研究Python中的一個項目,該項目旨在確定一個人的多任務處理效率。該項目的一部分是讓用戶使用鼠標在屏幕上響應事件。我決定讓用戶點擊一個球。但是,我在驗證鼠標光標實際上處於圓圈範圍內時遇到了我的代碼問題。單擊圓圈內的任意位置時,使用Python驗證鼠標位置是否在圓圈內。
有關方法的代碼如下。圓的半徑爲10.
#boolean method to determine if the cursor is within the position of the circle
@classmethod
def is_valid_mouse_click_position(cls, the_ball, mouse_position):
return (mouse_position) == ((range((the_ball.x - 10),(the_ball.x + 10)),
range((the_ball.y + 10), (the_ball.y - 10))))
#method called when a pygame.event.MOUSEBUTTONDOWN is detected.
def handle_mouse_click(self):
print (Ball.is_valid_mouse_click_position(self.the_ball,pygame.mouse.get_pos))
不管我在圓圈內點擊的位置,布爾值仍然返回False。
我不確定你會如何相信給定的代碼會起作用... –
我不確定你真的覺得你的評論對我有用。我不熟悉Python。 –
這比「知道Python」要低得多。 –