海龟绘图体系总结(2018年12月12号)

海龟绘图体系是Python中的一个内置的模块(turtle),提供一些比较基础的绘图方法
电脑屏幕的左上角坐标为(0,0)(startx,starty)
对于海龟有两个重要的参数:海龟的位置,海龟的朝向
1.1 运动控制(turtle motion)
把坐标点当做是有一个海龟在(分前后左右)

1.1.1
turtle.goto(x,y) #画笔定位到坐标(x,y)
例如:

import turtle
turtle.goto(12,12)
turtle.goto(120,120)
turtle.done()

就是从坐标(12,12)到坐标(120,120)的一条直线

1.1.2
turtle.forward(distance)
或者turtle.fd(distance)#向正前方运动distance长度的距离
(注意是正前方,不是右方)

1.1.3
turtle.backward(distance)
向负方向运动distance长的距离(注意是后退,不是转身走,区别在于海龟的朝向)

1.1.4
turtle.right(angle)
向右偏angle度
turtle.left(angle)
想做偏转angle度

1.1.5
turtle.home()
回到原点

1.1.6
turtle.circle(radius,extent = None,steps = None)
画圆形radius为半径,extent为圆的角度

1.1.7
turtle.speed(speed)
以speed为速度运动

1.1.8
turtle.seth(angle)
turtle.setheading(angle)
改变行进的方向,不行进

1.1.9
turtle.colormode(mode )
mode 为RGB(小数值,整数值)

扫描二维码关注公众号,回复: 4560958 查看本文章

综上的例子:画一个圆和正方形

import turtle
turtle.speed(5)
turtle.goto(0,0)
for i in range(4):
	turtle.forward(100)
	turtle.right(90)
turtle.home()
turtle.circle(50,270)
turtle.done()

在这里插入图片描述
1.2画笔控制(Pen control)

1.2.1
turtle.pendown()
落笔,在此状态下会画出运动的轨迹
turtle.penup()
起笔(将海龟抬起来),在此状态下不会画出运动的轨迹

1.2.2
turtle.pensize(width = None)
画笔的粗细
turtle.pencolor(*args)
画笔颜色
turtle.fillcolor(*args)
填充颜色
turtle.begin_fill()
开始填充
turtle.end_fill()
结束填充
turtle.write(arg,move = False,align = ‘left’,font = (“Arial”,8,“normal”))

import turtle
turtle.pencolor('red')
turtle.pendown()
turtle.fillcolor("blue")
turtle.begin_fill()
for i in range(4):
	turtle.fd(200)
	turtle.right(90)

turtle.end_fill()
turtle.penup()
turtle.goto(100,-100)
turtle.write('shao 编程实验室')
turtle.done()

在这里插入图片描述

1.3 视窗控制(window control)

turtle.bgcolor(‘red’)
设置背景颜色
turtle.bgpic(picname = None)
背景图片填充

实例:
用Python画一条蟒蛇

#PythonDraw.py
import turtle 
turtle.setup(200,200,0,0,)#前两项是画布的大小,后两项是画图的起始位置
turtle.penup()#海龟飞起来
turtle.fd(-250)#倒着走250个像素
turtle.pendown()#海龟落下
turtle.pensize(25)
turtle.pencolor("green")
turtle.seth(-40)
for i in range(4):
	turtle.circle(40,80)
	turtle.circle(-40,80)
turtle.circle(40,40)
turtle.fd(40)
turtle.circle(16,180)
turtle.fd(80/3)
turtle.done()

这里是我的微信号,大家可以加我一起交流啊!
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/anyifan369/article/details/84973371