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

时间:2017-11-21 07:02:04

标签: python turtle-graphics

我试图让它出现在乌龟身上,但我一直在接受" shape_color未定义"第50行(倒数第二行)

如果我将其更改为box_color

  第14行,在新月中      t.circle(width,extent = 180,steps = none)
  NameError:name' 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)

3 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

Yoy有很多错误:

-

forstar(x, y, width, height, shape_color)执行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) 请参阅文档:turtle - circle

_

-

您忘了t.endfill()命令t.end_fill() - 它必须是star

-

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

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)

完整代码:

    <?php

$base_url = 'http://www.inart.com/';
$gr_url   = $base_url . 'api/rest/products/store/1';   //Products in Greek language
$en_url   = $base_url . 'api/rest/products/store/2';   //Products in English Language

$consumer_key    = 'CONSUMER_KEY';
$consumer_secret = 'CONSUMER_SECRET';
$token           = 'TOKEN';
$token_secret    = 'TOKEN_SECRET';

$parameters = array();

$headers    = array(
    'Content-Type' => 'application/json',
    'Accept'       => 'application/json'
);

$filters    = array(
    //Number of products per page (Default 10, Max 100)
    'limit' => 20,
    //Number of page to fetch
    'page'  => 1
); 
//For more filters visit: http://devdocs.magento.com/guides/m1x/api/rest/get_filters.html

$url = $gr_url . '?' . http_build_query($filters);

try {
    $oauthClient = new OAuth($consumer_key, $consumer_secret, OAUTH_SIG_METHOD_HMACSHA1, OAUTH_AUTH_TYPE_AUTHORIZATION);

    $oauthClient->setToken($token, $token_secret);

    $oauthClient->fetch($url, $parameters, OAUTH_HTTP_METHOD_GET, $headers);

    $json = $oauthClient->getLastResponse();

    echo $json;
} catch (Exception $e) {
    echo $e->getMessage();
}

答案 2 :(得分:0)

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

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()的定义与其他解决方案进行比较。)它也有其局限性,例如我们需要使用简单的多边形。

enter image description here