【问题标题】:Embedding Functions in Python在 Python 中嵌入函数
【发布时间】:2012-10-08 12:54:35
【问题描述】:

我在 Python 3 中使用 Turtle Graphics 创建了一个程序,用于绘制美国国旗:

import turtle
import time
import random

def draw_rectangle(length, height):
    turtle.up()
    x = -150
    y = 150
    C = height*(7/13)
    D = length*(2/5)
    L = stripe_width = float(round(height/13,1))

    ## Draw rectangle first.
    turtle.color(0,0,0)
    turtle.begin_fill()
    turtle.setpos(x,y)
    turtle.down()
    turtle.forward(length)
    turtle.right(90)
    turtle.forward(height)
    turtle.right(90)
    turtle.forward(length)
    turtle.right(90)
    turtle.forward(height)
    turtle.end_fill()

    ## Then draw the stripes.
    x1 = -150
    y1 = 150-L
    for z in range(13):
        if z%2 == 0:
            r = s = t = 0
        else:
            r = s = t = 1
        turtle.up()
        turtle.speed(100)
        turtle.setpos(x1,y1)
        turtle.setheading(90)
        turtle.down()
        turtle.color(r,s,t)
        turtle.begin_fill()
        turtle.forward(L)
        turtle.right(90)
        turtle.forward(length)
        turtle.right(90)
        turtle.forward(L)
        turtle.right(90)
        turtle.forward(length)
        turtle.end_fill()
        y1 -= L

    ## Finally draw the stars rectangle overlapping the stripes, next is stars.
    x2 = -150+D
    y2 = 150.5-C
    turtle.up()
    turtle.setpos(x2,y2)
    turtle.down()
    turtle.color(0,0,0)
    turtle.begin_fill()
    turtle.forward(D)
    turtle.right(90)
    turtle.forward(C)
    turtle.right(90)
    turtle.forward(D)
    turtle.right(90)
    turtle.forward(C)
    turtle.end_fill()
    turtle.up()

    turtle.bye
    draw_star(-length, height)

def draw_star(l, h):
    for z in range(50):
        if z < 7:
            row = 140
            draw_starrows(row)
        if z < 14:
            row = row - 20
            draw_starrows(row)
        if z < 21:
            row = row - 20
            draw_starrows(row)
        if z < 28:
            row = row - 20
            draw_starrows(row)
        if z < 35:
            row = row - 20
            draw_starrows(row)
            ## This gets the turtle pen out of the way at the very end.
            turtle.up()
            turtle.setpos(-180,100)
        break

def draw_starrows(row):
    x = -160
    y = 150
    for z in range(10):
        x += 15
        turtle.up()
        turtle.color(1,1,1)
        turtle.speed(100)
        turtle.setpos(x,row)
        turtle.begin_fill()
        turtle.down()
        turtle.forward(6.154)
        turtle.left(144)
        turtle.forward(6.154)
        turtle.left(144)
        turtle.forward(6.154)
        turtle.left(144)
        turtle.forward(6.154)
        turtle.left(144)
        turtle.forward(6.154)
        turtle.left(144)
        turtle.end_fill()
    turtle.bye

##def get_color():
##    r = g = b = 0
##    color = r = g = b
##    return color

def draw_flag():
    A = 200
    height = int(A)
##    length = height*1.9
##    C = height*(7/13)
##    D = length*(2/5)
##    E = F = union_height/10
##    G = H = union_length/12
##    stripe_width = height/13
##    diameter_star = stripe_width*(4/5)
    draw_rectangle(height*1.9, height)

draw_flag()

现在对于最后一步,我想用一个函数 def get_color(color) 替换所有的 turtle.color(0,0,0) 和 (1,1,1),但是,我我对如何做到这一点有点困惑。其他需要用到颜色函数的函数有:

draw_rectangle(长度、高度、颜色) draw_star(l, h, color)

我也不想使用任何全局变量。

哦,我无法解决的另一件事是为什么我的图形窗口在您运行程序时没有关闭,我在适当的地方使用了 turtle.bye()(我认为),但我总是必须手动关闭乌龟窗口。

任何帮助将不胜感激,谢谢。

2015 年 3 月编辑:

这是更新后的 get_color 解决方案:

def get_color(color2):
    ## If color2 equals 1, then make the color white.
    if color2 == 1:
        r = g = b = 1
        return (r, g, b)
    ## If color2 equals 0, then make the color red.
    if color2 == 0:
        r = 1
        g = 0
        b = 0
        return (r, g, b)
    ## If color2 equals 2, then make the color black.
    if color2 == 2:
        r = 0
        g = 0
        b = 1
        return (r, g, b)

【问题讨论】:

  • 您希望代码是什么样的?我不确定你用get_color(color)替换所有turtle.color(0,0,0)是什么意思。
  • 哦,对不起,我的意思不是手动输入颜色 (0,0,0) 和 (1,1,1) 我想要一个函数将它们预定义为一个变量,然后将该变量分配给海龟.color()
  • 我已经编辑了我的答案。这有帮助吗?

标签: python function turtle-graphics


【解决方案1】:

你可以从一个函数返回多个值。

def get_color():
    r = g = b = 0
    return r, g, b

red, green, blue = get_color()

您可以将多个返回值传递给这样的函数:

turtle.color(*get_color())

get_color() 返回三个值,用作turtle.color 的三个参数。

【讨论】:

  • 啊,好吧,如果我想使用两种颜色,r,g,b = 0 和 r,g,b = 1,我是否必须创建两个单独的函数,或者我可以返回它有两次差异。变量(r,g,b=0 代表一个,然后例如 d,e,f = 1,代表下一个返回)会起作用吗?
  • 可能类似于get_color(stripe)?一个函数可以有多个return 语句。使用if 语句或其他一些控制流来决定使用哪个return
  • 我现在已经创建了必要的循环:code。唯一的问题是我收到一个错误,上面写着 draw_rectangle(length,height,color): 接受 3 个参数,但只给出了 1 个,我不知道如何将“颜色”与其他函数 draw_rectangle 和 draw_star 结合起来,如描述。
【解决方案2】:

1.我认为你不需要这样的功能,你可以很简单地设置颜色(对于单个标志):

turtle.color("red")

turtle.color("#285087")

或任何你想要的颜色。

编辑:那么我认为应该是这样的:

def get_color(color):
    c = color
    return c

a = get_color("blue")

你可以像这样使用它:

turtle.color(a)

2.您的窗口没有关闭,因为它应该是:

turtle.bye()

或者在你的代码中只有

turtle.bye

同样在你的代码中,turtle.bye 出现了两次并且出现在错误的地方——你应该把它放在代码的末尾(至少,对我来说,它是这样工作的):

draw_flag()
turtle.bye()

希望对你有帮助。

【讨论】:

  • 感谢您发现turtle.bye() 问题,但是,它是针对一个项目的,其中一个要求是使用函数来定义颜色,否则我已经手动添加了颜色。
猜你喜欢
  • 2013-07-27
  • 1970-01-01
  • 1970-01-01
  • 2023-01-11
  • 1970-01-01
  • 1970-01-01
  • 2012-10-21
  • 2018-05-19
  • 2020-05-16
相关资源
最近更新 更多