Drawing a spiral in a spiral using Python turtle
Solution 1
In addition to the angie = turtle.Turtle()
(not turtle.Screen()
), another problem you're likely to notice is that your windowexitonclick()
statement will have no effect. That is clicking on the window won't exit and close the window because it comes after an infinite while True:
loop:
while (True):
angie.forward(size)
angie.right(91)
size = size + 1
window.exitonclick()
and so is never reached. The simplest way to fix this is, without adding the complexity of timers, is to make this a for
loop with a range as you use elsewhere so that angie
eventually stops and lets the next line of code execute.
Finally, it doesn't quite look like your target as brad
is drawing five sides to his square instead of four. Once we fix that, it looks correct and angie
starts from the middle instead of the edge.
A rework of your code with the above and other style changes:
from turtle import Turtle, Screen
def draw_square(some_turtle):
for _ in range(4):
some_turtle.forward(200)
some_turtle.right(90)
def draw_art():
# Turtle Brad
brad = Turtle(shape="turtle")
brad.color("yellow")
brad.pensize(2)
brad.speed("normal") # 6/normal is the default so don't need to do it
for _ in range(36):
draw_square(brad)
brad.right(10)
# Turtle Angie
angie = Turtle(shape="turtle")
angie.color("blue")
angie.pensize(2)
angie.speed(5) # slightly slower than brad
size = 1
for _ in range(300):
angie.forward(size)
angie.right(91)
size += 1
window = Screen()
window.bgcolor("black")
draw_art()
window.exitonclick()
Once angie
finishes her design, you should be able to just click on the window to make it go away. For a complex design like this, I'd be tempted to set the turtle.speed()
to "fast" and "fastest" as I've no patience. (Instead of the numbers use the words 'fastest', 'fast', 'normal', 'slow' & 'slowest' instead to avoid surprises unless you need very fine control over the speed.)
Solution 2
This line is wrong:
angie = turtle.Screen()
It should be:
angie = turtle.Turtle()
Solution 3
angie
is a Turtle
not a Screen
.
Change line 22 to
angie = turtle.Turtle()
digi_life
Updated on April 17, 2020Comments
-
digi_life about 4 years
What is wrong with my code for turtle angie? I want her to spiral inside brad's square circle.
My Code:
import turtle def draw_square(some_turtle): for i in range (1,5): some_turtle.forward(200) some_turtle.right(90) def draw_art(): window = turtle.Screen() window.bgcolor("black") #Turtle Brad brad = turtle.Turtle() brad.shape("turtle") brad.color("yellow") brad.speed(6) brad.pensize(2) for i in range(1,37): draw_square(brad) brad.right(10) #Turtle Angie angie = turtle.Screen() angie.shape("turtle") angie.color("blue") angie.speed(5) angie.pensize(2) size=1 while (True): angie.forward(size) angie.right(91) size = size + 1 window.exitonclick() draw_art()
Here are photos of I what I want it to look like. I want the outer part of the brad showing and then the circle inside to include the spiral. It should look like the spiral image attached. Thanks!
-
digi_life over 7 yearsThank you! You explained that very well. This is really helpful. :) How would you make the turtle shape disappear after the drawing is complete?
-
cdlane over 7 years@digi_life, you can use
brad.hideturtle()
(and similarly for angie) to make the turtle itself disappear after it's finished drawing. (Some programs do this before the turtle draws to avoid showing the turtle at work.) -
MrPk over 7 yearscan you edit and add a short explanation about how your code solves the problem?
-
cdlane over 7 yearsThis code doesn't run under current Python 3 and turtle.py as it's assuming the wrong default color mode. Add
turtle.colormode(255)
near the top to get it to run so you can see it has no bearing on the OP's question. -
Nissa over 7 yearsYour copyright notice at the top does nothing. You agreed that everything you post here is the property of Stack Exchange.
-
Foster over 4 yearsIncorrect. You actually need both when running from a file.
turtle.Turtle()
creates the new turtle object, whileturtle.Screen()
opens the screen that the turtle object draws on. -
Foster over 4 yearsIf you are using an online IDE, you don't need the screen, just the init.
-
10 Rep about 4 years@Foster The Op is using IDLE.