用Python程序画一栋房子
时间: 2024-03-06 14:45:46 浏览: 129
简单python-画房子
5星 · 资源好评率100%
当然可以!以下是用Python程序画一栋简单房子的示例代码:
```python
import turtle
# 创建画布和画笔
window = 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.left(90)
# 绘制房顶
pen.penup()
pen.goto(-100, 100)
pen.pendown()
pen.goto(0, 200)
pen.goto(100, 100)
# 绘制门
pen.penup()
pen.goto(-30, -100)
pen.pendown()
pen.right(90)
pen.forward(80)
pen.left(90)
pen.forward(60)
pen.left(90)
pen.forward(80)
# 绘制窗户
pen.penup()
pen.goto(50, 0)
pen.pendown()
pen.forward(40)
pen.left(90)
pen.forward(40)
pen.left(90)
pen.forward(40)
pen.left(90)
pen.forward(40)
# 结束绘制
window.exitonclick()
```
这段代码使用了Python的turtle库来绘制图形。通过控制画笔的移动和旋转,可以绘制出房子的主体、房顶、门和窗户等元素。
阅读全文