用科赫曲线Python画一个小房子
时间: 2023-11-16 14:26:18 浏览: 69
好的,下面是代码:
```python
import turtle
def koch_curve(t, order, size):
if order == 0:
t.forward(size)
else:
for angle in [60, -120, 60, 0]:
koch_curve(t, order-1, size/3)
t.left(angle)
def draw_house():
# 初始化画布
window = turtle.Screen()
window.bgcolor("white")
# 初始化画笔
pen = turtle.Turtle()
pen.speed(0)
pen.color("black")
pen.penup()
pen.goto(-200, -150)
pen.pendown()
pen.pensize(2)
# 画正方形
for i in range(4):
pen.forward(200)
pen.left(90)
# 画屋顶
pen.left(45)
koch_curve(pen, 3, 200)
pen.right(90)
koch_curve(pen, 3, 200)
pen.right(45)
# 画门
pen.penup()
pen.goto(-50, -150)
pen.pendown()
pen.right(90)
pen.forward(100)
pen.left(90)
pen.forward(60)
pen.left(90)
pen.forward(40)
pen.left(90)
pen.forward(60)
# 画窗户
pen.penup()
pen.goto(-150, -50)
pen.pendown()
pen.right(90)
pen.forward(60)
pen.left(90)
pen.forward(60)
pen.left(90)
pen.forward(60)
pen.left(90)
pen.forward(60)
# 隐藏画笔
pen.hideturtle()
# 等待关闭窗口
turtle.done()
# 调用函数
draw_house()
```
运行代码,会弹出一个窗口,显示一个小房子,如下图所示:
![小房子](https://cdn.luogu.com.cn/upload/image_hosting/f86a5b6f.png)
阅读全文