Python 如何在pygame中绘制动态箭头?
我尝试使用Python 如何在pygame中绘制动态箭头?,python,pygame,Python,Pygame,我尝试使用pg.draw.arrow(),但显然不存在这种情况。我想创建一个箭头,其中端点根据直线的旋转进行更新,以便始终对齐。这可能吗?我一直在使用这个函数,这是基于我刚才在stackoverflow上看到的一些东西: def arrow(screen, lcolor, tricolor, start, end, trirad, thickness=2): pg.draw.line(screen, lcolor, start, end, thickness) rotation
pg.draw.arrow()
,但显然不存在这种情况。我想创建一个箭头,其中端点根据直线的旋转进行更新,以便始终对齐。这可能吗?我一直在使用这个函数,这是基于我刚才在stackoverflow上看到的一些东西:
def arrow(screen, lcolor, tricolor, start, end, trirad, thickness=2):
pg.draw.line(screen, lcolor, start, end, thickness)
rotation = (math.atan2(start[1] - end[1], end[0] - start[0])) + math.pi/2
pg.draw.polygon(screen, tricolor, ((end[0] + trirad * math.sin(rotation),
end[1] + trirad * math.cos(rotation)),
(end[0] + trirad * math.sin(rotation - 120*rad),
end[1] + trirad * math.cos(rotation - 120*rad)),
(end[0] + trirad * math.sin(rotation + 120*rad),
end[1] + trirad * math.cos(rotation + 120*rad))))
我将rad
定义为pi/180,以将角度转换为弧度
如果要将其指定给绘图
:
setattr(pg.draw, 'arrow', arrow)
也请查看