python - 我如何让乌龟对象与另一个对象面对相同的方向?
问题描述
因此,就上下文而言,我正在使用 python 海龟图形制作射击游戏。我试图找到可以多次尝试的东西,但无济于事。例如,我尝试过
def direction():
wn.onkey(lambda: shuttle.setheading(90), 'Up')
wn.onkey(lambda: shuttle.setheading(180), 'Left')
wn.onkey(lambda: shuttle.setheading(0), 'Right')
wn.onkey(lambda: shuttle.setheading(270), 'Down')
direction()
bullet.setheading(direction())
和
if bullet.distance(shuttle) < 15:
28 - if shuttle.setheading == 90:
29 - bullet.setheading(90)
30 - if shuttle.setheading == 180:
31 - bullet.setheading(180)
32 - if shuttle.setheading == 0:
33 - bullet.setheading(0)
34 - if shuttle.setheading == 270:
35 - bullet.setheading(270)
我为行号道歉,但删除它们似乎太麻烦了。
解决方案
假设你有 2 只海龟:turtle1
和turtle2
. 要转向turtle2
的方向turtle1
,请使用以下代码:
turtle2.setheading(turtle1.heading())
仅供参考:在您的第一个代码片段中,direction
返回None
,因为它只添加事件,所以它不起作用。
注意:我建议将 tkinter 用于游戏,因为 turtle 是为教育用途而创建的,而且速度非常慢。
推荐阅读
- php - 从 mysql 检索的 PHP json 格式表示 Unicode 中的数据
- bash - 努力打印出IP(openvpn)Tun0地址
- validation - 在 MIPS 程序集中验证用户整数输入
- python - 如何按列分组熊猫
- java - 如何使用户输入 0 产生输出“1234”
- sql - 将数组与先前的值合并(在定义的条件下)
- spring-boot - Keycloak 根据方法 [GET, POST, PATCH] 设置权限
- macos - opencv_createsamples 错误输出(分段错误:11)
- javascript - 如何仅在访问者到达页面底部后触发动画?
- r - R:如何通过条件/匹配在字符串中只留下一个值?