python海龟绘图turtle

turtle是python比较好用的画图模块,通过控制pen的移动,很方便实现画图功能。常用的函数如下表:

函数 功能
turtle.setup(width=0.5, height=0.75, startx=None, starty=None) 参数:width, height:输入宽和高为整数时, 表示像素; 为小数时, 表示占据电脑屏幕的比例,(startx, starty): 这一坐标表示矩形窗口左上角顶点的位置, 如果为空,则窗口位于屏幕中心。
turtle.bgcolor("black") 设置画面背景色
turtle.bgpic("xxx.gif") 设置背景图片,只支持gif格式
turtle.bye() 退出turtle,无任何提示信息
turtle.exitonclick() 点击后退出turtle
turtle.done() 关闭turtle,一般在使用完turtle后添加,否则会无响应
turtle.pendown() 放下画笔 
turtle.penup() 抬起画笔 
turtle.pensize(int)                          设置画笔宽度,值为整数型
turtle.forward(float)                        画笔前进长度,以像素为单位
turtle.backward(float)                       画笔后退长度,以像素为单位
turtle.right(angle)                          将画笔右转指定的角度
turtle.left(angle)                           将画笔左转指定的角度
turtle.goto(x,y)                             将画笔移动到一个指定的绝对坐标
turtle.setx(x)                               设置画笔向x方向移动的距离,值为实数
turtle.sety(y)                               设置画笔向y方向移动的距离,值为实数
turtle.setheading(angle)                     设定turtle箭头的方向为指定方向,0–东90—北
turtle.home()                                将画笔返回到原点
turtle.circle(r,ext,steps=int)               绘制一个设置半径和阶数的圆(设置之后会绘制多边形)
turtle.dot(d,color)                          绘制一个指定直径的圆点,颜色为字符串类型
turtle.undo()                                取消最后一个图操作
turtle.speed(s)                              设置画笔颜色,为整数类型,且取值在1-10之间
turtle.color(‘str’)                          设置画笔颜色,为字符串类型
turtle.fillcolor(‘str’)                      设置填充颜色,为字符串类型
turtle.begin_fill()                          结束填充
turtle.end_fill()                            开始填充
turtle.filling()                             返回填充状态,True表示填充,False表示没有填充
turtle.clear()                               清除窗口所有内容
turtle.reset()                               清除窗口,将状态和位置复位为初始值
turtle.screensize(w,h)                       设置turtle显示的大小,并设置宽度和高度
turtle.hideturtle()                          隐藏turtle箭头
turtle.showturtle()                          显示turtle窗口
turtle.done()                                使turtle窗口不会自动消失
turtle.isvisible()                           如果turtle可见,返回turtle
turtle.write('TTTTT',font=('Arial',8,'normal')) 在turtle位置编写字符串s,字体由字体名、字体大小、字体类型三部分组成
turtle.position()                            获取画笔的坐标,返回一个元组,值为浮点型

e.g:

#coding:utf-8

import turtle
import math

t = turtle.Turtle()
def setpen(x,y):
    t.penup()
    t.goto(x,y)
    t.pendown()
    t.setheading(0)
    t.pensize(2)
    t.speed(10)

def circle(r,color):
    start_x = 0
    start_y = -r
    setpen(start_x, start_y)
    t.pencolor(color)
    t.fillcolor(color)
    t.begin_fill()
    t.circle(r)
    t.end_fill()


def five_star(r,color):
    setpen(0,0)
    t.pencolor(color)
    t.setheading(162)
    t.forward(r)
    t.setheading(0)
    t.fillcolor(color)
    t.begin_fill()
    for i in range(5):
        t.forward(math.cos(math.radians(18))*2*r) #2cos18°*r
        t.right(144)
    t.end_fill()
    t.hideturtle()


if __name__ == '__main__':
    circle(288,'crimson')
    circle(234,'snow')
    circle(174,'crimson')
    circle(114,'blue')
    five_star(114,'snow')
    input()
    turtle.done()



猜你喜欢

转载自blog.csdn.net/gordennizaicunzai/article/details/81148402