簡體   English   中英

我的烏龜代碼有一個錯誤,我似乎無法解決

[英]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_colorcrescent(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但必須為NoneN較高。
但是您也可以跳過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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM