7.15学习-turtle库的使用
关于turtle库的学习
turtle库简介
urtle(海龟)库是turtle绘图体系的Python实现
1.turtle绘图体系:1969年诞生,主要用于程序设计入门
2.Python语言的标准库之一
3.入门级的图形绘制函数库
标准库
Python计算生态 = 标准库 + 第三方库
1.标准库:随解释器直接安装到操作系统中的功能模块
2.第三方库:需要经过安装才能使用的功能模块
3.库Library、包Package、模块Module,统称模块
turtle模块使用介绍
import turtle
#设置画线速度
turtle.speed(1)
#向前移动50像素
turtle.forward(50)
#向前移动50像素
turtle.fd(50)
#向后移动10像素
turtle.forward(-10)
#向后移动30像素
turtle.fd(-30)
#向后移动10像素
turtle.back(10)
#向后移动10像素
turtle.bk(10)
#机器小乌龟定位到x=100,y=100
turtle.goto(100,100)
#机器小乌龟定位到x=0,y=0
turtle.setpos(0,0)
#机器小乌龟定位到x=-10,y=-10
turtle.setposition(-10,-10)
#机器小乌龟定位到x=200, y不变
turtle.setx(200)
#机器小乌龟定位到y=-50, x不变
turtle.sety(-50)
#机器小乌龟箭头方向调整,0-向右,90-向上,180-向左,270-向下
turtle.setheading(0)
#机器小乌龟箭头方向调整,0-向右,90-向上,180-向左,270-向下
turtle.seth(90)
#回到原点(x=0,y=0)
turtle.home()
#画圆(逆时针),半径10像素
turtle.circle(10)
#画270度的圆(逆时针),半径20像素
turtle.circle(20,270)
#画90度的圆(顺时针),半径30像素
turtle.circle(-30,90)
#画360度的圆(顺时针),半径100像素,指定该圆由1000个内接多边形近似而成
turtle.circle(-100,360,1000)
习题练习
使用turtle库,绘制一个八边形。
import turtle
turtle.setup(800,400)
turtle.penup()
turtle.fd(80)
turtle.pendown()
turtle.pensize(5)
turtle.pencolor("purple")
for i in range(8):
turtle.forward(40)
turtle.right(45)
turtle.done()
使用turtle库,绘制一个八角图形。
import turtle
turtle.setup(800,800)
turtle.penup()
turtle.fd(80)
turtle.pendown()
turtle.pensize(5)
turtle.pencolor("purple")
for i in range(8):
turtle.forward(100)
turtle.left(135)
turtle.done()
简述import <模块名>
/from <模块名> import *
/import <模块名> as <新模块名>
三者的区别
import <模块名>为导入模块
from <模块名> import *为导入该模块所有方法,只加载一次
import <模块名> as <新模块名>为将一个名字较长的模块名 简化成另一个名字
设计程序,要求:循环打印数列`1,3,5,...,99
for i in range (100):
if i % 2 == 1:
print(i)
使用turtle库,绘制一个自己喜欢的图形
我画了个巨丑的粉红小王八
import turtle as t
t.setup(1000,600)
#画头
t.penup()
t.fd(80)
t.pendown()
t.pensize(5)
t.pencolor("pink")
t.circle(45)
#画左眼
t.penup()
t.goto(60,60)
t.pendown()
t.pencolor("black")
t.circle(1)
#画右眼
t.penup()
t.goto(100,60)
t.pendown()
t.pencolor("black")
t.circle(1)
#画嘴巴
t.penup()
t.goto(65,20)
t.pendown()
t.pencolor("red")
t.seth(-15)
t.circle(40,45)
#画身体
t.penup()
t.goto(80,0)
t.pendown()
t.pencolor("pink")
t.seth(180)
t.circle(70)
#画左上
t.penup()
t.goto(20,-27)
t.pendown()
t.pencolor("pink")
t.seth(45)
t.circle(20)
#画右上
t.penup()
t.goto(160,-27)
t.pendown()
t.pencolor("pink")
t.seth(45)
t.circle(20)
#画左下
t.penup()
t.goto(20,-124)
t.pendown()
t.pencolor("pink")
t.seth(45)
t.circle(20)
#画右下
t.penup()
t.goto(175,-124)
t.pendown()
t.pencolor("pink")
t.seth(45)
t.circle(20)
#画龟背
#竖纹
t.penup()
t.goto(10,-70)
t.pendown()
t.seth(0)
t.fd(140)
t.pencolor("pink")
t.penup()
t.goto(10,-50)
t.pendown()
t.seth(0)
t.fd(140)
t.pencolor("pink")
t.penup()
t.goto(20,-25)
t.pendown()
t.seth(0)
t.fd(120)
t.pencolor("pink")
t.penup()
t.goto(15,-105)
t.pendown()
t.seth(0)
t.fd(120)
t.pencolor("pink")
t.penup()
t.goto(8,-85)
t.pendown()
t.seth(0)
t.fd(140)
t.pencolor("pink")
#画竖纹
t.penup()
t.goto(40,-10)
t.pendown()
t.seth(270)
t.fd(120)
t.pencolor("pink")
t.penup()
t.goto(80,0)
t.pendown()
t.seth(270)
t.fd(140)
t.pencolor("pink")
t.penup()
t.goto(120,-10)
t.pendown()
t.seth(270)
t.fd(120)
t.pencolor("pink")
t.done()