Python用turtle画房子
时间: 2023-08-21 10:33:56 浏览: 145
好的,下面是Python用turtle画房子的代码:
```python
import turtle
# 设置画笔
pen = turtle.Turtle()
# 画墙壁
pen.pencolor("black")
pen.fillcolor("red")
pen.begin_fill()
for i in range(4):
pen.forward(200)
pen.left(90)
pen.end_fill()
# 画屋顶
pen.pencolor("black")
pen.fillcolor("gray")
pen.begin_fill()
pen.left(45)
pen.forward(141.4)
pen.right(90)
pen.forward(141.4)
pen.right(45)
pen.forward(200)
pen.left(135)
pen.end_fill()
# 画门
pen.pencolor("black")
pen.fillcolor("brown")
pen.begin_fill()
pen.left(45)
pen.forward(70.7)
pen.right(90)
pen.forward(50)
pen.right(90)
pen.forward(70.7)
pen.end_fill()
# 画窗户
pen.pencolor("black")
pen.fillcolor("white")
pen.begin_fill()
pen.penup()
pen.goto(70, 100)
pen.pendown()
for i in range(4):
pen.forward(60)
pen.right(90)
pen.end_fill()
# 完成画图
turtle.done()
```
运行上面的代码,就可以在窗口中看到一个用turtle画的房子了。