Pythonを使用してパッチワークパターンを描画するタスクが与えられました。ユーザーから幅と高さを取得する必要があります。どちらも3色より大きく10色未満で、4色である必要があります。同じ色にすることはできません。私はその罰金のためのコードを書きました。しかし、私は2つの異なるタイプのパターンを描く必要があります。1つのパターンはすべてエッジに沿っており、パッチの深さは1つだけで、2番目のパターンは中央の正方形を塗りつぶします。さて、これを描くために使用したコードは、最初のパッチを上、下、そして各側に沿って繰り返し、次に2番目のパターンを中央全体に繰り返します。しかし今、私はそれぞれの異なるパッチに色を割り当て、与えられた色を循環して、最初の色から始まり、それらすべてを循環し、最初の色から再び始まるようにする必要があります。
私の質問は、順序が行ごとに異なるため、色を循環させる方法がわからないということです。私がこれまでに書いたものでそれを行うための良い効率的な方法はありますか?個々のパッチを描画し、それを線上で繰り返します。もしそうなら、最良の方法は何ですか?
from graphics import *
def main():
width, height = getDimensions()
colour1, colour2, colour3, colour4 = getColours()
win = drawGraphWin(width, height)
drawPattern(win, width, height, colour1, colour2, colour3, colour4)
def getDimensions():
width = input("How many patches, between 4 and 9, would you like \
horizontally? :")
while True:
try:
width = int(width)
break
except ValueError:
width = input("How many patches, between 4 and 9, would you like \
horizontally? :")
while width < 4 or width > 9:
width = eval(input("How many patches, between 4 and 9, would you like \
horizontally? :"))
height = input("How many patches, between 4 and 9, would you like \
vertically? :")
while True:
try:
height = int(height)
break
except ValueError:
height = input("How many patches, between 4 and 9, would you like \
vertically? :")
while height < 4 or height > 9:
height = eval(input("How many patches, between 4 and 9, would you like \
vertically? :"))
return width, height
def getColours():
colour1 = input("Please enter a colour \
(red, green, blue, yellow, magenta, orange, cyan): ")
while valid1(colour1) == False:
colour1 = input("Please enter a colour \
(red, green, blue, yellow, magenta, orange, cyan): ")
colour2 = input("Please enter a second colour \
(red, green, blue, yellow, magenta, orange, cyan): ")
while valid2(colour1, colour2) == False:
colour2 = input("Please enter a second colour \
(red, green, blue, yellow, magenta, orange, cyan): ")
colour3 = input("Please enter third a colour \
(red, green, blue, yellow, magenta, orange, cyan): ")
while valid3(colour1, colour2, colour3) == False:
colour3 = input("Please enter third a colour \
(red, green, blue, yellow, magenta, orange, cyan): ")
colour4 = input("Please enter a fourth colour \
(red, green, blue, yellow, magenta, orange, cyan): ")
while valid4(colour1, colour2, colour3, colour4) == False:
colour4 = input("Please enter a fourth colour \
(red, green, blue, yellow, magenta, orange, cyan): ")
return colour1, colour2, colour3, colour4
def valid1(colour1):
if any( [colour1 == "red", colour1 == "green", colour1 == "blue", \
colour1 == "yellow", colour1 == "magenta", colour1 == "orange", \
colour1 == "cyan"]):
return True
else:
return False
def valid2(colour1, colour2):
if any( [colour2 == "red", colour2 == "green", colour2 == "blue", \
colour2 == "yellow", colour2 == "magenta", colour2 == "orange", \
colour2 == "cyan"]):
if colour2 == colour1:
return False
else:
return True
else:
return False
def valid3(colour1, colour2, colour3):
if any( [colour3 == "red", colour3 == "green", colour3 == "blue", \
colour3 == "yellow", colour3 == "magenta", colour3 == "orange", \
colour3 == "cyan"]):
if any( [colour3 == colour2, colour3 == colour1]):
return False
else:
return True
else:
return False
def valid4(colour1, colour2, colour3, colour4):
if any( [colour4 == "red", colour4 == "green", colour4 == "blue", \
colour4 == "yellow", colour4 == "magenta", colour4 == "orange", \
colour4 == "cyan"]):
if any( [colour4 == colour3, colour4 == colour2, colour4 == colour1]):
return False
else:
return True
else:
return False
def drawGraphWin(width, height):
win = GraphWin("CW PatchWork Deisgn", width*100, height*100)
win.setCoords(0.0,0.0,4*width,3*height)
for i in range(width):
vLineN = Line(Point(i*4, 0), Point(i*4, height*3))
vLineN.draw(win)
for j in range(height):
hLineN = Line(Point(0, j*3), Point(width*4, j*3))
hLineN.draw(win)
return win
def drawPattern(win, width, height, colour1, colour2, colour3, colour4):
for widthNo in range(width):
drawPatch1(win, widthNo, 0, colour1, colour2, colour3, colour4)
for widthNo in range(width):
drawPatch1(win, widthNo, height-1, colour1, colour2, colour3, colour4)
for heightNo in range(height-2):
drawPatch1(win, 0, heightNo+1, colour1, colour2, colour3, colour4)
for heightNo in range(height-2):
drawPatch1(win, width-1, heightNo+1, colour1, colour2, colour3, colour4)
for innerNoH in range(width-2):
innerWidth = innerNoH +1
for innerNoV in range(height-2):
innerHeight = height - 2 - innerNoV
drawPatch2(win, innerWidth, innerHeight, colour1, colour2, colour3,\
colour4)
win.getMouse()
win.close()
def drawPatch1(win, widthNo, height, colour1, colour2, colour3, colour4):
for i in range(6):
vLineN = Line(Point((0.8*(i))+widthNo*4, 0+height*3), \
Point((0.8*(i))+widthNo*4, 3+height*3))
vLineN.draw(win)
hLineN = Line(Point(0+widthNo*4, (0.6*(i))+height*3), \
Point(4+widthNo*4, (0.6*(i))+height*3))
hLineN.draw(win)
if i == 0:
for j in range(5):
hiMessage = drawHiMessage(0.4+widthNo*4, (0.3+(j*0.6))+height*3,\
win, colour1, colour2, colour3, colour4)
hiMessage.setFill(colour1)
vLineN.setFill(colour1)
hLineN.setFill(colour1)
elif i == 1:
for k in range(5):
hiMessage = drawHiMessage(1.2+widthNo*4, (0.3+(k*0.6))+height*3,\
win, colour1, colour2, colour3, colour4)
hiMessage.setFill(colour2)
vLineN.setFill(colour2)
hLineN.setFill(colour2)
elif i == 2:
for l in range(5):
hiMessage = drawHiMessage(2+widthNo*4, (0.3+(l*0.6))+height*3, \
win, colour1, colour2, colour3, colour4)
hiMessage.setFill(colour3)
vLineN.setFill(colour3)
hLineN.setFill(colour3)
elif i == 3:
for m in range(5):
hiMessage = drawHiMessage(2.8+widthNo*4, (0.3+(m*0.6))+height*3,\
win, colour1, colour2, colour3, colour4)
hiMessage.setFill(colour4)
vLineN.setFill(colour4)
hLineN.setFill(colour4)
elif i == 4:
for n in range(5):
hiMessage = drawHiMessage(3.6+widthNo*4, (0.3+(n*0.6))+height*3,\
win, colour1, colour2, colour3, colour4)
hiMessage.setFill(colour1)
vLineN.setFill(colour1)
hLineN.setFill(colour1)
def drawHiMessage(x, y, win, colour1, colour2, colour3, colour4):
hiMessage = Text(Point(x, y), "hi!")
hiMessage.draw(win)
hiMessage.setSize(7)
return hiMessage
def drawPatch2(win, innerNoH, height, colour1, colour2, colour3, colour4):
for i in range(4):
x = (0.5+i)+innerNoH*4
for j in range(3):
y = (2.2-j)+height*3
sail = drawBoat(x, y, win, colour1, colour2, colour3, colour4)
if i == 0:
sail.setFill(colour1)
elif i == 1:
sail.setFill(colour2)
elif i ==2:
sail.setFill(colour3)
elif i == 3:
sail.setFill(colour4)
def drawBoat(x, y, win, colour1, colour2, colour3, colour4):
sail = Polygon(Point(x-0.5,y), Point(x,y+0.8), Point(x+0.5,y))
hull = Polygon(Point(x-0.5,y-0.1), Point(x+0.5,y-0.1), Point(x+0.3,y-0.2), \
Point(x-0.3,y-0.2))
mast = Line(Point(x,y), Point(x,y-0.1))
sail.draw(win)
hull.draw(win)
mast.draw(win)
hull.setFill("white")
return sail
main()