我的乌龟代码有一个错误,我似乎无法在它结束时修复

My turtle code has an error i cant seem to fix at the end of it

我试图让它出现在 turtle 上,但我一直在第 50 行(倒数第二行)"shape_color is not defined"

如果我将其更改为 box_color 我会得到

line 14, in crescent
t.circle(width, extent=180, steps=none)
NameError: name 'none' is not defined

我不明白为什么其余的没有得到这个错误,请帮助。

import random
import turtle as t

def crescent(x, y, width, height, shape_color):
    t.fillcolor(shape_color)
    t.penup()
    t.goto(x, y)
    t.pendown()
    t.begin_fill()
    t.circle(width, extent=180, steps=none)
    t.endfill()
    t.penup()
    t.fillcolor("white")
    t.end_fill()
    t.penup()  
    t.fillcolor("white") 

def star(x, y, width, shape_color):
    t.fillcolor(shape_color)
    t.penup()
    t.goto(x, y)
    t.pendown()
    t.begin_fill()
    for s in range(5):
        t.forward(width)
        t.right(144)
    t.end_fill()
#--------------------------------------------------------------------

t.colormode(255)  
t.tracer(-1)


for n in range(10):
    x = random.randint(-200, 200)
    y = random.randint(-200, 200)

    r = random.randint(0, 255)
    g = random.randint(0, 255)
    b = random.randint(0, 255)
    box_color = (r, g, b)

    width  = random.randint(5, 50) 
    height = random.randint(5, 50) 

    crescent(x, y, width, height, box_color)
    star(x, y, width, height, shape_color)

我认为代码有两个检查点。 首先,将 t.fillcolor(white) 更改为 t.fillcolor("white").

其次,将crescent(x, y, width, height, shape_color改为crescent(x, y, width, height, box_color。因为你赋值变量box_color,而不是shape_color。它只是 'crescent'

上的参数

你有很多错误:

-

for 中你执行了 star(x, y, width, height, shape_color) 但你没有 defive shape_color。您至少需要:

shape_color = box_color
star(x, y, width, height, shape_color)

-

t.circle 中你使用 step=none 但它必须是 NoneN.
但您也可以跳过 step=None,它会默认使用 step=None
请参阅文档:turtle - circle

t.circle(width, extent=180)

-

您在命令 t.endfill() 中忘记了 _ - 它必须是 t.end_fill()

-

函数star需要4个参数def star(x, y, width, shape_color):但是在for循环中你用5个参数执行它star(x, y, width, height, shape_color)'. You have to removeheight`

shape_color = box_color
star(x, y, width, shape_color)

完整代码:

import random
import turtle as t

def crescent(x, y, width, height, shape_color):
    t.fillcolor(shape_color)
    t.penup()
    t.goto(x, y)
    t.pendown()
    t.begin_fill()
    t.circle(width, extent=180)
    t.end_fill()
    t.penup()
    t.fillcolor("white")
    t.end_fill()
    t.penup()  
    t.fillcolor("white") 

def star(x, y, width, shape_color):
    t.fillcolor(shape_color)
    t.penup()
    t.goto(x, y)
    t.pendown()
    t.begin_fill()
    for s in range(5):
        t.forward(width)
        t.right(144)
    t.end_fill()
#--------------------------------------------------------------------

t.colormode(255)  
t.tracer(-1)


for n in range(10):
    x = random.randint(-200, 200)
    y = random.randint(-200, 200)

    r = random.randint(0, 255)
    g = random.randint(0, 255)
    b = random.randint(0, 255)
    box_color = (r, g, b)

    width  = random.randint(5, 50) 
    height = random.randint(5, 50) 

    crescent(x, y, width, height, box_color)
    star(x, y, width, shape_color)

重复放置多边形时,您可能会考虑冲压而不是绘图。冲压时,我们为每个本身具有该形状的形状创建一个海龟。然后我们根据需要移动、着色和调整海龟的大小,然后标记它。这有两个优点:速度;利用图形操作的能力(如 shear)绘图不能:

from random import randint, random
import turtle

SHAPE_SIZE = 20

def crescent(width):
    turtle.begin_poly()
    turtle.circle(width, extent=180)
    turtle.end_poly()

    return turtle.get_poly()

def star(width):
    turtle.begin_poly()
    for _ in range(5):
        turtle.forward(width)
        turtle.right(144)
    turtle.end_poly()

    return turtle.get_poly()

screen = turtle.Screen()
width, height = screen.window_width() / 2, screen.window_height() / 2

turtle.penup()  # use the 'default' turtle to create the other two turtles
turtle.hideturtle()
turtle.setheading(90)
turtle.speed('fastest')

screen.register_shape('crescent', crescent(SHAPE_SIZE))
crescent_turtle = turtle.Turtle('crescent', visible=False)
crescent_turtle.speed('fastest')
crescent_turtle.penup()

screen.register_shape('star', star(SHAPE_SIZE))
star_turtle = turtle.Turtle('star', visible=False)
star_turtle.speed('fastest')
star_turtle.penup()

for _ in range(25):
    x, y = randint(-width, width), randint(-height, height)

    r, g, b = random(), random(), random()
    shape_color = (r, g, b)

    size = randint(5, 50)

    heading = [0, 180][randint(0, 1)]

    for tortoise in [crescent_turtle, star_turtle]:
        tortoise.turtlesize(SHAPE_SIZE / size, outline=1)
        tortoise.setheading(heading)
        tortoise.color(shape_color)
        tortoise.goto(x, y)
        tortoise.stamp()

screen.exitonclick()

在某些方面,它可以简化逻辑(将我对 star()crescent() 的定义与其他解决方案进行比较。)它也有其局限性,例如我们需要使用简单的多边形。