标签:python boolean pygame mouseevent
我正在研究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.
解决方法:
我不知道pygame,但也许你想要这样的东西:
distance = sqrt((mouse_position.x - the_ball.x)**2 + (mouse_position.y - the_ball.y)**2)
这是获得鼠标位置和球心之间距离的标准距离公式.然后你会想做:
return distance <= circle_radius
此外,要使sqrt工作,您需要从数学导入sqrt开始
注意:您可以执行以下操作:
x_good = mouse_position.x in range(the_ball.x - 10, the_ball.x + 10)
y_good = mouse_position.y in range(the_ball.y - 10, the_ball.y + 10)
return x_good and y_good
这更符合你所写的内容 – 但这会给你一个允许的区域,这是一个正方形.要获得一个圆,您需要计算距离,如上所示.
注意:我的回答是假设mouse_position具有属性x和y.我不知道这是否真的是因为我不知道pygame,正如我所提到的那样.
标签:python,boolean,pygame,mouseevent 来源: https://codeday.me/bug/20190729/1576104.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。