Reputation: 51
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 right. 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
.
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()
Upvotes: 4
Views: 70306
Reputation: 27577
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.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.
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 int
s or float
s, 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:
Upvotes: 6
Reputation: 41905
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.
Upvotes: 1