How to change size of turtle?

后端 未结 2 1157
鱼传尺愫
鱼传尺愫 2020-12-12 07:46

I am trying to double the size of the turtle in the window every time I press x on my keyboard. I tried using .turtlesize(2,2,2), but that\'s not r

相关标签:
2条回答
  • 2020-12-12 08:16

    Change this line:

    tess.turtlesize(increase)
    

    to instead be:

    tess.turtlesize(*increase)
    

    turtlesize() wants three separate values but you were passing one tuple of three values so we need to spread that tuple across the argument list.

    0 讨论(0)
  • 2020-12-12 08:23

    The default size of a Turtle object is 20 pixels, which is the equivalent of the ratio 1 when resizing the Turtle.

    For example:

    import turtle
    
    tess = turtle.Turtle()
    print(tess.shapesize())
    

    Output:

    (1.0, 1.0, 1)
    

    The first two 1.0s in the tuple represents how many units 20 pixels the Turtle's width and height are, and the last 1 represents the width of the Turtle's outline. You won't be able to see the outline if you only pass one argument into the tess.color() brackets, because by default, there is no outline.

    To increase the Turtle's size, simply pass in the number of 20 pixels you want each of the Turtle's dimensions to be into tess.shapesize() or tess.turtesize():

    import turtle
    
    tess = turtle.Turtle()
    tess.shapesize(2, 3, 1) # Sets the turtle's width to 60px and height to 90px
    

    The other answer points out that the turtlesize function does not take in an array; it takes in ints or floats, so you'll need to unpack the tuple with a * when you pass the tuple into the function.

    In your increaseSize function, the tuple and [] wrappers aren't necessary, and only wastes efficiency. Simply use ():

    def increaseSize():
        size = tess.turtlesize()
        increase = (2 * num for num in size)
        tess.turtlesize(*increase)
    

    On top of your code there is

    turtle.setup(500,500)
    wn = turtle.Screen()
    

    Since you defined a Screen object, wn, it's cleaner to use wn.setup() instead of turtle.setup():

    wn = turtle.Screen()
    wn.setup(500,500)
    

    All together:

    import turtle
    
    wn = turtle.Screen()
    wn.setup(500,500)
    
    tess = turtle.Turtle("triangle")
    tess.color("red")
    tess.left(90)
    
    def increaseSize():
        size = tess.turtlesize()
        increase = (2 * num for num in size)
        tess.turtlesize(*increase)
    
    wn.onkey(increaseSize, "x")
    wn.listen()
    

    Output:

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