- 基础知识
import turtle
turtle.showturtle() # 显示箭头
turtle.write("Celia") # 写字符串
turtle.forward(300) # 前进300像素
turtle.color("red") # 画笔颜色改为red
turtle.left(90) # 箭头左转90度
turtle.forward(300)
turtle.goto(0, 50) # 去坐标(0, 50)
turtle.goto(0, 0)
turtle.penup() # 抬笔,这样路径不会画出来但依旧会走
turtle.goto(0, 300)
turtle.pendown()
turtle.circle(100) # 画圆
turtle.done() # 保持窗口在程序执行完依然在
画奥运五环
import turtle
t = turtle.Pen()
t.width(4)
t.color("blue")
t.circle(50)
t.penup()
t.goto(120,0)
t.pendown()
t.color("black")
t.circle(50)
t.penup()
t.goto(240,0)
t.pendown()
t.color("red")
t.circle(50)
t.penup()
t.goto(60,-50)
t.pendown()
t.color("yellow")
t.circle(50)
t.penup()
t.goto(180,-50)
t.pendown()
t.color("green")
t.circle(50)
turtle.done()
- 画同心圆
import turtle
my_colors = ("red", "green", "yellow", "black")
t = turtle.Pen() # 获得Pen对象画笔
t.width(4) # 宽度
t.speed(0)
for i in range(10):
t.penup() # 将笔抬起来,不要挪动半径时的线
t.goto(0, -i*10) # 起始点坐标
t.pendown()
t.color(my_colors[i % len(my_colors)]) # 将余数值当索引只可能=0,1,2,3
t.circle(15+i*10) # 半径
turtle.done() # 保持窗口在程序执行完依然在
- 画棋盘格(横18条边,竖18条边)
import turtle
t = turtle.Pen() # 获得Pen对象画笔
t.width(1) # 宽度
t.speed(5)
for i in range(18):
t.penup()
t.goto(-170, 170-i*10)
t.pendown()
t.goto(0, 170-i*10)
for i in range(18):
t.penup()
t.goto(-170+i*10, 170)
t.pendown()
t.goto(-170+i*10, 0)
turtle.done()