python turtle画简易房子
时间: 2023-06-11 16:04:40 浏览: 101
简单python-画房子
5星 · 资源好评率100%
可以尝试以下代码:
```python
import turtle
# 绘制正方形
def square(length):
for i in range(4):
turtle.forward(length)
turtle.left(90)
# 绘制三角形
def triangle(length):
for i in range(3):
turtle.forward(length)
turtle.left(120)
# 绘制矩形
def rectangle(length, width):
for i in range(2):
turtle.forward(length)
turtle.left(90)
turtle.forward(width)
turtle.left(90)
# 绘制房子
def house(length):
square(length)
turtle.penup()
turtle.right(90)
turtle.forward(length)
turtle.left(90)
turtle.pendown()
triangle(length)
turtle.penup()
turtle.left(90)
turtle.forward(length / 2)
turtle.right(90)
turtle.pendown()
rectangle(length / 2, length)
# 设置画布和画笔
turtle.setup(600, 600)
turtle.penup()
turtle.goto(-150, -150)
turtle.pendown()
# 绘制房子
house(200)
# 隐藏画笔
turtle.hideturtle()
# 保持画面
turtle.done()
```
运行代码可以得到一个简易房子的画面。
阅读全文