[英]My turtle code has an error i cant seem to fix at the end of it
我正在尝试让它显示在乌龟上,但在第50行(倒数第二行)上一直显示“ shape_color未定义”
如果我将其更改为box_color
第14行,新月
t.circle(宽度,范围= 180,步长=无)
NameError:名称“ none”未定义
我不明白为什么其余的都不会出现此错误,请提供帮助。
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'上的参数
Yoy有很多错误:
--
里面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
但必须为None
且N
较高。
但是您也可以跳过step=None
,它将默认使用step=None
。
参见文档: 乌龟-圆
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 remove
执行它star(x, y, width, height, shape_color)'. You have to remove
star(x, y, width, height, shape_color)'. You have to remove
高度`
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)
重复放置多边形时,您可以考虑冲压而不是绘图 。 冲压时,我们为每个具有该形状的形状创建一个乌龟。 然后,根据需要移动,着色和调整乌龟的大小,然后对其进行标记。 这有两个优点:速度; 利用图形操作无法执行的图形操作(例如剪切 ):
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()
与其他解决方案进行比较。)它也有其局限性,例如,我们需要使用简单的多边形。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.