Python 如何在pygame中设置鼠标在圆周围的位置
我试图在pygame中创建这个程序来学习代码/数学,但我不知道如何在pygame中锁定鼠标在圆圈周围的位置,有什么帮助吗Python 如何在pygame中设置鼠标在圆周围的位置,python,pygame,Python,Pygame,我试图在pygame中创建这个程序来学习代码/数学,但我不知道如何在pygame中锁定鼠标在圆圈周围的位置,有什么帮助吗 import pygame pygame.init() x = 250 y = 250 window = pygame.display.set_mode((500, 500)) pygame.display.set_caption("around circle") #line to be crea
import pygame
pygame.init()
x = 250
y = 250
window = pygame.display.set_mode((500, 500))
pygame.display.set_caption("around circle")
#line to be created
def Lin(xref, yref):
lin = pygame.draw.line(window, (250, 250, 0), (x, y), (xref, yref), 1)
window_open = True
while window_open:
pygame.display.update()
window.fill((0, 0, 0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
window_open = False
mousepos = pygame.mouse.get_pos()
xref = mousepos[0]
yref = mousepos[1]
# 2 circles to get only the border
cir0 = pygame.draw.circle(window, (250, 250, 250), (x, y), 100, 1)
cir1 = pygame.draw.circle(window, (250, 250, 250), (x, y), 99, 1)
Lin(xref, yref)
pygame.quit()
要使线保持在圆中,只需调整鼠标相对于鼠标距离中心的x/y坐标 以下是更新的代码:
import pygame
import math
pygame.init()
x = 250
y = 250
window = pygame.display.set_mode((500, 500))
pygame.display.set_caption("around circle")
#line to be created
def Lin(xref, yref):
lin = pygame.draw.line(window, (250, 250, 0), (x, y), (xref, yref), 1)
window_open = True
while window_open:
pygame.display.update()
window.fill((0, 0, 0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
window_open = False
mousepos = pygame.mouse.get_pos()
xref = mousepos[0]
yref = mousepos[1]
# get mouse distance from center
dist = math.sqrt((xref-x)**2 + (yref-y)**2)
if (dist > 100): # if mouse outside circle, adjust x/y proportionally
xref = x + (xref - x) * (100/dist)
yref = y + (yref - y) * (100/dist)
# 2 circles to get only the border
cir0 = pygame.draw.circle(window, (250, 250, 250), (x, y), 100, 1)
cir1 = pygame.draw.circle(window, (250, 250, 250), (x, y), 99, 1)
Lin(xref, yref)
pygame.quit()
我会做一些类似于Mike67 answer的事情,但会利用pygame Vector的功能(参见文档)
导入pygame
pygame.init()
x=250
y=250
半径=100
中心=pygame.Vector2(x,y)
window=pygame.display.set_模式((500500))
pygame.display.set_标题(“环绕圆圈”)
#要创建的行
def Lin(起点,矢量):
pygame.draw.line(窗口,(250,250,0),起点,起点+向量,1)
窗口打开=真
窗口打开时:
pygame.display.update()
窗口填充((0,0,0))
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
窗口打开=错误
mousepos=pygame.mouse.get_pos()
line_vector=pygame.Vector2(鼠标点)-居中
如果line_vector.length()>半径:
线向量。缩放到长度(半径)
#2圈,仅获取边界
cir0=pygame.draw.circle(窗口,(250250250),(x,y),半径,1)
cir1=pygame.draw.circle(窗口,(250250250),(x,y),半径-1,1)
Lin(中心、直线和矢量)
pygame.quit()
我还修改了Lin()
函数
编辑:
从您的评论中,您表示希望它始终缩放到圆的一侧。在这种情况下,只要跳过测试如果line_vector.length()>radius
,并始终缩放它。如果长度!=radius,但在比较浮点计算的数字时,它们实际上不太可能相同(因为涉及到小数点),您可以看到差异是否小于阈值并将其称为相等,但这确实不值得复杂化
导入pygame
pygame.init()
x=250
y=250
半径=100
中心=pygame.Vector2(x,y)
window=pygame.display.set_模式((500500))
pygame.display.set_标题(“环绕圆圈”)
#要创建的行
def Lin(起点,矢量):
pygame.draw.line(窗口,(250,250,0),起点,起点+向量,1)
窗口打开=真
窗口打开时:
pygame.display.update()
窗口填充((0,0,0))
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
窗口打开=错误
mousepos=pygame.mouse.get_pos()
line_vector=pygame.Vector2(鼠标点)-居中
线向量。缩放到长度(半径)
#2圈,仅获取边界
cir0=pygame.draw.circle(窗口,(250250250),(x,y),半径,1)
cir1=pygame.draw.circle(窗口,(250250250),(x,y),半径-1,1)
Lin(中心、直线和矢量)
pygame.quit()
我想你肯定需要正反方向。谢谢,我还添加了一个:if(dist<100):xref=x+(xref-x)*(100/dist)yref=y+(yref-y)*(100/dist)现在它总是100像素长。我还添加了一个:if line_vector.length()