python--turtle画图
Turtle 语法 |
解释 |
例子 |
turtle.bgcolor("red ") |
画布背景颜色 |
|
turtle.setup(width=800,height=600,startx=None,starty=None) |
当宽和高为整数时, 表示像素; 当宽和高为小数时, 表示占据电脑屏幕的比例, startx, starty表示矩形窗口左上角顶点的位置, 如果为空,则窗口位于屏幕中心 |
|
turtle.up() |
拿起笔 |
联用形式 turtle.up() turtle.goto(x1,y1) |
turtle.goto(x1,y1)
|
到某一点 |
|
turtle.down() |
放下笔
|
|
turtle.begin_fill() |
开始填充 |
turtle.begin_fill() |
turtle.fillcolor("yellow") |
填充颜色 |
|
for i in range(5): |
所填充的形状绘图 (本例绘图效果为:五角星) |
|
turtle.end_fill() |
填充完毕 |
|
turtle.color('yellow') |
画笔颜色 |
|
turtle.speed(speed) |
画笔速度 “fastest”: 0“fast”: 10“normal”: 6“slow”: 3“slowest”: 1 |
|
turtle.forward(distance) |
向前方画直线长度200 |
turtle.forward(200) |
turtle.right(degree) |
向右侧旋转144度 |
turtle.right(144) |
turtle.setheading(degree) |
朝向角度30 (一般是下笔的朝向) |
turtle.setheading(30) |
turtle.done() |
放在结尾,能持续显示画布 |
|
circle(50,180) 50为半径长度 180为角度 180半圆(图型在第一象限,方向逆时针半圆) circle(50,-180) 50为半径长度 180为角度 180半圆(图型在第二象限,方向顺时针半圆) circle(-50,180) 50为半径长度 180为角度 180半圆(图型在第四象限,方向顺时针半圆) circle(-50,-180) 50为半径长度 180为角度 180半圆(图型在第三象限,方向逆时针半圆) |
参数分别为半径、弧度和画圆弧线段的数量
|
|
turtle.pencolor(color) turtle.pensize(distance) |
画笔的颜色与粗细 |
|
import turtle as t
t.speed(0)
t.pensize(8)
t.hideturtle()
t.screensize(500,500,bg='white')
t.penup()
t.goto(0, 0)
t.pensize(4)
t.pendown()
t.color('black', 'white')
t.begin_fill()
# 画椭圆
a = 0.4
for i in range(120):
if 0 <= i < 30 or 60 <= i < 90:
a = a + 0.08
t.lt(3) # 向左转3度
t.fd(a) # 向前走a的步长
else:
a = a - 0.08
t.lt(3)
t.fd(a)
t.end_fill()
# 画弧
import turtle as t
t.pendown()
t.setheading(90)
for j in range(60): # 重复执行60次
t.forward(3)
t.left(3)
t.penup()
t.penup()
t.goto(20,40)
t.pendown()
#画弧
t.circle(-20,180)
#画多边形
t.circle(40,360,5)
t.done()
# 画波浪线
t.circle(40, 80)
t.circle(-40, 80)
t.circle(40, 80)