简体   繁体   English

如何改变乌龟的大小?

[英]How to change size of turtle?

I am trying to double the size of the turtle in the window every time I press x on my keyboard.每次按键盘上的x ,我都试图将窗口中乌龟的大小加倍。 I tried using .turtlesize(2,2,2) , but that's not right.我尝试使用.turtlesize(2,2,2) ,但这是不对的。 I need to double every time the key is pressed so if the turtle size is (1,1,1) , it will become (2,2,2) then (4,4,4) and so on each time I press x .每次按下键时我都需要加倍,所以如果海龟大小是(1,1,1) ,那么每次按下x时它都会变成(2,2,2)然后是(4,4,4)等等.

This is what I have so far:这是我到目前为止:

import turtle
turtle.setup(500,500)
wn = turtle.Screen()
wn.title("Commands")
wn.bgcolor("black")

tess = turtle.Turtle()
tess.shape("triangle")
tess.color("red")
tess.left(90)

def increaseSize():
    size = tess.turtlesize()
    increase = tuple([2 * num for num in size])
    tess.turtlesize(increase) #this is where the error occurs

wn.onkey(increaseSize, "x")
wn.listen()

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. turtlesize()三个单独的值,但是您传递了一个包含三个值的元组,因此我们需要将该元组分布在参数列表中。

The default size of a Turtle object is 20 pixels, which is the equivalent of the ratio 1 when resizing the Turtle . Turtle对象的默认大小为20像素,相当于调整Turtle大小时的比率1

For example:例如:

import turtle

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

Output:输出:

(1.0, 1.0, 1)

The first two 1.0 s 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.元组中的前两个1.0表示Turtle的宽度和高度是多少个 20 像素的单位,最后一个1表示Turtle轮廓的宽度。 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.如果您只将一个参数传递到tess.color()括号中,您将无法看到轮廓,因为默认情况下,没有轮廓。

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() ortess.turtesize() :要增加Turtle的尺寸,只需将您希望Turtle的每个尺寸的 20 像素数传入tess.shapesize()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;另一个答案指出, turtlesize函数不接受数组; it takes in int s or float s, so you'll need to unpack the tuple with a * when you pass the tuple into the function.它接受int s 或float s,所以当你将元组传递给函数时,你需要*解压元组。

In your increaseSize function, the tuple and [] wrappers aren't necessary, and only wastes efficiency.在您的increaseSize函数中, tuple[]包装器不是必需的,只会浪费效率。 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() :由于您定义了一个Screen对象wn ,因此使用wn.setup()而不是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:输出:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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