在Python中使用turtle绘图绘制一个房屋图
时间: 2024-02-06 19:08:53 浏览: 74
好的,以下是一个绘制房屋图的Python代码:
```python
import turtle
# 绘制房屋主体
def draw_house():
turtle.penup()
turtle.goto(-100, -100)
turtle.pendown()
turtle.forward(200) # 底部长度
turtle.left(90)
turtle.forward(150) # 左侧长度
turtle.left(90)
turtle.forward(200) # 顶部长度
turtle.left(90)
turtle.forward(150) # 右侧长度
turtle.left(90)
turtle.penup()
# 绘制屋顶
def draw_roof():
turtle.goto(-100, 50)
turtle.pendown()
turtle.left(45)
turtle.forward(141) # 屋顶斜边长度
turtle.right(90)
turtle.forward(141) # 屋顶斜边长度
turtle.left(45)
turtle.penup()
# 绘制门
def draw_door():
turtle.goto(-40, -100)
turtle.pendown()
turtle.color('brown')
turtle.forward(40)
turtle.left(90)
turtle.forward(80)
turtle.left(90)
turtle.forward(40)
turtle.left(90)
turtle.forward(80)
turtle.left(90)
turtle.color('black')
turtle.penup()
# 绘制窗户
def draw_window():
turtle.goto(0, 0)
turtle.pendown()
turtle.color('brown')
turtle.forward(30)
turtle.right(90)
turtle.forward(30)
turtle.right(90)
turtle.forward(30)
turtle.right(90)
turtle.forward(30)
turtle.right(90)
turtle.penup()
# 绘制花园
def draw_garden():
turtle.goto(100, -100)
turtle.pendown()
turtle.color('green')
turtle.begin_fill()
turtle.circle(50)
turtle.end_fill()
turtle.penup()
# 主程序
def main():
turtle.speed(5)
draw_house()
draw_roof()
draw_door()
draw_window()
draw_garden()
turtle.hideturtle()
turtle.done()
if __name__ == '__main__':
main()
```
运行以上代码后,你应该会看到一个使用turtle绘制的房屋图。你也可以根据自己的需求和想象,修改代码中的参数和绘制步骤,绘制出自己想要的房屋图。
阅读全文