使用 Python 海龟在螺旋中绘制螺旋

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41471370/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 01:01:46  来源:igfitidea点击:

Drawing a spiral in a spiral using Python turtle

pythonturtle-graphics

提问by digi_life

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!

这是我想要的照片。我想要布拉德的外部显示,然后在里面的圆圈包括螺旋。它应该看起来像附加的螺旋图像。谢谢!

bradangie

bradangie

回答by cdlane

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:

除了angie = turtle.Turtle()(not turtle.Screen()) 之外,您可能会注意到的另一个问题是您的windowexitonclick()语句将无效。那就是单击窗口不会退出并关闭窗口,因为它是在无限while True:循环之后出现的:

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 forloop with a range as you use elsewhere so that angieeventually stops and lets the next line of code execute.

所以永远不会达到。解决此问题的最简单方法是,在不增加计时器复杂性的情况下,使其成为一个for循环,其范围与您在其他地方使用的一样,以便angie最终停止并让下一行代码执行。

Finally, it doesn't quite look like your target as bradis drawing five sides to his square instead of four. Once we fix that, it looks correct and angiestarts from the middle instead of the edge.

最后,它看起来不太像您的目标,因为它brad正在为他的正方形绘制五个边而不是四个边。一旦我们解决了这个问题,它看起来是正确的,并且angie从中间而不是边缘开始。

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

一旦angie完成她的设计,你应该能够只需点击窗口上把它赶走。对于像这样的复杂设计,我很想将其设置turtle.speed()为“快速”和“最快”,因为我没有耐心。(代替数字使用“最快”、“快”、“正常”、“慢”和“最慢”等词来避免意外,除非您需要非常精细地控制速度。)

enter image description here

enter image description here

回答by Rob?

This line is wrong:

这一行是错误的:

angie = turtle.Screen()

It should be:

它应该是:

angie = turtle.Turtle()

回答by ppasler

angieis a Turtlenot a Screen.

angieTurtle不是Screen

Change line 22 to angie = turtle.Turtle()

将第 22 行更改为 angie = turtle.Turtle()

回答by Ardov

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

您只需要调整螺旋的大小即可获得和谐

回答by Amar Nath

enter image description here

在此处输入图片说明

from turtle import Turtle, Screen

从海龟进口海龟,屏幕

def draw_square(some_turtle):

def draw_square(some_turtle):

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

def draw_art():

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

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

draw_art()

draw_art()

window.exitonclick()

window.exitonclick()

回答by j.doecozynot

#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()