目录
前言
准备工作
常用代码介绍
实例
需要注意的是... ...
能在Python编程画画真是太好了... ...
该功能需要调用turtle库。没错,是小乌龟。
import turtle
(1)基本代码
代码 | 功能 |
---|---|
turtle.forward(x) | 向画笔方向移动x像素 |
turtle.backward(x) | 向画笔反方向移动x像素 |
turtle.right(x) | 将画笔向右(顺时针)旋转x° |
turtle.left(x) | 将画笔向左(逆时针)旋转x° |
turtle.goto(x,y) | 将画笔移动到(x,y) |
turtle.circle(r) | 画圆; 若r<0:向左画半径为|r|的圆 若r>0:向右画半径为r的圆 |
turtle.pendown() | 落笔 |
turtle.penup() | 抬笔 |
turtle.pensize(x) | 设置笔粗为x |
(2)颜色、填充代码
代码 | 功能 |
---|---|
turtle.pencolor(color_str) | 设置画笔颜色 |
turtle.fillcolor(color_str) | 设置填充颜色 |
turtle.color(color1_str,color2_str) | 同时设置两个参数: 将画笔颜色设置为color1, 将填充颜色设置为color2 |
turtle.begin_fill() | 准备开始填充 |
trtle.end_fill() | 结束填充 |
0.探究坐标方向
前言:被App Inventor的坐标系给耍过。
人家是这样的:
上图截自本人自制的某软件的“介绍”。
代码如下:
import turtle
turtle.penup()
turtle.pensize(3)
turtle.goto(-10,0)
turtle.pendown()
turtle.goto(250,0)
turtle.goto(240,-5)
turtle.goto(250,0)
turtle.goto(240,5)
turtle.penup()
turtle.goto(0,-10)
turtle.pendown()
turtle.goto(0,250)
turtle.goto(5,240)
turtle.goto(0,250)
turtle.goto(-5,240)
turtle.penup()
turtle.goto(0,0)
turtle.done()
结果:
真好。
1.绘制箭头(如例0)
import turtle
turtle.penup()
turtle.pensize(3)
turtle.color("red","yellow")
turtle.goto(50,100)
turtle.pendown()
turtle.goto(200,100)
turtle.goto(190,90)
turtle.goto(200,100)
turtle.goto(190,110)
turtle.done() #End.
结果:
2.绘制脸(你管这个叫“脸”?)
import turtle
turtle.penup()
turtle.pensize(3)
#Left eye
turtle.goto(50,100)
turtle.pendown()
turtle.fillcolor("black")
turtle.begin_fill()
turtle.circle(-30)
turtle.end_fill()
turtle.penup()
turtle.goto(50,100)
turtle.pendown()
turtle.fillcolor("white")
turtle.begin_fill()
turtle.circle(-10)
turtle.end_fill()
turtle.penup()
#Right eye
turtle.goto(100,100)
turtle.pendown()
turtle.fillcolor("black")
turtle.begin_fill()
turtle.circle(30)
turtle.end_fill()
turtle.penup()
turtle.goto(100,100)
turtle.pendown()
turtle.fillcolor("white")
turtle.begin_fill()
turtle.circle(10)
turtle.end_fill()
turtle.penup()
#Mouth
turtle.goto(175,100)
turtle.pendown()
turtle.fillcolor("red")
turtle.begin_fill()
turtle.circle(20)
turtle.end_fill()
turtle.penup()
turtle.done()
结果:
3.绘制三角形
import turtle
turtle.pendown()
turtle.pensize(3)
#Repeat 3 times
for i in range(3):
turtle.forward(250)
turtle.left(120)
turtle.penup()
turtle.done()
结果: