使用乌龟在python中用颜色填充矩形 [英] Filling rectangles with colors in python using turtle

查看:44
本文介绍了使用乌龟在python中用颜色填充矩形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何绘制一个矩形,其中:最小的高度为 5,每个连续的矩形都加上第一个矩形的高度,即 5、10、15…….每个矩形的宽度为200.线条颜色为蓝色,填充颜色,从上到下分别为grey0、grey14、grey28、....我怎样才能做到这一点并确保图片适合屏幕?(每个新矩形都低于前一个)

How can I draw a rectangle where: the smallest is 5 in height and each consecutive rectangle adds the first rectangles height, i.e. 5, 10, 15, …. The width of each rectangle is 200. The color of the lines are blue and the fill color, from top to bottom is grey0, grey14, grey28, …. How can I do this and make sure the picture fits onto the screen? (Each new rectangle goes below the previous one)

这是我目前所拥有的,但我不知道如何填满它:

This is what I have so far, but I don't know how I can fill it:

import turtle

def rectangle(t, l, w):
    for i in range(2):
                t.right(90)
                t.forward(l)
                t.right(90)
                t.forward(w)

me = turtle.Turtle()
me.color('blue')
me.pensize(2)
me.penup()
l = 2.5
w = 250
x = 50
y = 150

for i in range(9):
    rectangle(me, l, w)
    l = l*2
    w = w
    x = x
    y = y
    me.setposition(x,y)
    me.pendown()

推荐答案

def filled_rectangle(t, l, w):
    t.begin_fill()
    for i in range(2):
            t.right(90)
            t.forward(l)
            t.right(90)
            t.forward(w)
    t.end_fill()

这篇关于使用乌龟在python中用颜色填充矩形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆