python用海龟画一张山水画
时间: 2023-05-23 09:06:04 浏览: 190
A:可以使用Python标准库中的turtle模块来画一张山水画,代码如下:
```
import turtle
# 设置画布和画笔
screen = turtle.Screen()
pen = turtle.Turtle()
# 画天空
screen.bgcolor("#00BFFF") # 天空颜色为淡蓝色
pen.penup()
pen.goto(-300, 250) # 画笔移动到左上角
pen.pendown()
pen.begin_fill()
pen.color("#FFFFFF") # 白色
for i in range(2):
pen.forward(600)
pen.right(90)
pen.forward(150)
pen.right(90)
pen.end_fill()
# 画山
pen.penup()
pen.goto(-200, 60)
pen.pendown()
pen.begin_fill()
pen.color("#90EE90") # 山的颜色为淡绿色
pen.right(45)
pen.forward(300)
pen.right(90)
pen.forward(300)
pen.right(135)
pen.forward(500)
pen.end_fill()
# 画树
pen.penup()
pen.goto(180, -100)
pen.pendown()
pen.color("#8B4513") # 树干颜色为棕色
pen.pensize(20) # 树干宽度为20
pen.right(90)
pen.forward(150)
pen.color("#228B22") # 树叶颜色为深绿色
pen.pensize(1) # 树叶宽度为1
pen.begin_fill()
pen.circle(100)
pen.end_fill()
screen.mainloop() # 等待画布关闭
```
运行后,就可以得到一张简单的山水画了。效果如下:
![avatar](https://img-blog.csdn.net/20180723095530621?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L25zemRhbmtlcm42NjY=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/q/85/img.png)
阅读全文