猿问

如何为不同区域添加颜色

所以我使用两种不同的设计创建了一个循环模式。现在的问题是制作颜色,所以我必须要求用户选择 3 种不同的颜色,并使用它们来使设计的某些区域具有该颜色。例如,我的意思是,如果我要选择 5x5 补丁布局,那么横贯和下方的前 2 个图案框应该是一种颜色(因此是四个框),依此类推。

编辑:清除混乱。

  1. 所以如果你看看 500x500 的那一张。当它询问您想要的补丁大小时输入“5”。

  2. 你应该看到两种不同的图案设计。

  3. 我想要的是 x = 0 到 x = 200 和 y = 0 到 y = 200 是蓝色。这意味着该区域中的所有东西(按区域,我的意思是那里的所有图案设计)都应该是蓝色。x = 300 到 x = 500 和 y = 300 到 y = 500 应使用相同的颜色。

  4. x = 300 到 x = 500 和 y = 0 到 y = 200 应该是红色,而 x = 0 到 x = 200 和 y = 300 到 y = 500 也应该是红色。

  5. 剩下的就是中间的“+”部分,它应该有第三种颜色,绿色。

  6. https://prntscr.com/m48xxd这就是我所说的颜色。所以左上角和右下角是相同的颜色。右上角和左下角是相同的颜色,“+”是另一种颜色。

7. https://prntscr.com/m48xzm这里是 7x7 版本

http://img3.mukewang.com/615c080b0001e0a201570151.jpg

我现在只是添加了颜色,以便于查看。

我已经尝试过执行 'getX' 和 'getY',但我似乎做对了。



猛跑小猪
浏览 256回答 2
2回答

慕尼黑5688855

关键是将所有东西参数化为合适大小的立方体,然后将填充代码变成可以调用来填充这些立方体的子程序。下面是沿着这些行重新编写的代码,可以将任何奇数 3 或更大的数作为输入处理:from graphics import *UNIT = 100def patchWork(win, size):    blocks = size // 2    def hatch_box(x, y):        for n in range(0, UNIT, UNIT//5):            line = Line(Point(n + x * UNIT, y * UNIT), Point((x + 1) * UNIT, UNIT - n + y * UNIT))            line.draw(win)        for n in range(UNIT, 0, -UNIT//5):            line = Line(Point(n + x * UNIT, (y + 1) * UNIT), Point(x * UNIT, UNIT - n + y * UNIT))            line.draw(win)        for n in range(UNIT, 0, -UNIT//5):            line = Line(Point((x + 1) * UNIT, n + y * UNIT), Point(n + x * UNIT, (y + 1) * UNIT))            line.setFill('red')            line.draw(win)        for n in range(0, UNIT, UNIT//5):            line = Line(Point(x * UNIT, UNIT - n + y * UNIT), Point(UNIT - n + x * UNIT, y * UNIT))            line.setFill('red')            line.draw(win)    for y in range(blocks):        for x in range(blocks):            hatch_box(x, y)    for y in range(blocks + 1, 2 * blocks + 1):        for x in range(blocks + 1, 2 * blocks + 1):            hatch_box(x, y)    def draw_circles(x, y):        for o in range(0, UNIT, UNIT // 10):            for c in range(0, UNIT, UNIT // 10):                circle = Circle(Point(c + UNIT // 20 + x * UNIT, o + UNIT // 20 + y * UNIT), UNIT // 20)                circle.setFill('blue')                circle.draw(win)    for y in range(blocks):        for x in range(blocks + 1, 2 * blocks + 1):            draw_circles(x, y)            draw_circles(y, x)    def draw_cube(x, y):        cube = Rectangle(Point(x * UNIT, y * UNIT), Point((x + 1) * UNIT, (y + 1) * UNIT))        cube.setFill('yellow')        cube.draw(win)    x = blocks    for y in range(0, 2 * blocks + 1):        draw_cube(x, y)        draw_cube(y, x)def layout(size):    win = GraphWin('Patchwork', size * UNIT, size * UNIT)    for i in range(0, size * UNIT, UNIT):        Line(Point(i, 0), Point(i, size * UNIT)).draw(win)        Line(Point(0, i), Point(size * UNIT, i)).draw(win)    return windef main():    patchWorkSize = int(input("What patchwork size would you like? "))    if patchWorkSize % 2 == 1 and patchWorkSize > 2:        win = layout(patchWorkSize)        patchWork(win, patchWorkSize)        win.getMouse() # pause for click in window        win.close()    else:        print('Only odd sizes 3 or greater are available')main()你应该能够UNIT在合理范围内改变。

蝴蝶不菲

我不会为您编写所有内容,但下面的代码显示了如何根据每个区域在拼凑网格 (i和j) 中的位置为每个区域选择颜色。颜色在 中确定color_func()。希望这足以让您弄清楚如何应用所示的相对简单的编码模式来绘制所需的图形。提示:根据(或相对于)这些相同的位置值计算每个图形元素的坐标(即不要为每个可能的拼凑尺寸编写单独的函数)。这段代码对我来说似乎仍然过于重复,可能可以做得更简洁,但我也会把它留给你...... ;¬)def main():#&nbsp; &nbsp; size = int(input("What patchwork size would you like?"))#&nbsp; &nbsp; Hardcoded for testing.##&nbsp; &nbsp; if size % 2 == 0:#&nbsp; &nbsp; &nbsp; &nbsp; raise RuntimeError('patchwork size must be odd')&nbsp; &nbsp; patch_work(5)&nbsp; &nbsp; print()&nbsp; &nbsp; patch_work(7)def patch_work(n):&nbsp; &nbsp; colors = '0', '1', '2'&nbsp; &nbsp; mid = n // 2&nbsp; &nbsp; # Call color_func() for every possible position in patchwork and&nbsp; &nbsp; # prints the results in rows.&nbsp; &nbsp; for i in range(n):&nbsp; &nbsp; &nbsp; &nbsp; row = []&nbsp; &nbsp; &nbsp; &nbsp; for j in range(n):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; row.append(color_func(n, i, j, colors))&nbsp; &nbsp; &nbsp; &nbsp; print(''.join(row))def color_func(n, i, j, colors):&nbsp; &nbsp; mid = n // 2&nbsp; &nbsp; if i == mid:&nbsp; &nbsp; &nbsp; &nbsp; index = 2&nbsp; &nbsp; elif i < mid:&nbsp; &nbsp; &nbsp; &nbsp; if j < mid:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; index = 0&nbsp; &nbsp; &nbsp; &nbsp; elif j == mid:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; index = 2&nbsp; &nbsp; &nbsp; &nbsp; elif j > mid:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; index = 1&nbsp; &nbsp; elif i > mid:&nbsp; &nbsp; &nbsp; &nbsp; if j < mid:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; index = 1&nbsp; &nbsp; &nbsp; &nbsp; elif j == mid:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; index = 2&nbsp; &nbsp; &nbsp; &nbsp; elif j > mid:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; index = 0&nbsp; &nbsp; return colors[index]if __name__ == '__main__':&nbsp; &nbsp; main()输出:00211002112222211200112000002111000211100021112222222111200011120001112000
随时随地看视频慕课网APP

相关分类

Python
我要回答