Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在pygame中绘制动态箭头?_Python_Pygame - Fatal编程技术网

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)
也请查看