用python的turtle在黄色的长方形里画一个简易房子
时间: 2024-02-11 09:09:11 浏览: 68
Python 标准库turtle
好的,以下是Python代码实现:
```python
import turtle
# 创建画布和画笔
screen = turtle.Screen()
pen = turtle.Turtle()
# 设置画笔的颜色和填充颜色
pen.color('black', 'yellow')
# 开始填充长方形
pen.begin_fill()
# 绘制长方形
pen.forward(600)
pen.right(90)
pen.forward(400)
pen.right(90)
pen.forward(600)
pen.right(90)
pen.forward(400)
# 结束填充
pen.end_fill()
# 绘制房子
pen.penup()
pen.goto(150, 0)
pen.pendown()
pen.color('black', 'white')
pen.begin_fill()
pen.forward(300)
pen.right(90)
pen.forward(200)
pen.right(90)
pen.forward(300)
pen.right(90)
pen.forward(200)
pen.end_fill()
# 绘制屋顶
pen.penup()
pen.goto(150, 200)
pen.pendown()
pen.color('black', 'red')
pen.begin_fill()
pen.right(45)
pen.forward(212)
pen.right(90)
pen.forward(212)
pen.end_fill()
pen.right(135)
# 绘制门和窗户
pen.penup()
pen.goto(220, -50)
pen.pendown()
pen.color('black', 'brown')
pen.begin_fill()
pen.forward(60)
pen.right(90)
pen.forward(100)
pen.right(90)
pen.forward(60)
pen.end_fill()
pen.penup()
pen.goto(270, -100)
pen.pendown()
pen.color('black', 'light blue')
pen.begin_fill()
pen.circle(30)
pen.end_fill()
# 隐藏画笔
pen.hideturtle()
# 完成绘制
turtle.done()
```
运行以上代码,你就可以在窗口中看到绘制的黄色长方形和一个简易房子。
阅读全文