奔驰宝马游戏网站建设,标识导视设计公司官网,东莞阳光网站官网,东营市建设网学习网址#xff1a;https://docs.python.org/zh-cn/3/library/turtle.html
Turtle库
Turtle库是Python语言中一个很流行的绘制图像的函数库#xff0c;一个小乌龟#xff0c;在一个横轴为x、纵轴为y的坐标系原点#xff08;画布中心#xff09;#xff0c;(0,0)位置开…学习网址https://docs.python.org/zh-cn/3/library/turtle.html
Turtle库
Turtle库是Python语言中一个很流行的绘制图像的函数库一个小乌龟在一个横轴为x、纵轴为y的坐标系原点画布中心(0,0)位置开始移动从而在它爬行的路径上绘制了图形
Turtle-窗口
turtle.setup(width0.5, height0.75, startxNone, startyNone)用于设置窗口的大小和位置 参数width, height: 输入宽和高为整数时, 表示像素; 为小数时, 表示占据电脑屏幕的比例 (startx, starty): 这一坐标表示矩形窗口左上角顶点的位置, 如果为空,则窗口位于屏幕中心。
如
turtle.setup(width0.6,height0.6)
turtle.setup(width800,height800, startx100, starty100)说明当画布尺寸比窗口小时系统会自动把画布放大填充满整个窗口比比窗口大时会出现滚动条。
Turtle库-画布
画布就是turtle展开用于绘图的区域我们可以设置它的大小和初始位置。 设置画布大小 turtle.screensize(canvwidthNone, canvheightNone, bgNone)参数分别为画布的宽(单位像素), 高, 背景颜色。
如
turtle.screensize(800,600, green)Turtle库-画笔
画笔(画笔的属性颜色、画线的宽度等)
turtle.pensize()设置画笔的宽度turtle.pencolor()没有参数传入返回当前画笔颜色传入参数设置画笔颜色可以是字符串如green, “red”,也可以是RGB 3元组。turtle.speed(speed)设置画笔移动速度画笔绘制的速度范围[0,10]整数数字越大越快。
Turtle-绘图命令
操纵海龟绘图有着许多的命令这些命令可以划分为3种一种为运动命令一种为画笔控制命令还有一种是全局控制命令
蟒蛇绘制程序 代码
# author:dq
# project:PythonProject
# date:2021年11月18日
# function:
import turtledef drawSnake(rad, angle, len, neckrad):for i in range(len):turtle.circle(rad, angle)turtle.circle(-rad, angle)turtle.circle(rad,angle / 2)turtle.fd(rad)turtle.circle(neckrad 1, 180)turtle.fd(rad * 2 / 3)def main():turtle.setup(1300, 800, 0, 0)pythonsize 30turtle.pensize(pythonsize)turtle.pencolor(blue)turtle.seth(-40)drawSnake(40, 80, 5, pythonsize / 2)turtle.done()
main()
蟒蛇绘制程序说明 太阳花程序 五角星程序 小猪佩奇程序 代码
import turtle as tt.begin_fill()
t.pensize(4)
t.hideturtle()
t.colormode(255)
t.color((255, 155, 192), pink)
t.setup(840, 500)
t.speed(10)# 鼻子
t.pu()
t.goto(-100, 100)
t.pd()
t.seth(-30)
t.begin_fill()
a 0.4
for i in range(120):if 0 i 30 or 60 i 90:a a 0.08t.lt(3) # 向左转3度t.fd(a) # 向前走a的步长else:a a - 0.08t.lt(3)t.fd(a)
t.end_fill()t.pu()
t.seth(90)
t.fd(25)
t.seth(0)
t.fd(10)
t.pd()
t.pencolor(255, 155, 192)
t.seth(10)
t.begin_fill()
t.circle(5)
t.color(160, 82, 45)
t.end_fill()t.pu()
t.seth(0)
t.fd(20)
t.pd()
t.pencolor(255, 155, 192)
t.seth(10)
t.begin_fill()
t.circle(5)
t.color(160, 82, 45)
t.end_fill()# 头
t.color((255, 155, 192), pink)
t.pu()
t.seth(90)
t.fd(41)
t.seth(0)
t.fd(0)
t.pd()
t.begin_fill()
t.seth(180)
t.circle(300, -30)
t.circle(100, -60)
t.circle(80, -100)
t.circle(150, -20)
t.circle(60, -95)
t.seth(161)
t.circle(-300, 15)
t.pu()
t.goto(-100, 100)
t.pd()
t.seth(-30)
a 0.4
for i in range(60):if 0 i 30 or 60 i 90:a a 0.08t.lt(3) # 向左转3度t.fd(a) # 向前走a的步长else:a a - 0.08t.lt(3)t.fd(a)
t.end_fill()# 耳朵
t.color((255, 155, 192), pink)
t.pu()
t.seth(90)
t.fd(-7)
t.seth(0)
t.fd(70)
t.pd()
t.begin_fill()
t.seth(100)
t.circle(-50, 50)
t.circle(-10, 120)
t.circle(-50, 54)
t.end_fill()t.pu()
t.seth(90)
t.fd(-12)
t.seth(0)
t.fd(30)
t.pd()
t.begin_fill()
t.seth(100)
t.circle(-50, 50)
t.circle(-10, 120)
t.circle(-50, 56)
t.end_fill()# 眼睛
t.color((255, 155, 192), white)
t.pu()
t.seth(90)
t.fd(-20)
t.seth(0)
t.fd(-95)
t.pd()
t.begin_fill()
t.circle(15)
t.end_fill()t.color(black)
t.pu()
t.seth(90)
t.fd(12)
t.seth(0)
t.fd(-3)
t.pd()
t.begin_fill()
t.circle(3)
t.end_fill()t.color((255, 155, 192), white)
t.pu()
t.seth(90)
t.fd(-25)
t.seth(0)
t.fd(40)
t.pd()
t.begin_fill()
t.circle(15)
t.end_fill()t.color(black)
t.pu()
t.seth(90)
t.fd(12)
t.seth(0)
t.fd(-3)
t.pd()
t.begin_fill()
t.circle(3)
t.end_fill()# 腮
t.color((255, 155, 192))
t.pu()
t.seth(90)
t.fd(-95)
t.seth(0)
t.fd(65)
t.pd()
t.begin_fill()
t.circle(30)
t.end_fill()# 嘴
t.color(239, 69, 19)
t.pu()
t.seth(90)
t.fd(15)
t.seth(0)
t.fd(-100)
t.pd()
t.seth(-80)
t.circle(30, 40)
t.circle(40, 80)# 身体
t.color(red, (255, 99, 71))
t.pu()
t.seth(90)
t.fd(-20)
t.seth(0)
t.fd(-78)
t.pd()
t.begin_fill()
t.seth(-130)
t.circle(100, 10)
t.circle(300, 30)
t.seth(0)
t.fd(230)
t.seth(90)
t.circle(300, 30)
t.circle(100, 3)
t.color((255, 155, 192), (255, 100, 100))
t.seth(-135)
t.circle(-80, 63)
t.circle(-150, 24)
t.end_fill()# 手
t.color((255, 155, 192))
t.pu()
t.seth(90)
t.fd(-40)
t.seth(0)
t.fd(-27)
t.pd()
t.seth(-160)
t.circle(300, 15)
t.pu()
t.seth(90)
t.fd(15)
t.seth(0)
t.fd(0)
t.pd()
t.seth(-10)
t.circle(-20, 90)t.pu()
t.seth(90)
t.fd(30)
t.seth(0)
t.fd(237)
t.pd()
t.seth(-20)
t.circle(-300, 15)
t.pu()
t.seth(90)
t.fd(20)
t.seth(0)
t.fd(0)
t.pd()
t.seth(-170)
t.circle(20, 90)# 脚
t.pensize(10)
t.color((240, 128, 128))
t.pu()
t.seth(90)
t.fd(-75)
t.seth(0)
t.fd(-180)
t.pd()
t.seth(-90)
t.fd(40)
t.seth(-180)
t.color(black)
t.pensize(15)
t.fd(20)t.pensize(10)
t.color((240, 128, 128))
t.pu()
t.seth(90)
t.fd(40)
t.seth(0)
t.fd(90)
t.pd()
t.seth(-90)
t.fd(40)
t.seth(-180)
t.color(black)
t.pensize(15)
t.fd(20)# 尾巴
t.pensize(4)
t.color((255, 155, 192))
t.pu()
t.seth(90)
t.fd(70)
t.seth(0)
t.fd(95)
t.pd()
t.seth(0)
t.circle(70, 20)
t.circle(10, 330)
t.circle(70, 30)
t.end_fill()t.done()
樱花程序 # codingutf-8
# 画一棵樱花import turtle
import randomfrom time import sleep# 画樱花的躯干(60,t)
def tree(branchLen, t):sleep(0.0005)if branchLen 3:if 8 branchLen 12:if random.randint(0, 2) 0:t.color(snow) # 白else:t.color(lightcoral) # 淡珊瑚色t.pensize(branchLen / 3)elif branchLen 8:if random.randint(0, 1) 0:t.color(snow)else:t.color(lightcoral) # 淡珊瑚色t.pensize(branchLen / 2)else:t.color(sienna) # 赭(zhě)色t.pensize(branchLen / 10) # 6t.forward(branchLen)a 1.5 * random.random()t.right(20 * a)b 1.5 * random.random()tree(branchLen - 10 * b, t)t.left(40 * a)tree(branchLen - 10 * b, t)t.right(20 * a)t.up()t.backward(branchLen)t.down()# 掉落的花瓣
def petal(m, t):for i in range(m):a 200 - 400 * random.random()b 10 - 20 * random.random()t.up()t.forward(b)t.left(90)t.forward(a)t.down()t.color(lightcoral) # 淡珊瑚色t.circle(1)t.up()t.backward(a)t.right(90)t.backward(b)def main():# 绘图区域t turtle.Turtle()# 画布大小w turtle.Screen()t.hideturtle() # 隐藏画笔turtle.getscreen().tracer(5, 0)w.screensize(bgwheat) # wheat小麦t.left(90)t.up()t.backward(150)t.down()t.color(sienna)# 画樱花的躯干tree(60, t)# 掉落的花瓣petal(200, t)w.exitonclick()main()三角形程序 #author:dq
#project:PythonProject
#date:2021年11月18日
#function:三角形import turtle
def drawBig():turtle.pensize(5)turtle.pencolor(yellow)turtle.forward(200)turtle.left(120)turtle.forward(200)turtle.left(120)turtle.forward(200)
def drawLitte():turtle.home()turtle.forward(100)turtle.left(60)turtle.forward(100)turtle.left(120)turtle.forward(100)turtle.left(120)turtle.forward(100)turtle.done()
drawBig()
drawLitte()