gpt4 book ai didi

python - 如何循环不同的颜色并将它们分配给已经绘制的对象?

转载 作者:太空宇宙 更新时间:2023-11-03 15:22:08 24 4
gpt4 key购买 nike

我的任务是使用 Python 绘制拼凑图案。我需要从用户那里得到宽度和高度,两者都必须大于 3 但小于 10,并且有 4 种颜色,不能相同。我已经为罚款编写了代码。但我需要绘制两种不同类型的图案。一个图案沿着边缘,只有 1 个补丁深,第二个图案填充中心方 block 。现在,我用来绘制它的代码是沿着顶部、底部和每一侧重复第一个补丁,然后在整个中间重复第二个图案。但现在我必须为每个不同的单独补丁分配一种颜色,循环使用给定的颜色,以便它从第一个开始,循环遍历所有颜色,然后再次从第一个开始。

我的问题是,我不知道如何循环显示颜色,因为顺序会逐行不同。有没有一种有效的方法可以用我到目前为止所写的来做到这一点,即。绘制一个单独的补丁并在线条上重复它,如果是这样,最好的方法是什么?

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

最佳答案

您可以使用 itertools.cycle反复检查所有颜色。因此,假设您将所有补丁都放在一个名为 patches 的列表中,您可以执行如下操作

import itertools
colors = ['red','blue','green','yellow']
patches = ['a','b','c','d','e','f','g','h','i','j']

for patch, color in itertools.izip(patches, itertools.cycle(colors)):
# color a patch
print 'Colour', patch, 'as', color

打印出来

Colour a as red
Colour b as blue
Colour c as green
Colour d as yellow
Colour e as red
Colour f as blue
Colour g as green
Colour h as yellow
Colour i as red
Colour j as blue

关于python - 如何循环不同的颜色并将它们分配给已经绘制的对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13442707/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com