用python画一只乌龟
时间: 2023-11-09 19:07:14 浏览: 128
好的,下面是用Python的turtle库画一只乌龟的代码:
```python
import turtle
# 创建画布
canvas = turtle.Screen()
canvas.bgcolor("white")
# 创建画笔
pen = turtle.Turtle()
pen.speed(2)
# 画出乌龟头部
pen.penup()
pen.goto(-50, 0)
pen.pendown()
pen.circle(50)
# 画出乌龟身体
pen.penup()
pen.goto(50, 0)
pen.pendown()
pen.circle(50)
# 画出乌龟四条腿
pen.penup()
pen.goto(-25, -50)
pen.pendown()
pen.forward(50)
pen.penup()
pen.goto(-25, -50)
pen.pendown()
pen.right(90)
pen.forward(25)
pen.penup()
pen.goto(25, -50)
pen.pendown()
pen.left(180)
pen.forward(50)
pen.penup()
pen.goto(25, -50)
pen.pendown()
pen.right(90)
pen.forward(25)
# 画出乌龟尾巴
pen.penup()
pen.goto(75, 10)
pen.pendown()
pen.right(120)
pen.forward(50)
# 隐藏画笔
pen.hideturtle()
# 关闭画布
canvas.exitonclick()
```
运行代码后,会弹出一个窗口,显示出画出的乌龟。你可以尝试修改代码,让乌龟的颜色、大小等属性发生变化。
阅读全文