简体   繁体   中英

drawing an X with python Turtle graphics

I have a really simple task to draw an X in python using Turtles, but i cannot seem to understand how the setposition() method works.

my code currently draws the first line correct, but then the second line skews up too much no matter what i try.

t.right(45)
t.pendown()
t.setposition(50,-50)
t.penup()
t.left(90)
t.setposition(0,-50)
t.pendown()
t.setposition(50,50)

Your first line starts at (0,0) and got to (50, -50) , thus goes 50 units in X and Y direction. But you second ist going from (0,-50) to (50,50) , thus goes 50 units in X direction and 100 units in Y direction. Additionally your right and left have no effect.

One possible solution would be:


t.setposition(-50,50)
t.pendown()
t.setposition(50,-50)
t.penup()
t.setposition(-50,-50)
t.pendown()
t.setposition(50,50)

Another approach you can take is to avoid setposition() altogether and think like a turtle. That is, crawl forward, backward and turn rather than teleport:

import turtle as t

t.right(45)
t.forward(70)
t.backward(140)
t.forward(70)
t.left(90)
t.forward(70)
t.backward(140)

t.hideturtle()
t.done()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM