简体   繁体   中英

Drawing a spiral in a spiral using Python turtle

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!

布拉德安吉

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

在此处输入图片说明

This line is wrong:

angie = turtle.Screen()

It should be:

angie = turtle.Turtle()

angie is a Turtle not a Screen .

Change line 22 to angie = turtle.Turtle()

something simpler would be like this:

import turtle

t = turtle.Turtle()
t.speed(0)
def kuadrado(lado): 
  for i in range(36): #draw the circle of squares
   t.forward(lado)
   t.right(90)
   t.forward(lado)
   t.right(90)
   t.forward(lado)
   t.right(90)
   t.forward(lado)
   t.right(100)
  for i in range(100): #draw the spiral inside the circle of squares
   t.forward(i)
   t.right(80)   

kuadrado(50)

input("<enter>")

you only have to adjust the measures of the spiral to have the harmony

enter image description here

from turtle import Turtle, Screen

def draw_square(some_turtle):

for _ in range(4):
    some_turtle.forward(290)
    some_turtle.right(90)

def draw_art():

# Turtle Brad
brad = Turtle(shape="turtle")
brad.color("cyan")
brad.pensize(2)
brad.speed("fast")  # 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("darkcyan")
angie.pensize(2)
angie.speed(0)  # slightly slower than brad

size = 1

for _ in range(250):
    angie.forward(size)
    angie.right(91)
    size += 0

window = Screen() window.bgcolor("black")

draw_art()

window.exitonclick()

#made by jonny big smokes©™®❤ this code  is illegal to use without permission of the king of gypos jonny big smokes
import turtle
import random
colorlistsize = 3
wn = turtle.Screen()
wn.bgcolor("black")
tess = turtle.Turtle()

def random_color():
    levels = range(32,256,32)
    return list(random.choice(levels) for _ in range(3))


def color_selector():
  listo = []
  for i in range(colorlistsize):
    listo.append(random_color())
  return listo


while True:
  tess.speed(-1000)
  sz = 2 
  ang = random.randint(58, 302)
  color_list = color_selector()
  for i in range(500):
    tess.right(ang)
    tess.forward(sz)
    color = color_list [i % 3]
    tess.color(color)

    sz = sz+1
  tess.clear()
  tess.reset()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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