turtle画图代码大全
时间: 2023-09-05 09:13:51 浏览: 74
以下是turtle模块的常用绘图代码:
1. 画一个正方形:
```
import turtle
pen = turtle.Turtle()
for i in range(4):
pen.forward(100)
pen.right(90)
turtle.done()
```
2. 画一个圆形:
```
import turtle
pen = turtle.Turtle()
pen.circle(50)
turtle.done()
```
3. 画一个三角形:
```
import turtle
pen = turtle.Turtle()
for i in range(3):
pen.forward(100)
pen.left(120)
turtle.done()
```
4. 画一个五角星:
```
import turtle
pen = turtle.Turtle()
for i in range(5):
pen.forward(100)
pen.right(144)
turtle.done()
```
5. 画一个正多边形:
```
import turtle
pen = turtle.Turtle()
num_sides = 6
side_length = 70
angle = 360.0 / num_sides
for i in range(num_sides):
pen.forward(side_length)
pen.right(angle)
turtle.done()
```
6. 画一个螺旋线:
```
import turtle
pen = turtle.Turtle()
for i in range(100):
pen.forward(i)
pen.right(20)
turtle.done()
```
7. 画一个彩色螺旋线:
```
import turtle
import random
pen = turtle.Turtle()
colors = ["red", "orange", "yellow", "green", "blue", "purple"]
for i in range(100):
pen.color(random.choice(colors))
pen.forward(i)
pen.right(20)
turtle.done()
```
希望这些代码可以帮助你入门turtle模块的绘图功能。
阅读全文