简体   繁体   English

我的乌龟代码有一个错误,我似乎无法解决

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

I'm trying to get it to show up on turtle but I keep getting "shape_color is not defined" on line 50(second to last line) 我正在尝试让它显示在乌龟上,但在第50行(倒数第二行)上一直显示“ shape_color未定义”

If I change it to box_color I get 如果我将其更改为box_color

line 14, in crescent 第14行,新月
t.circle(width, extent=180, steps=none) t.circle(宽度,范围= 180,步长=无)
NameError: name 'none' is not defined NameError:名称“ none”未定义

I don't understand why the rest don't get this error, please help. 我不明白为什么其余的都不会出现此错误,请提供帮助。

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)

I think the code has two check points. 我认为代码有两个检查点。 first, change t.fillcolor(white) to t.fillcolor("white") . 首先,将t.fillcolor(white)更改为t.fillcolor("white")

Second, change crescent(x, y, width, height, shape_color to crescent(x, y, width, height, box_color . Because you assignment variable box_color, not shape_color. It's just parameter on 'crescent' 其次,将crescent(x, y, width, height, shape_colorcrescent(x, y, width, height, box_color 。因为您分配变量box_color而不是shape_color。这只是'crescent'上的参数

Yoy have many mistakes: Yoy有很多错误:

- --

Inside for you execute star(x, y, width, height, shape_color) but you didn't defive shape_color . 里面for你执行star(x, y, width, height, shape_color)但你没有defive shape_color You need at least: 您至少需要:

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

- --

In t.circle you use step=none but it has to be None with upper N . t.circle您使用step=none但必须为NoneN较高。
But you can also skip step=None and it will use step=None as default. 但是您也可以跳过step=None ,它将默认使用step=None
See documentation: turtle - circle 参见文档: 乌龟-圆

t.circle(width, extent=180)

- --

You forgot _ in command t.endfill() - it has to be t.end_fill() 你忘了_在命令t.endfill() -它必须是t.end_fill()

- --

Function star expects 4 arguments def star(x, y, width, shape_color): but in for loop you execute it with 5 arguments star(x, y, width, height, shape_color)'. You have to remove 函数star需要4个参数def star(x, y, width, shape_color):但是在for循环中,您需要使用5个参数star(x, y, width, height, shape_color)'. You have to remove执行它star(x, y, width, height, shape_color)'. You have to remove star(x, y, width, height, shape_color)'. You have to remove height` star(x, y, width, height, shape_color)'. You have to remove高度`

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

Full code: 完整代码:

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)

When repeatedly laying down polygons, you might consider stamping instead of drawing . 重复放置多边形时,您可以考虑冲压而不是绘图 When stamping, we create a turtle for each shape that itself has that shape. 冲压时,我们为每个具有该形状的形状创建一个乌龟。 We then move, color and size the turtle as desired and then stamp it. 然后,根据需要移动,着色和调整乌龟的大小,然后对其进行标记。 This has a couple of advantages: speed; 这有两个优点:速度; the ability to tap into graphic operations (like shear ) that drawing can't: 利用图形操作无法执行的图形操作(例如剪切 ):

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

In some ways, it can simplify the logic (compare my definitions of star() and crescent() to other solutions.) It also has its limitations, eg we need to work with simple polygons. 在某些方面,它可以简化逻辑(将star()crescent()与其他解决方案进行比较。)它也有其局限性,例如,我们需要使用简单的多边形。

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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