首页 > 解决方案 > 还有其他类型的函数可以替代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()

         

标签: python

解决方案


你可以试试setpos(),它有点简单。说白了,跟 goto() 差不多。如果您只是一次又一次地来回移动海龟,请使用:

  • forward() 或 fd() 向前移动
  • right() 或 rt() 右转
  • left 或 lt() 左转
  • position() 或 pos() 返回当前海龟的位置。
  • 等等

https://www.geeksforgeeks.org/turtle-setpos-and-turtle-goto-functions-in-python/


推荐阅读