007 海归图turtle
本节内容:基本语法及一些应用场景。
- 实心矩阵
- 旗帜
- 五角星
- Write方法
- 柱状图
- 36多花瓣
- 折线图
- fractal分形图形
一、基本语法
import turtle #导入模块 t=turtle.Turtle()#画布 t.hideturtle()#隐藏 t.up()#抬起笔 t.down()#放下笔 t.forward(dist)#按照小海龟朝向的方向移动dist个像素 t.backward(dist)#按照小海龟朝向的相反方向移动dist个像素 t.goto(x,y)#将小海龟移动到坐标(x,y) t.pencolor(colorName)#笔的颜色,初始颜色为黑色 t.setheading(deg)#设置小海龟面朝deg度的方向 t.left(deg)#将小海龟逆时针旋转deg度 t.right(deg)#将小海龟顺时针旋转deg度 t.dot(diameter,colorName)#按照给定直径和颜色在当前位置上画点(圆) t.fillcolor(colorName)#填充颜色(完全封闭的区域) t.begin_fill() and t.end_fill()#绘制区域的颜色填充(必须放在绘制区域的语句前后) t.write(s)#以当前位置作为字符串的左下角显示出字符串。 t.write(s,align="right") t.write(s,align="center") t.write(s,align="left")
二、场景应用
1、画一个实心矩形
import time import turtle def main(): t=turtle.Turtle() t.hideturtle() drawFilledRectangle(t,0,0,100,150,"red","yellow") time.sleep(3) def drawFilledRectangle(t,x,y,w,h,colorP="black",colorF="write"): t.pencolor(colorP) t.fillcolor(colorF) t.up() t.goto(x,y) t.down() t.begin_fill() t.goto(x+w,y) t.goto(x+w,y+h) t.goto(x,y+h) t.goto(x,y) t.end_fill() main()

2、旗帜
import time import turtle def main(): t=turtle.Turtle() t.hideturtle() drawFilledRectangle(t,0,0,150,25,"light blue","light blue") drawFilledRectangle(t,0,25,150,50,"blue","blue") drawFilledRectangle(t,0,75,150,25,"light blue","light blue") drawDot(t,75,50,40,"white") time.sleep(15) def drawFilledRectangle(t,x,y,w,h,colorP="black",colorF="write"): t.pencolor(colorP) t.fillcolor(colorF) t.up() t.goto(x,y) t.down() t.begin_fill() t.goto(x+w,y) t.goto(x+w,y+h) t.goto(x,y+h) t.goto(x,y) t.end_fill() def drawDot(t,x,y,diameter,colorP): t.up() t.goto(x,y) t.pencolor(colorP) t.dot(diameter) main()

3、五角星
import time import turtle def main(): t=turtle.Turtle() t.hideturtle() lengthOfSize=200 drawFivePointStar(t,0,0,lengthOfSize) time.sleep(5) def drawFivePointStar(t,x,y,lengthOfSide): t.pencolor("red") t.fillcolor("blue") t.up() t.goto(x,y) t.left(36) t.down() t.begin_fill() for i in range(5): t.forward(lengthOfSide) t.left(144) #144=180-36 t.end_fill() main()

4、Write方法
import time import turtle def main(): t=turtle.Turtle() t.hideturtle() t.up() t.goto(0,60) t.dot() # t.write("Python") t.write("Python",font=("Courier New",12,"bold")) #加粗,12号,Courier New字体 t.goto(0,30) t.dot() t.write("Python", align="right") t.goto(0,0) t.dot() t.write("Python",align="center") t.down() time.sleep(1) main()

5、柱状图
import time import turtle heigts=[856,420,360,260,205] def main(): t=turtle.Turtle() t.hideturtle() for i in range(5): drawFilledRectangle(t,-200 + (76 * i),0,76,heigts[i] / 4,"black","light blue") displayText(t) time.sleep(15) def drawFilledRectangle(t,x,y,w,h,colorP="black",colorF="white"): t.pencolor(colorP) t.fillcolor(colorF) t.up() t.goto(x,y) t.down() t.begin_fill() t.goto(x+w,y) t.goto(x+w,y+h) t.goto(x,y+h) t.goto(x,y) t.end_fill() def displayText(t): languages=["Mandarin","Spanish","English", "Hindi","Bengali"] t.pencolor("blue") t.up() for i in range(5): t.goto(-162 + (76 * i), heigts[i] / 4) t.write(str(heigts[i]), align="center", font=("Arial", 10, "normal")) t.goto(-162+(76 * i),10) t.write(str(languages[i]),align="center",font=("Arial",10,"normal")) t.goto(-200,-25) t.write('(in millions of "first language" speekers)', font=("Arial",10,"normal")) main()

6、36朵花瓣
import time import turtle heigts=[856,420,360,260,205] def main(): t=turtle.Turtle() t.hideturtle() t.color("blue","light blue") t.begin_fill() for i in range(36): t.forward(200) t.left(170) t.end_fill() time.sleep(5) main()

7、折线图
#折线图 import turtle import time yValues=[10.0,7.4,6.4,5.3,4.4,3.7,2.6] def main(): t=turtle.Turtle() t.hideturtle() drawLine(t,0,0,300,0) drawLine(t,0,0,0,175) for i in range(6): drawLineWithDots(t,40+(40*i),15*yValues[i],40+(40*(i+1)),15*yValues[i+1],"blue") drawTickMarks(t) displayText(t) time.sleep(1) def drawLine(t,x1,y1,x2,y2,colorP="black"): t.up() t.goto(x1,y1) t.down() t.pencolor(colorP) t.goto(x2,y2) def drawLineWithDots(t,x1,y1,x2,y2,colorP="black"): t.pencolor(colorP) t.up() t.goto(x1,y1) t.dot(5) t.down() t.goto(x2,y2) t.dot(5) def drawTickMarks(t): for i in range(1,8): drawLine(t,40*i,0,40*i,10) drawLine(t,0,15*max(yValues),10,15*max(yValues)) drawLine(t,0,15*min(yValues),10,15*min(yValues)) def displayText(t): t.pencolor("blue") t.up() t.goto(-3,(15*max(yValues))-10) t.write(max(yValues),align="right") x=40 for i in range(2000,2013,2): t.goto(x,-20) t.write(str(i),align="center") x+=40 t.goto(0,-50) t.write("Pencentage of College Freshmen Who Smoke") main()

8、绘制一个fractal分形图形(递归的分形计算函数)
import turtle import time def main(): t=turtle.Turtle() t.hideturtle() t.speed(0) level=12 fract(t,-80,60,80,60,level) time.sleep(5) def fract(t,x1,y1,x2,y2,level): newX=0 newY=0 if level==0: drawLine(t,x1,y1,x2,y2) else: newX=(x1+x2)/2+(y2-y1)/2 newY=(y1+y2)/2-(x2-x1)/2 fract(t,x1,y1,newX,newY,level-1) fract(t,newX,newY,x2,y2,level-1) def drawLine(t,x1,y1,x2,y2): t.up() t.goto(x1,y1) t.down() t.goto(x2,y2) main()







浙公网安备 33010602011771号