python画四个小正方形并填充颜色
时间: 2023-07-29 20:10:24 浏览: 368
正方形四色填充
你可以使用Python的turtle库来画四个小正方形并填充颜色,具体代码如下:
```python
import turtle
# 设置画笔大小和颜色
turtle.pensize(5)
turtle.pencolor('blue')
# 画第一个小正方形
turtle.begin_fill()
turtle.forward(50)
turtle.left(90)
turtle.forward(50)
turtle.left(90)
turtle.forward(50)
turtle.left(90)
turtle.forward(50)
turtle.end_fill()
# 画第二个小正方形
turtle.penup()
turtle.goto(70, 0)
turtle.pendown()
turtle.begin_fill()
turtle.forward(50)
turtle.left(90)
turtle.forward(50)
turtle.left(90)
turtle.forward(50)
turtle.left(90)
turtle.forward(50)
turtle.end_fill()
# 画第三个小正方形
turtle.penup()
turtle.goto(0, 70)
turtle.pendown()
turtle.begin_fill()
turtle.forward(50)
turtle.left(90)
turtle.forward(50)
turtle.left(90)
turtle.forward(50)
turtle.left(90)
turtle.forward(50)
turtle.end_fill()
# 画第四个小正方形
turtle.penup()
turtle.goto(70, 70)
turtle.pendown()
turtle.begin_fill()
turtle.forward(50)
turtle.left(90)
turtle.forward(50)
turtle.left(90)
turtle.forward(50)
turtle.left(90)
turtle.forward(50)
turtle.end_fill()
# 隐藏画笔
turtle.hideturtle()
# 等待用户关闭窗口
turtle.done()
```
上述代码中,我们使用了turtle库的各种函数来绘制四个小正方形,并使用begin_fill()和end_fill()函数来填充颜色。你可以根据需要修改画笔大小、颜色和填充颜色等参数。
阅读全文