【问题标题】:Concentric Rainbow Squares同心彩虹广场
【发布时间】:2019-07-21 19:35:11
【问题描述】:

我想使用turtle 制作这张图片,

这是我目前的情况,但有多个错误。

import turtle
import colorsys
import random

def draw_circle(x,y,r,color):
    turtle.up()
    turtle.seth(0)
    turtle.goto(x,y-r)
    turtle.down()
    turtle.fillcolor(color)
    turtle.begin_fill()
    turtle.circle(r)
    turtle.end_fill()

def draw_square(x,y):
    turtle.up()
    turtle.seth(0)
    turtle.goto(x,y)
    turtle.fillcolor(color)
    turtle.pencolor(color)
    turtle.down()
    turtle.begin_fill()

    for i in range(2):
        turtle.fd(w)
        turtle.left(90)
        turtle.fd(h)
        turtle.left(90)

    turtle.end_fill()


turtle.speed(0)

x = 0
y = -200

for i in range(100):
    color = colorsys.hsv_to_rgb(x,1,1)
    turtle.pencolor(color)
    draw_square(x,y)
    x += .01
    y += 10

我收到此错误:

Traceback (most recent call last):
  File "/Users/MBach/Documents/concentric squares.py", line 39, in <module>
    draw_square(x,y)
  File "/Users/MBach/Documents/concentric squares.py", line 24, in draw_square
    turtle.fd(w)
NameError: name 'w' is not defined
>>> 

【问题讨论】:

  • 欢迎来到 StackOverflow。但是,你的问题太笼统了。请更具体地说明您的一些“多重错误”。至少向我们展示第一条错误消息的完整回溯。如果没有,请向我们展示您的代码的输出。换句话说,阅读并关注How to create a Minimal, Complete, and Verifiable example
  • “多个错误”。呃,这甚至没有正确缩进。我建议去opentechschool.github.io/python-beginners/en/… 了解如何使用turtle 绘制矩形。还要密切注意你的缩进,因为python的执行依赖于它。
  • turtle.fd(w) w 在哪里定义?它不是。 h 也不是。

标签: python colors turtle-graphics


【解决方案1】:

在等待 @darksky 的解决方案完成时 ;-) 我编写了这个替代示例,它使用 stamping 而不是 drawing 来简化代码并加快图形速度:

import turtle
import colorsys

STEP = 2  # distance between squares
WIDTH = 375  # width of the biggest square

CURSOR_SIZE = 20

def draw_square(width, color):
    turtle.shapesize(width / CURSOR_SIZE)
    turtle.pencolor(color)
    turtle.stamp()

turtle.shape('square')
turtle.fillcolor('white')

hue = 0

for width in range(WIDTH, 0, -2 * STEP):
    color = colorsys.hsv_to_rgb(hue, 1, 1)
    draw_square(width, color)
    hue += 0.01

turtle.done()

冲压并不是所有海龟问题的解决方案,但像这样简单的几何图形,它有它的优势:

【讨论】:

【解决方案2】:

除了缩进之外,这里还有一些问题表明您可能从某个地方随机复制粘贴了其中的一些代码,而没有密切注意它的作用。例如这个 for 循环:

for i in range(2):
    turtle.fd(w)
    turtle.left(90)
    turtle.fd(h)
    turtle.left(90)

包含变量wh,但您的代码中没有定义这些变量。让我们首先了解如何绘制正方形。海龟必须做的两个步骤:

  1. 转到给定坐标 (x, y)
  2. 通过 4 次 90 度转弯画出正方形的形状。

要在不绘制任何东西的情况下转到某个坐标,我们必须用turtle.penup() 提起笔,用turtle.goto() 去那里,用turtle.pendown() 放下笔。所以你的draw_square 应该是这样的:

def draw_square(x, y, width):
    turtle.penup()
    turtle.goto(x, y)
    turtle.pendown()
    turtle.forward(width)
    turtle.right(90)
    turtle.forward(width)
    turtle.right(90)
    turtle.forward(width)
    turtle.right(90)
    turtle.forward(width)
    turtle.right(90)

接下来,让我们看看您编写的第二个 for 循环:

x = 0
y = -200

for i in range(100):
  color = colorsys.hsv_to_rgb(x, 1, 1)
  turtle.pencolor(color)
  draw_square(x, y)
  x += .01
  y += 10

你的想法有点正确。从 x, y 坐标开始,在每次迭代中更新它,更新颜色,然后绘制一个正方形。然而,有几个问题。首先,虽然 x 必须增加,但 y 实际上必须减少。这是因为当您从屏幕顶部到底部时,y 值会减小。其次,它们必须改变的量必须相等,因为正方形是同心的。

让我们定义一个step,即正方形之间的距离,width,正方形的大小,一个初始位置,我们必须确保hue 的增量比 x 和 y 慢得多。

step = 3 # distance between squares
width = 600 #width of the biggest square
x, y = -width / 2,  width / 2 
hue = 0

for i in range(100):
    color = colorsys.hsv_to_rgb(hue, 1, 1)
    turtle.pencolor(color)
    draw_square(x, y, width)
    x += step
    y -= step
    width -= 2 * step
    hue += 0.01

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-11-15
    • 2015-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-07
    • 2011-03-12
    相关资源
    最近更新 更多