python - 还有其他类型的函数可以替代turtle.goto吗?
问题描述
**the code below uses 6 functions and approximantly every function uses t. goto
is there any other function I can use to replace it? (similar functions that give the same output)**strong text**
----------**
该项目的要求是至少使用 5 个功能
import turtle as t
def drawhat():
t.speed(15)
t.fillcolor("black")
t.begin_fill()
t.penup()
t.goto(0,250)
t.pendown()
t.left(180)
t.forward(60)
t.left(90)
t.forward(50)
t.right(90)
t.forward(30)
t.left(90)
t.forward(20)
t.left(90)
t.forward(120)
t.left(90)
t.forward(20)
t.left(90)
t.forward(30)
t.right(90)
t.forward(50)
t.end_fill()
代码中使用的大部分命令都非常重复
def drawhead():
t.speed(15)
#head
t.penup()
t.goto(20,150)
t.pendown()
t.circle(50)
#eyes
t.fillcolor("blue")
t.begin_fill()
t.penup()
t.goto(0,155)
t.pendown()
t.circle(5)
t.end_fill()
t.fillcolor("red")
t.begin_fill()
t.penup()
t.goto(-50, 155)
t.pendown()
t.circle(5)
t.end_fill()
#mouth
t.penup()
t.goto(0,120)
t.pendown()
t.left(90)
t.forward(60)
def midsection():
t.penup()
t.goto(-20,100)
t.pendown()
t.circle(75)
def drawarms():
t.penup()
t.goto(40,65)
t.pendown()
t.right(150)
t.forward(60)
t.left(30)
t.forward(15)
t.penup()
t.goto(85,90)
t.pendown()
t.forward(25)
t.penup()
t.goto(-90,55)
t.pendown()
t.left(60)
t.forward(50)
t.left(30)
t.forward(25)
t.penup()
t.goto(-10,20)
t.pendown()
是否有一个命令可以让笔在不使用turtle.goto函数的情况下保持在同一位置并获得相同的结果
def arm2():
t.penup()
t.goto(-120,100)
t.pendown()
t.right(30)
t.forward(25)
def base():
t.penup()
t.goto(60,-100)
t.pendown()
t.circle(100)
如您所见,它们的代码具有相同的模式,首先是 penup,然后是 goto,然后是 pendown
(calling all the functions):
drawhat()
drawhead()
midsection()
drawarms()
arm2()
base()
解决方案
你可以试试setpos(),它有点简单。说白了,跟 goto() 差不多。如果您只是一次又一次地来回移动海龟,请使用:
- forward() 或 fd() 向前移动
- right() 或 rt() 右转
- left 或 lt() 左转
- position() 或 pos() 返回当前海龟的位置。
- 等等
https://www.geeksforgeeks.org/turtle-setpos-and-turtle-goto-functions-in-python/
推荐阅读
- postgresql - 使用 flyway、Access FE 和 Postgresql BE 跟踪 DDL 和 DML 更改
- java - 如何使用 Apache POI 将 .ppt 文件转换为 html?
- r - 计算 R 中 2 个日期时间 ("%Y-%m-%dT%H:%M:%OS-05:00") 列之间经过的时间
- python - Tensorflow tf.train.shuffle_batch() 无法创建内容大于 2GB 的张量原型
- powershell - 来自远程 GCI 命令的过滤结果问题
- r - 如何在一行中获取矩阵的所有值?
- c# - 如何使用 cookie 容器 c# 执行多个发布请求
- intellij-idea - 将光标放在方法/变量名称上不会突然显示它在 intellij 中的所有外观
- php - 根据用户权限在下拉菜单中显示/隐藏选项
- mercurial - Mercurial - 记录文件的第一次修订