用turtle画一个故宫
时间: 2023-10-25 15:09:55 浏览: 51
turtle作图作品
好的,以下是使用Python中的turtle库来画一个简单的故宫的代码:
```python
import turtle
# 设置画笔
turtle.setup(800, 600)
turtle.speed(5)
turtle.pensize(5)
turtle.penup()
# 画建筑物
turtle.goto(-200, -150)
turtle.pendown()
turtle.color("red")
turtle.begin_fill()
turtle.forward(400)
turtle.left(90)
turtle.forward(300)
turtle.left(90)
turtle.forward(400)
turtle.left(90)
turtle.forward(300)
turtle.end_fill()
# 画屋顶
turtle.penup()
turtle.goto(-200, 150)
turtle.pendown()
turtle.color("yellow")
turtle.begin_fill()
turtle.goto(0, 250)
turtle.goto(200, 150)
turtle.goto(-200, 150)
turtle.end_fill()
# 画门
turtle.penup()
turtle.goto(-100, -150)
turtle.pendown()
turtle.color("green")
turtle.begin_fill()
turtle.forward(100)
turtle.right(90)
turtle.forward(150)
turtle.right(90)
turtle.forward(100)
turtle.right(90)
turtle.forward(150)
turtle.end_fill()
# 画门楼
turtle.penup()
turtle.goto(-150, 0)
turtle.pendown()
turtle.color("blue")
turtle.begin_fill()
turtle.forward(50)
turtle.right(90)
turtle.forward(100)
turtle.right(90)
turtle.forward(50)
turtle.right(90)
turtle.forward(100)
turtle.end_fill()
# 画旗帜
turtle.penup()
turtle.goto(0, 250)
turtle.right(90)
turtle.forward(50)
turtle.right(90)
turtle.forward(100)
turtle.right(180)
turtle.pendown()
turtle.color("black", "yellow")
turtle.begin_fill()
turtle.circle(25)
turtle.end_fill()
# 隐藏画笔
turtle.hideturtle()
# 等待关闭窗口
turtle.done()
```
运行代码后,会弹出一个窗口,显示出我们绘制的故宫图案。你可以尝试调整代码中的参数来画出更加复杂的故宫图案。
阅读全文