简体   繁体   English

Python 2.7.7 / Pygame-如何检测鼠标是否在圆圈内?

[英]Python 2.7.7/Pygame - How to detect if the mouse is within a circle?

Hi I am trying to make a punny cookie clicker type game called py clicker and made an invisible circle over the sprite which is a pie. 嗨,我正在尝试制作一个名为py clicker的小巧的Cookie Clicker类型游戏,并在Sprite(它是馅饼)上制作了一个不可见的圆圈。 How do I detect if the mouse is within the circle so when the user clicks it checks if it is in the circle and adds one to the counter? 如何检测鼠标是否在圆圈内,所以当用户单击鼠标时,会检查鼠标是否在圆圈内并向计数器添加一只?

If you know the x,y of the center of the circle and it's radius then you can calculate the distance from the center of the circle to your mouse pointer when you click. 如果知道圆心的x,y及其radius则可以单击时计算从圆心到鼠标指针的距离。 If it's greater than the radius then you are outside. 如果它大于半径,那么您就在外面。 There is a built in method that might help called math.hypot that will return the length between two points. 有一个内置的方法可能会称为math.hypot ,该方法将返回两点之间的长度。

You could try pygame.sprite.collide_circle() . 您可以尝试pygame.sprite.collide_circle() But you will need another Sprite with small radius and mouse position. 但是您将需要另一个具有较小radiusmouse位置的Sprite

您可以使用图形库并使用称为getMouse的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM