Python turtle set start position

后端 未结 5 2192
长情又很酷
长情又很酷 2020-12-20 03:36

How do I set the startpos (topleft of my turtle square) when starting my code?

And I don\'t mean that it starts from the middle and then goes to that position.

5条回答
  •  -上瘾入骨i
    2020-12-20 03:53

    Just translate your co-ordinate system to that of the turtle. Say you want to start in the top left of the square - lets call that (0, 10) for arguments sake.

    Now, whenever you need to specify a co-ordinate for the turtle, just translate it!

    my_start = (0, 10)
    

    If you want to move to (10, 10) - the top right corner, just provide the new co-ordinates:

    >>> new_position = (10 - my_start[0], 10 - my_start[1])
    >>> new_position
    (10, 0)
    

    (10, 0) is to the East of the turtle - in the turtle's co-ordinate system, but for you it's (10, 10) the top right! Everyone wins!

    Edit

    You could just do

    turtle.penup()
    turtle.setx(my_start[0])
    turtle.sety(my_start[1])
    turtle.pendown()
    

    but that's not nearly as fun :(

提交回复
热议问题