简体   繁体   中英

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)

If I change it to box_color I get

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

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

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'

Yoy have many mistakes:

-

Inside for you execute star(x, y, width, height, shape_color) but you didn't 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 .
But you can also skip step=None and it will use step=None as default.
See documentation: turtle - circle

t.circle(width, extent=180)

-

You forgot _ in command t.endfill() - it has to be 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(x, y, width, height, shape_color)'. You have to remove height`

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.

在此处输入图片说明

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