python - 拼布样式和颜色安排
问题描述
我必须拼凑而成,我需要一个特定的颜色和样式安排。现在我有这个:
[
我的代码:
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()
但我需要补丁样式和颜色的这种整体安排:
其中“F”表示我上面拼凑的第二种拼贴样式,方形排列。
我有点理解如何使用颜色,因为我只使用蓝色和橙色,并if
为红色使用新的声明。我真正挣扎的主要事情是我最终补丁的风格(对角线或方形排列)。
解决方案
我要冒昧地说我想我明白你在问什么。(我觉得沟通问题由您和编辑您的问题的人共同解决,省略了必要的细节。)我相信我们可以通过对row
和column
值进行简单测试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
>
输出
如果这不是您想要的,请修改您的问题以包含正确输出的说明。
推荐阅读
- android - 如何在 Android Studio WebView 上的 Android 7.0 或更高版本中打开图像选择器?
- c - C - 指向一个字符
- ios - 如何在运行时判断 iOS 应用程序是否正在通过 Crashlytics Beta 安装运行
- javascript - 解析 Promise 返回一个值
- node.js - Gulp、mocha 和 istanbul - 不捕获测试
- dart - 如何获得被测文件的覆盖率报告?
- json - tsconfig.json - 扩展 - 编译器不使用其他 tsconfig.json 文件
- scikit-learn - 高斯过程 RBF 内核使用什么各向异性方法?
- html - 使用 FOR XML PATH 在 SQL Server 中格式化颜色
- node.js - 在节点中使用 postcss 修改 css 字符串