python tirtle 画一幅乡村图
时间: 2023-07-06 16:27:10 浏览: 94
好的,这里给你提供一个绘制乡村风光的Python Turtle代码示例,你可以根据需要进行修改:
```python
import turtle
# 创建窗口和画布
window = turtle.Screen()
canvas = turtle.Turtle()
# 绘制天空
canvas.penup()
canvas.goto(-400, 200)
canvas.pendown()
canvas.begin_fill()
canvas.fillcolor('#87CEEB')
canvas.forward(800)
canvas.right(90)
canvas.forward(400)
canvas.right(90)
canvas.forward(800)
canvas.right(90)
canvas.forward(400)
canvas.end_fill()
# 绘制太阳
canvas.penup()
canvas.goto(-200, 150)
canvas.pendown()
canvas.begin_fill()
canvas.fillcolor('yellow')
canvas.circle(50)
canvas.end_fill()
# 绘制草地
canvas.penup()
canvas.goto(-400, -200)
canvas.pendown()
canvas.begin_fill()
canvas.fillcolor('green')
canvas.forward(800)
canvas.right(90)
canvas.forward(200)
canvas.right(90)
canvas.forward(800)
canvas.right(90)
canvas.forward(200)
canvas.end_fill()
# 绘制房子
canvas.penup()
canvas.goto(-100, -200)
canvas.pendown()
canvas.begin_fill()
canvas.fillcolor('brown')
canvas.forward(200)
canvas.right(90)
canvas.forward(100)
canvas.right(90)
canvas.forward(200)
canvas.right(90)
canvas.forward(100)
canvas.end_fill()
canvas.penup()
canvas.goto(-100, -100)
canvas.pendown()
canvas.begin_fill()
canvas.fillcolor('red')
canvas.forward(200)
canvas.right(120)
canvas.forward(200)
canvas.right(120)
canvas.forward(200)
canvas.end_fill()
# 绘制树
canvas.penup()
canvas.goto(200, -200)
canvas.pendown()
canvas.begin_fill()
canvas.fillcolor('brown')
canvas.forward(20)
canvas.right(90)
canvas.forward(50)
canvas.right(90)
canvas.forward(20)
canvas.right(90)
canvas.forward(50)
canvas.end_fill()
canvas.penup()
canvas.goto(180, -150)
canvas.pendown()
canvas.begin_fill()
canvas.fillcolor('green')
canvas.circle(30)
canvas.end_fill()
# 绘制云
canvas.penup()
canvas.goto(-250, 100)
canvas.pendown()
canvas.begin_fill()
canvas.fillcolor('white')
canvas.circle(30)
canvas.penup()
canvas.goto(-220, 100)
canvas.pendown()
canvas.circle(30)
canvas.penup()
canvas.goto(-190, 100)
canvas.pendown()
canvas.circle(30)
canvas.end_fill()
# 隐藏画笔
canvas.hideturtle()
# 保持窗口一直显示
window.mainloop()
```
这段代码绘制了一个乡村风光,包括天空、太阳、草地、房子、树和云。你可以在代码中看到调用了不同的方法来实现绘制不同的形状和填充颜色。
阅读全文