python画小房子代码
时间: 2023-07-02 18:09:51 浏览: 116
以下是使用turtle库绘制小房子的Python代码:
```python
import turtle
# 绘制房屋墙壁
def draw_wall():
turtle.begin_fill()
turtle.color("orange")
for i in range(4):
turtle.forward(100)
turtle.left(90)
turtle.end_fill()
# 绘制屋顶
def draw_roof():
turtle.begin_fill()
turtle.color("red")
turtle.right(45)
turtle.forward(70.71)
turtle.right(90)
turtle.forward(70.71)
turtle.end_fill()
# 绘制门
def draw_door():
turtle.penup()
turtle.goto(30, -50)
turtle.pendown()
turtle.begin_fill()
turtle.color("brown")
for i in range(2):
turtle.forward(40)
turtle.left(90)
turtle.forward(60)
turtle.left(90)
turtle.end_fill()
# 绘制窗户
def draw_window():
turtle.penup()
turtle.goto(-40, 0)
turtle.pendown()
turtle.begin_fill()
turtle.color("white")
for i in range(4):
turtle.forward(30)
turtle.left(90)
turtle.end_fill()
# 绘制小房子
def draw_house():
draw_wall()
draw_roof()
draw_door()
draw_window()
# 设置画布和海龟初始状态
turtle.setup(400, 400)
turtle.penup()
turtle.goto(-50, 50)
turtle.pendown()
# 绘制小房子
draw_house()
# 隐藏海龟画笔
turtle.hideturtle()
# 点击任意位置退出程序
turtle.exitonclick()
```
运行上述代码,即可在turtle画布上看到绘制的小房子。
阅读全文