蟒蛇龟的颜色

时间:2016-10-21 04:50:38

标签: python turtle-graphics

嘿我必须在python中创建一个程序,该程序从用户那里获取行数,列数,方边长度和三种颜色的输入。然后程序必须根据行数和列数制作网格,并以交替的检查器模式填充方块。我把它编码到它填充颜色的点,我想知道是否有人可以帮助我。这是我到目前为止所拥有的:

    sourceSets {
        main {
            // let gradle pack the shared library into apk
            jniLibs.srcDirs = ['point/to/your/shared-lib']
       }
    }

1 个答案:

答案 0 :(得分:1)

你拥有所需的所有代码,实际上太多了。你没有正确组装它。您需要将您的计划视为一个故事,并以正确的事件顺序讲述该故事,以使故事有意义。下面是我对代码的修改,以便更好地处理事情以及一些样式调整和代码清理:

from turtle import Turtle, Screen

def draw_square(turtle, length, color):
    turtle.color(color)
    turtle.pendown()

    turtle.begin_fill()
    for _ in range(4):
        turtle.forward(length)
        turtle.left(90)
    turtle.end_fill()

    turtle.penup()
    turtle.forward(length)

def draw_board(turtle, length, colors):
    n = 0

    for row in range(int(rows)):
        turtle.goto(0, length * n)
        for column in range(int(columns)):
            square_color = colors[(column + row) % len(colors)]
            draw_square(turtle, length, square_color)
        n += 1

screen = Screen()

rows = screen.numinput('Number of rows', 'How many rows shall there be?', 5, 1, 10)
columns = screen.numinput('Number of columns', 'How many columns shall there be?', 5, 1, 10)
side_length = screen.numinput('Length of side', 'How long shall the square sides be?', 30, 10, 50)

first_color = screen.textinput('First color', 'What shall the first color be?')
second_color = screen.textinput('Second color', 'What shall the second color be?')
third_color = screen.textinput('Third color', 'What shall the third color be?')

colors = [first_color, second_color, third_color]

turtle = Turtle()
turtle.penup()

draw_board(turtle, side_length, colors)

turtle.hideturtle()

screen.exitonclick()

<强>产生

enter image description here