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 = (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 将度数转换为弧度
如果要将其分配给draw
:
setattr(pg.draw, 'arrow', arrow)
推荐阅读
- python - 非字母的正则表达式或什么都没有
- c - NUCLEO F401RE 通过 UART 向 nRF DK 板发送错误数据
- bash - 如何在 bash 别名定义中使用控制字符
- powershell - 如何从通过 powershell 从 Teams 导出的 csv 文件中获取 samAccountName
- html - 如何在 React js 中以表格格式显示 API 响应?
- ios - UINavigationController 的 RootViewController 没有被释放,而 UINavigationController 由另一个 UINavigationController 呈现
- javascript - Google Apps 脚本文件上传、保存到 Google 表格、无文件提交表单
- javascript - 使用jquery 动态颜色选择器?
- android - 伪本地化在语言中找不到英语 (XA)
- r - R中有没有办法计算两个不同事件之间的事件发生次数?