Python turtle set start position

后端 未结 5 2190
长情又很酷
长情又很酷 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条回答
  • 2020-12-20 03:40

    You can set the world coordinates to something else. For example, to start near the lower left corner, do:

    turtle.setworldcoordinates(-1, -1, 20, 20)
    

    This will make the entire window be 21x21 "units" and place the origin one unit from the bottom and left edges. Whatever position you command will also be in terms of these units (rather than pixels).

    0 讨论(0)
  • 2020-12-20 03:44

    Python turtle, change start position:

    import turtle
    a = turtle.Turtle()      #instantiate a new turtle object called 'a'
    a.hideturtle()           #make the turtle invisible
    a.penup()                #don't draw when turtle moves
    a.goto(-200, -200)       #move the turtle to a location
    a.showturtle()           #make the turtle visible
    a.pendown()              #draw when the turtle moves
    a.goto(50, 50)           #move the turtle to a new location
    

    The turtle becomes visible and starts drawing at position -200, -200 and goes to 50, 50.

    Here's the documentation on how you can change the turtle's state: https://docs.python.org/2/library/turtle.html#turtle-state

    0 讨论(0)
  • 2020-12-20 03:45

    At the beginning of the code, after defining the turtle, you can do:

    tom.penup()
    tom.goto(x coordinate that you want, y coordinate that you want)
    tom.pendown()
    

    This will make the turtle invisible.
    Then go to the position given by x and y, it makes the turtles trail visible again.

    0 讨论(0)
  • 2020-12-20 03:47

    Thomas Antony's setworldcoordinates() solution is viable (+1) but that's a tricky function to work with (easy to mess up your aspect ratio.) The other folks who suggested something like:

    penup()
    goto(...)
    pendown()
    

    are simply wrong and/or didn't read your question as your user will see the turtle move into position. Unfortunately, your question isn't clear when you say, "my turtle square" as it's not clear if you mean the window, a square you've drawn, or a square you're about to draw.

    I'll give you my solution for starting at the top left of the window and you can adjust it to your needs:

    from turtle import Turtle, Screen
    
    TURTLE_SIZE = 20
    
    screen = Screen()
    
    yertle = Turtle(shape="turtle", visible=False)
    yertle.penup()
    yertle.goto(TURTLE_SIZE/2 - screen.window_width()/2, screen.window_height()/2 - TURTLE_SIZE/2)
    yertle.pendown()
    yertle.showturtle()
    
    screen.mainloop()
    

    The turtle's first appearance should be at the top left of the window.

    0 讨论(0)
  • 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 :(

    0 讨论(0)
提交回复
热议问题