拼布款式及配色

Patchwork style and color arrangement

我要做一个拼布,我需要一个特定的颜色和样式安排。现在我有这个:

[

我的代码:

from graphics import *
colour = ["","",""]

def main():
    size= getSize()
    colour = getColour()
    win = GraphWin("Patchwork", size*100, size*100)
    drawPatches(win, size)

def getSize():
    while True:
        size = ""
        while not(size.isdecimal()):
            size = input("Please enter a valid size between 2 - 9 for your patchwork:")
            if not(size.isdecimal()):
                print("Only digits are allowed.")
        size = eval(size)
        if size < 2 or size > 9:
            print("The valid numbers are 2 - 9.")
        else:
            return size

def getColour():
    for i in range(3):
        chosenColour = validateColour(i + 1)
        colour[i] = chosenColour
    return colour

def validateColour(colourNumber):
        while True:
            colour1 = input("Please pick 3 colours {0}:".format(colourNumber))
            if (colour1 == "red" or colour1 == "blue" or colour1 == "green" or
            colour1 == "cyan" or colour1 == "magenta"):
                if repetitiveColour(colour1):
                    return colour1
                else:
                    print("Cannot enter the same colour more than once.")
            else:
                print("The possible colours are; red, blue, green, cyan and magenta.")

def repetitiveColour(colour1):
    return(colour1 != colour[0] and colour1 != colour[1] and colour1 != colour[2])

def selectingColour(row, size, column):
    return(column * size + row)%3

def drawLine(win, p1, p2, colour):
    line = Line(p1, p2)
    line.setOutline(colour)
    line.draw(win)

def drawRectangle(win, p1, p2, colour):
    square = Rectangle(p1, p2)
    square.setFill(colour)
    square.draw(win)

def drawPatchOne(win, position, colour):
    x = position.getX()
    y = position.getY()
    for i in range(5):
        drawLine(win, Point(x + 20*i, y),
        Point(x + 100, y + 100 - 20*i), colour)
        drawLine(win, Point(x, y + 20*i),
        Point(x + 100 - 20*i , y + 100), colour)
        drawLine(win, Point(x + 100 - 20*i, y),
        Point(x, y + 100 - 20*i), colour)
        drawLine(win, Point(x + 20*i, y + 100),
        Point(x + 100, y + 20*i), colour)

def drawPatchTwo(win, position, colour):
    x = position.getX()
    y = position.getY()
    drawRectangle(win, Point(x + 0, y + 0), Point(x + 100, y + 100), colour)
    for i in range(5):
        drawRectangle(win, Point(x + 5 + 20*i, y + 0),
         Point(x + 15 + 20*i, y + 90), "white")
    for i in range(5):
        for j in range(5):
            drawRectangle(win, Point(x + j*20, y + 10 + i*20),
             Point(x + (j+1)*20, y + (i+1)*20), "white")

def drawPatches(win, size):
    for row in range(size):
        for column in range(size):
            if (row % 2 != 0 and column % 2 == 0 or
            row % 2 != 0 and column % 2 == 0):
                drawPatchTwo(win, Point(row*100, column*100),
                colour[selectingColour(row, size, column)])
            else:
                drawPatchOne(win, Point(row*100, column*100),
                colour[selectingColour(row, size, column)])  

main()

但是我需要这个补丁sylescolors:

的整体安排

其中'F'表示我上面拼凑的第二种补丁样式,正方形排列。

我有点了解如何处理颜色,因为我只使用蓝色和橙色,而对红色使用新的 if 语句。我真正纠结的主要问题是我最终补丁的样式(对角线方形排列)。

我要冒险说我想我明白你在问什么。 (我觉得沟通问题由你和那些编辑你的问题的人共同承担,省略了必要的细节。)我相信我们可以通过对 rowcolumn 值进行简单测试来获得你想要的模式在 drawPatches() 中并简化颜色分配。我修改了你的代码来解决这个问题以及一些代码风格问题:

from graphics import *

VALID_COLOURS = ['red', 'blue', 'green', 'cyan', 'orange']

colours = ['', '', '']

def main():
    size = getSize()
    selectColours()
    win = GraphWin("Patchwork", size*100, size*100)
    drawPatches(win, size)

    win.getMouse()
    win.close()

def getSize():
    while True:
        size = ""

        while not size.isdecimal():
            size = input("Please enter a valid size between 2 - 9 for your patchwork: ")

            if not size.isdecimal():
                print("Only digits are allowed.")

        size = int(size)

        if 2 <= size <= 9:
            break

        print("The valid numbers are 2 - 9.")

    return size

def selectColours():
    for index in range(len(colours)):
        colours[index] = validateColour(index + 1)

def validateColour(colourNumber):
    colour = None

    while True:
        colour = input("Please pick 3 colours {0}: ".format(colourNumber))

        if colour in VALID_COLOURS:
            if not repetitiveColour(colour):
                break

            print("Cannot enter the same colour more than once.")
        else:
            print("The possible colours are:", ", ".join(VALID_COLOURS))

    return colour

def repetitiveColour(colour):
    return colour in colours

def drawLine(win, p1, p2, colour):
    line = Line(p1, p2)
    line.setOutline(colour)
    line.draw(win)

def drawRectangle(win, p1, p2, colour):
    square = Rectangle(p1, p2)
    square.setFill(colour)
    square.draw(win)

def drawPatchOne(win, position, colour):
    x = position.getX()
    y = position.getY()

    for i in range(5):
        drawLine(win, Point(x + i*20, y), Point(x + 100, y + 100 - i*20), colour)
        drawLine(win, Point(x, y + i*20), Point(x + 100 - i*20, y + 100), colour)
        drawLine(win, Point(x + 100 - i*20, y), Point(x, y + 100 - i*20), colour)
        drawLine(win, Point(x + i*20, y + 100), Point(x + 100, y + i*20), colour)

def drawPatchTwo(win, position, colour):
    x = position.getX()
    y = position.getY()

    drawRectangle(win, Point(x + 0, y + 0), Point(x + 100, y + 100), colour)

    for i in range(5):
        drawRectangle(win, Point(x + 5 + i*20, y + 0), Point(x + 15 + i*20, y + 90), 'white')

    for i in range(5):
        for j in range(5):
            drawRectangle(win, Point(x + j*20, y + 10 + i*20), Point(x + (j+1) * 20, y + (i+1) * 20), 'white')

def drawPatches(win, size):
    for row in range(size):
        for column in range(size):
            if 0 < row < size - 1 and 0 < column < size - 1:
                colour = colours[2]
            elif row % 2 != column % 2:
                colour = colours[1]
            else:
                colour = colours[0]

            if row == column:
                drawPatchTwo(win, Point(row*100, column*100), colour)
            else:
                drawPatchOne(win, Point(row*100, column*100), colour)

main()

用法

> python3 test.py
Please enter a valid size between 2 - 9 for your patchwork: 5
Please pick 3 colours 1: blue
Please pick 3 colours 2: orange
Please pick 3 colours 3: red
>

输出

如果这不是您想要的,请修改您的问题以包括正确输出的说明。