Python – Drawing a spiral in a spiral using Python turtle

pythonturtle-graphics

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!

brad
angie

Best Answer

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.)

enter image description here