Question

I have created a program using Turtle Graphics in Python 3 which draws the American flag:

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()

Now for the last step, I want to replace all the turtle.color(0,0,0) and (1,1,1) with a function def get_color(color) that grabs the colors above, however, I'm a bit confused on how to do this. The other functions that need to utilize the color function are:

draw_rectangle(length, height, color) draw_star(l, h, color)

I don't want to use any global variables either.

Oh and another thing I can't fix is why my graphics window when you run the program doesn't close, I have turtle.bye() used in appropriate places (I think), but I always have to manually close the turtle window.

Any help would be appreciated, thanks.

EDIT Mar 2015:

Here is the updated get_color solution:

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)
Était-ce utile?

La solution

You can return multiple values from a function.

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

red, green, blue = get_color()

You can pass multiple return values to a function like this:

turtle.color(*get_color())

get_color() returns three values that get used as the 3 arguments to turtle.color.

Autres conseils

1.I don't think you need such a function, you can set the color very simply (for a single flag):

turtle.color("red")

or

turtle.color("#285087")

or whatever color you want.

EDIT: Then I think it should be so:

def get_color(color):
    c = color
    return c

a = get_color("blue")

and you can use it like so:

turtle.color(a)

2.Your window doesn't close because it should be:

turtle.bye()

or in your code is only

turtle.bye

Also in your code turtle.bye appears twice and in wrong places - you should put it at the end of the code (at least, for me it works this way):

draw_flag()
turtle.bye()

I hope it helps.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top