468,467 Members | 2,678 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,467 developers. It's quick & easy.

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

Expand|Select|Wrap|Line Numbers
  1. #question 4
  2. import turtle as t
  3. def drawhat():
  4.     t.speed(15)
  5.     t.fillcolor("black")
  6.     t.begin_fill()
  7.     t.penup()
  8. #i used goto to make the pen goto a certain x,y cordinates so i can continue drawing from that point
  9.     t.goto(0,250)
  10.     t.pendown()
  11.     t.left(180)
  12.     t.forward(60)
  13.     t.left(90)
  14.     t.forward(50)
  15.     t.right(90)
  16.     t.forward(30)
  17.     t.left(90)
  18.     t.forward(20)
  19.     t.left(90)
  20.     t.forward(120)
  21.     t.left(90)
  22.     t.forward(20)
  23.     t.left(90)
  24.     t.forward(30)
  25.     t.right(90)
  26.     t.forward(50)
  27.     t.end_fill()
  28.  
  29. #i used more than one function because this way i can categorize the different parts of the drawing
  30.  
  31. def drawhead():
  32.     t.speed(15)
  33.     #head
  34.     t.penup()
  35.     t.goto(20,150)
  36.     t.pendown()
  37.     t.circle(50)
  38.     #eyes
  39.     t.fillcolor("blue")
  40.     t.begin_fill()
  41.     t.penup()
  42.     t.goto(0,155)
  43.     t.pendown()
  44.     t.circle(5)
  45.     t.end_fill()
  46.  
  47.     t.fillcolor("red")
  48.     t.begin_fill()
  49.     t.penup()
  50.     t.goto(-50, 155)
  51.     t.pendown()
  52.     t.circle(5)
  53.     t.end_fill()
  54.     #mouth
  55.     t.penup()
  56.     t.goto(0,120)
  57.     t.pendown()
  58.     t.left(90)
  59.     t.forward(60)
  60.  
  61. def midsection():
  62.     t.penup()
  63.     t.goto(-20,100)
  64.     t.pendown()
  65.     t.circle(75)
  66.  
  67. def drawarms():
  68.     t.penup()
  69.     t.goto(40,65)
  70.     t.pendown()
  71.     t.right(150)
  72.     t.forward(60)
  73.     t.left(30)
  74.     t.forward(15)
  75.     t.penup()
  76.     t.goto(85,90)
  77.     t.pendown()
  78.     t.forward(25)
  79.     #arm 2
  80.     t.penup()
  81.     t.goto(-90,55)
  82.     t.pendown()
  83.     t.left(60)
  84.     t.forward(50)
  85.     t.left(30)
  86.     t.forward(25)
  87.     t.penup()
  88.     t.goto(-10,20)
  89.     t.pendown()
  90. def arm2():
  91.     t.penup()
  92.     t.goto(-120,100)
  93.     t.pendown()
  94.     t.right(30)
  95.     t.forward(25)
  96.  
  97. def base():
  98.     t.penup()
  99.     t.goto(60,-100)
  100.     t.pendown()
  101.     t.circle(100)
  102.  
  103.  
  104.  
  105.  
  106. #i called them all together at the end      
  107. drawhat()    
  108. drawhead()
  109. midsection()
  110. drawarms()
  111. arm2()
  112. base()
  113.  
so basically after every function the same commands are used so is there another type of function that can replace turtle.goto so the code wont seem repetitive?
3 Weeks Ago #1
1 3482
Banfa
9,052 Expert Mod 8TB
Why would an API contain 2 functrions that do the same thing, that would be crazy and a maintenance issue. What you can do is create your own functions to encapsulate common sets of commands, e.g. nearly every goto is preceded by a penup and followed by a pendown so you could have a function as

Expand|Select|Wrap|Line Numbers
  1. def startLine(x, y):
  2.     t.penup()
  3.     t.goto(x, y)
  4.     t.pendown()
  5.  
Similarly almost every move consists of a turn (left or right) and then moving forward. It should be easy to put that into a function (using a negative value for turn left and a positive value for turn right.

Having functions that encapsulate common sequences will reduce your own code and make it easier to read.
2 Weeks Ago #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

3 posts views Thread by Tuvas | last post: by
4 posts views Thread by Joe | last post: by
20 posts views Thread by elderic | last post: by
1 post views Thread by kmladenovski | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.