新浪博客

Python turtle库的函数汇总(两个示例--)

2014-05-15 21:03阅读:
2.python turtle 库(点线面的简单图像库)应用
python turtle库是不自带画点,点都是用半径很小的填充圆来表示
本代码使用了python 中的turtle 来完成结果的可视化任务,下面介绍一些基本函数的用法,总的来说,turtle库功能简单,能够完成一些比较简单的几何图像可视化
设置函数:
turtle.penup()
turtle.pendown()
turtle.pensize(width)
这三个函数分别表示gui界面中,画笔的提起,落下,和画笔的粗细.画笔默认为落下
关于画笔的移动函数:
turtle.forward(100) ---向前移动100
turtle.backward(100)----向后移动100
turtle.right(angle)-----画笔的前进方向向右转动 (单位是角度)
turtle.left(angle)-----画笔的前进方向向左转动(单位为角度)
turtle.goto(x,y)------移动到x,y
turtle.setx(x) ———————横向移动
turtle.sety(y)--------纵向移动
turtle.setheading(angle)——(0 —- 东 90—北,180--西,270--南)画笔的移动方向设置
启动界面后,画笔的默认方向为东
turtle.home() ————返回原点
turtle.circle(r,ext,step)--
---画圆或者圆的内接多边形(不是以左边点为圆形,是已坐标点为多边形的一个点)
turtle.dot(diameter,color)———-画圆(以坐标点为圆心),diameter为半径
turtle.undo() —————-清空画面
turtle.speed()—————- 速度 1-10
turtle.color(“red”)——-设置画笔的颜色
关于填充图像的一些命令:
turtle.begin_fill()
turtle.end_fill()
turtle.fillcolor(“red”)填充颜色


turtle.done()---画布停留,不然画面会一闪而过。
关于文字:
turtle.write(“s”,font = (“times”,18,”bold”)) 写字符s font中的三个属性分别是(字体样式,大小,加粗)
下面给一个示例(随机游走的模拟)
Python <wbr>turtle库的函数汇总(两个示例--)
1。RandomWalk.py
#画出网格图,坐标关系x---右,y ——-上,初始(0,0)为turtle的初始点
import random
import turtle
x = -80
#纵坐标的线绘制
for y in range(-80,80+1,10):
turtle.penup()
turtle.goto(x,y)
turtle.pendown()
turtle.forward(160)
#横坐标线绘制
y = 80
turtle.right(90)
for x in range(-80,80+1,10):
turtle.penup()
turtle.goto(x,y)
turtle.pendown()
turtle.forward(160)
turtle.pensize(3)
turtle.color(“red')
#模拟走动(是个方向等概率)
turtle.penup()
turtle.goto(0,0)
turtle.pendown()
x = y =0
#设置移动范围
while abs(x) < 80 and abs(y) < 80:
r = random.randint(0,3)#产生随机数,0,1,2,3表示是个方向
if r == 0:
x += 10#right
turtle.setheading(0)
turtle.forward(10)
elif r == 1:
y -= 10
turtle.setheading(270)
turtle.forward(10)
elif r == 2:
x -= 10
turtle.setheading(180)
turtle.forward(10)
elif r == 3:
y += 10
turtle.setheading(90)
turtle.forward(10)
turtle.done()


2.画多边形并且标注
Python <wbr>turtle库的函数汇总(两个示例--)
import turtle
turtle.pensize(3) # Set pen thickness to 3 pixels
turtle.penup() # Pull the pen up
turtle.goto(-200, -50)
turtle.setheading(60)
turtle.pendown() # Pull the pen down
turtle.begin_fill() # Begin to fill color in a shape
turtle.color('red')
turtle.circle(40, steps = 3) # Draw a triangle
turtle.end_fill() # Fill the shape
turtle.penup()
turtle.goto(-100, -50)
turtle.pendown()
turtle.begin_fill() # Begin to fill color in a shape
turtle.color('blue')
turtle.setheading(45)
turtle.circle(40, steps = 4) # Draw a square
turtle.end_fill() # Fill the shape
turtle.penup()
turtle.goto(0, -50)
turtle.pendown()
turtle.begin_fill() # Begin to fill color in a shape
turtle.color('green')
turtle.setheading(35)
turtle.circle(40, steps = 5) # Draw a pentagon
turtle.end_fill() # Fill the shape
turtle.penup()
turtle.goto(100, -50)
turtle.pendown()
turtle.begin_fill() # Begin to fill color in a shape
turtle.color('yellow')
turtle.setheading(30)
turtle.circle(40, steps = 6) # Draw a hexagon
turtle.end_fill() # Fill the shape
turtle.penup()
turtle.goto(200, -50)
turtle.pendown()
turtle.begin_fill() # Begin to fill color in a shape
turtle.color('purple')
turtle.setheading(25)
turtle.circle(40, steps = 8) # Draw a circle
turtle.end_fill() # Fill the shape
turtle.color('green')
turtle.penup()
turtle.goto(-100, 50)
turtle.pendown()
turtle.write('Cool Colorful Shapes',
font = ('Times', 18, 'bold'))
turtle.hideturtle()
turtle.done()

我的更多文章

下载客户端阅读体验更佳

APP专享