【问题标题】:Drawing piles of rectangles using python turtle使用 python turtle 绘制成堆的矩形
【发布时间】:2021-04-12 21:52:16
【问题描述】:

我该怎么做?

我应该宽高是 50 像素

【问题讨论】:

  • 欢迎来到 Stack Overflow!你试过什么?我们必须查看您的代码才能知道如何修复它。

标签: python geometry turtle-graphics python-turtle


【解决方案1】:

您可以使用turtle.stamp() 方法标记每个单元格,并使用random.choice() 方法从颜色列表中选择随机颜色:

import turtle
from random import choice

def grid(rows, cols, x, y, size=50):
    turtle.shape("square")
    turtle.shapesize(size / 20, size / 20)
    turtle.penup()
    for i in range(rows):
        turtle.setpos(x, y - size * i)
        for _ in range(cols):
            turtle.forward(size)
            turtle.color("black", choice(["white", "grey"]))
            turtle.stamp()

grid(5, 5, -200, 200)

输出:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-17
    • 1970-01-01
    相关资源
    最近更新 更多