python海龟画海
时间: 2023-09-12 13:11:27 浏览: 52
当使用Python中的turtle模块绘制海洋场景时,可以使用以下代码:
```python
import turtle
# 设置画布和画笔
screen = turtle.Screen()
screen.setup(800, 600)
screen.bgcolor("lightblue")
pen = turtle.Turtle()
pen.speed(2)
pen.hideturtle()
# 绘制海洋
pen.penup()
pen.goto(-400, -300)
pen.pendown()
pen.fillcolor("deepskyblue")
pen.begin_fill()
pen.goto(400, -300)
pen.goto(400, 0)
pen.goto(-400, 0)
pen.goto(-400, -300)
pen.end_fill()
# 绘制海龟
turtle.shape("turtle")
turtle.color("green")
turtle.penup()
turtle.goto(-100, -200)
turtle.pendown()
turtle.setheading(90)
# 绘制海龟的身体
turtle.fillcolor("darkgreen")
turtle.begin_fill()
turtle.circle(50)
turtle.end_fill()
# 绘制海龟的头部
turtle.penup()
turtle.goto(-100, -150)
turtle.pendown()
turtle.setheading(0)
turtle.fillcolor("green")
turtle.begin_fill()
turtle.circle(30)
turtle.end_fill()
# 绘制海龟的眼睛
turtle.penup()
turtle.goto(-115, -130)
turtle.pendown()
turtle.fillcolor("white")
turtle.begin_fill()
turtle.circle(7)
turtle.end_fill()
# 绘制海龟的眼珠
turtle.penup()
turtle.goto(-113, -125)
turtle.pendown()
turtle.fillcolor("black")
turtle.begin_fill()
turtle.circle(3)
turtle.end_fill()
# 绘制海龟的脚
turtle.penup()
turtle.goto(-120, -180)
turtle.pendown()
turtle.fillcolor("green")
turtle.begin_fill()
turtle.setheading(-45)
turtle.forward(40)
turtle.setheading(45)
turtle.forward(40)
turtle.setheading(135)
turtle.forward(40)
turtle.setheading(-135)
turtle.forward(40)
turtle.end_fill()
# 绘制海草
pen.penup()
pen.goto(-300, -250)
pen.pendown()
pen.pensize(3)
pen.color("darkgreen")
for _ in range(10):
pen.forward(50)
pen.backward(50)
pen.right(36)
# 隐藏海龟
turtle.hideturtle()
# 点击画布退出程序
screen.exitonclick()
```
这个代码使用了Python中的`turtle`模块绘制了一个简单的海洋场景,包括海水、海龟、海草等元素。你可以运行这段代码,然后通过点击画布来退出程序。