turtle画房子代码
时间: 2023-07-03 21:20:36 浏览: 103
以下是使用Python的turtle模块画一个简单的房子的代码:
```python
import turtle
# 创建一个画布和画笔
canvas = turtle.Screen()
pen = turtle.Turtle()
# 画房子的主体
pen.penup()
pen.goto(-100, -100)
pen.pendown()
pen.forward(200)
pen.left(90)
pen.forward(200)
pen.left(90)
pen.forward(200)
pen.left(90)
pen.forward(200)
# 画房顶
pen.penup()
pen.goto(-100, 100)
pen.pendown()
pen.right(45)
pen.forward(141)
pen.right(90)
pen.forward(141)
# 画门
pen.penup()
pen.goto(-50, -100)
pen.pendown()
pen.right(90)
pen.forward(100)
pen.left(90)
pen.forward(50)
pen.left(90)
pen.forward(100)
# 画窗户
pen.penup()
pen.goto(50, 0)
pen.pendown()
pen.right(90)
pen.forward(50)
pen.left(90)
pen.forward(50)
pen.left(90)
pen.forward(50)
pen.left(90)
pen.forward(50)
# 隐藏画笔
pen.hideturtle()
# 显示画布
canvas.mainloop()
```
运行代码后,你将会得到一个类似如下的房子图形:
![turtle-house](https://cdn.jsdelivr.net/gh/YanhuiJessica/figure-bed-typora/img/turtle-house.png)
阅读全文