Turtle入门一

2021-10-18 10:15:34 浏览数 (1)

1.方形

代码语言:javascript复制
import turtle
t = turtle.Pen()
for x in range(200):
    t.forward(x)#向右移动x
    t.left(90)#向左旋转九十度
    #之后一直这样循环,直到x=199![在这里插入图片描述](https://img-blog.csdnimg.cn/202010311347557.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NjIxMTI2OQ==,size_16,color_FFFFFF,t_70#pic_center)

效果图:

2.画圆形

代码语言:javascript复制
import turtle
t = turtle.Pen()
for x in range(100):
    t.circle(x)#在当前位置绘制了一个半径为x的圆
    t.left(91)#按道理90度,91是为了便于不断变大

3.带上颜色

代码语言:javascript复制
import turtle
t = turtle.Pen()
t.pencolor("red")#笔颜色为红色
for x in range(100):
    t.forward(x)
    t.left(91)

效果图:

4.多种颜色

代码语言:javascript复制
import turtle
t = turtle.Pen()
colors = ["red", "yellow", "blue","green"]
for x in range(100):
    t.pencolor(colors[x%4])#%叫做模除操作符,依次选取四种颜色,记住这种模型就好了
    t.forward(x)
    t.left(91)

效果图:

5.加上背景图

代码语言:javascript复制
import turtle
t = turtle.Pen()
turtle.bgcolor("black")#画图背景为黑色
colors = ["red","yellow","blue","green"]
for x in range(100):
    t.pencolor(colors[x%4])
    t.circle(x)
    t.left(91)

6.控制边数

代码语言:javascript复制
import turtle
t = turtle.Pen()#拿起笔
turtle.speed(9)
turtle.bgcolor("black")#背景颜色
# You can choose between 2 and 6 sides for some cool shapes!
sides = 6#边数
colors = ["red","yellow","blue", "orange","green","purple"]#颜色
for x in range(360):
    t.pencolor(colors[x%sides])#有多少边就除以几,每一边颜色一种
    t.forward(x * 3/sides   x)#默认方向为右,每一次画完后
    t.left(360/sides   1)#这个1加不加影响不大,实际来说就应该360/6=60度旋转, 1是为帮助图不断变大
    t.width(x*sides/200)#随着图越来越大,笔的宽度越来越大,width用来修改笔宽度

效果图:

0 人点赞