Is there another type of function that can replace turtle.goto?

Viewed 117
**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**

----------**

the requirement for the project was to use at least 5 functions

        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()

most of the commands used in the code are very repetitive

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()

is there a command where the pen would stay the in the same location and have the same outcome without using the turtle.goto function

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)

as you can see they code has the same pattern first penup, then goto, and then pendown

(calling all the functions):
        
        
drawhat()    
drawhead()
midsection()
drawarms()
arm2()
base()

         
2 Answers

You can try setpos(), it is somewhat easy and simple. Frankly speaking, it is almost the same as goto(). If you are only moving turtle to and fro times and times, use:

  • forward() or fd() to move forward
  • right() or rt() to turn right
  • left or lt() to turn left
  • position() or pos() to return the current turtle's position.
  • etc.

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

Well, the only function I can think of is turtle.setpos(), which @Dolfinwu mentioned and is almost the exact same with turtle.goto. Which can be used like this:

def base():
     
    t.penup()
    t.goto(60,-100)
    t.pendown()
    t.circle(100)

(Your original code)

How turtle.setpos() can be used in your code:

def base():
    turtle.penup()
    turtle.setpos(60, -100)
    turtle.pendown()
    turtle.circle(100)

And it would have the exact same outcome if using turtle.goto

P.S. I am also a beginner, so I do not have the best answers, please forgive me

Related