• Python3-2020-测试开发-2- Python3之turtle模块使用


    一、基础语法:

    (1)画笔属性:
    
    1) turtle.pensize():设置画笔的宽度;
    
    2) turtle.pencolor():没有参数传入,返回当前画笔颜色,传入参数设置画笔颜色,可以是字符串如"green", "red",也可以是RGB 3元组。
    
    3) turtle.speed(speed):设置画笔移动速度,画笔绘制的速度范围[0,10]整数,数字越大越快。
    
     
    
    (2)绘制命令:
    
    1)turtle.forward(distance)(别名:turtle.fd):向当前画笔方向移动distance像素长度。
    
    2)turtle.backward(distance):向当前画笔相反方向移动distance像素长度。
    
    3)turtle.right(degree):顺时针移动degree°。
    
    4)turtle.left(degree):逆时针移动degree°。
    
    5)turtle.pendown()(别名:turtle.pd(),turtle.down()):移动时绘制图形,缺省时也为绘制。
    
    6)turtle.goto(x,y):将画笔移动到坐标为x,y的位置。
    
    7)turtle.penup()(别名:turtle.pu(),turtle.up()):提起笔移动,不绘制图形,用于另起一个地方绘制。
    
    8)turtle.circle():画圆,半径为正(负),表示圆心在画笔的左边(右边)画圆。
    
    9)setx( ):将当前x轴移动到指定位置。
    
    10)sety( ):将当前y轴移动到指定位置。
    
    11)setheading(angle):设置当前朝向为angle角度。
    
    12)home():设置当前画笔位置为原点,朝向东。
    
    13)dot(r):绘制一个指定直径和颜色的圆点。
    
    14)turtle.fillcolor(colorstring):绘制图形的填充颜色。
    
    15)turtle.color(color1, color2):同时设置pencolor=color1, fillcolor=color2。
    
    16)turtle.filling():返回当前是否在填充状态。
    
    17)turtle.begin_fill():准备开始填充图形。
    
    18)turtle.end_fill():填充完成。
    
    19)turtle.hideturtle():隐藏画笔的turtle形状。
    
    20)turtle.showturtle():显示画笔的turtle形状。
    
    21)turtle.seth(to_angle)(别名:turtle.setheading(to_angle)):设置小海龟当前前进方向为to_angle,该角度是绝对方向的角度值。

    二、例子 

    1、代码展示:图为奥运五环

    def circle():
    
        # 设置画笔的宽度
        t.width(10)
    
        # 画第一个圆
        # 设置颜色
        t.color("blue")
        t.circle(50)
    
    
        # 横坐标向右移动120
        # 抬笔
        # 落笔
        t.penup()
        t.goto(120,0)
        t.pendown()
    
    
        # 画第二个圆
        t.color("black")
        t.circle(50)
    
        # 横坐标向右移动240
        t.penup()
        t.goto(240,0)
        t.pendown()
    
        # 画第三个圆
        t.color("red")
        t.circle(50)
    
    
        t.penup()
        t.goto(60,-50)
        t.pendown()
    
        # 画第四个圆
        t.color("yellow")
        t.circle(50)
    
    
        t.penup()
        t.goto(180,-50)
        t.pendown()
    
        # 画第五个圆
        t.color("green")
        t.circle(50)
    
    
        t.done()
    
    
    circle()

    结果:

     2、 玫瑰花

    from turtle import *
    def flower():
    
        # global pen and speed
        pencolor("black")
        fillcolor("red")
        speed(50)
        s = 0.15
        # init poistion
        penup()
        goto(0, 600 * s)
        pendown()
        begin_fill()
        circle(200 * s, 30)
        for i in range(60):
            lt(1)
            circle(50 * s, 1)
        circle(200 * s, 30)
        for i in range(4):
            lt(1)
            circle(100 * s, 1)
        circle(200 * s, 50)
        for i in range(50):
            lt(1)
            circle(50 * s, 1)
        circle(350 * s, 65)
        for i in range(40):
            lt(1)
            circle(70 * s, 1)
        circle(150 * s, 50)
        for i in range(20):
            rt(1)
            circle(50 * s, 1)
        circle(400 * s, 60)
        for i in range(18):
            lt(1)
            circle(50 * s, 1)
        fd(250 * s)
        rt(150)
        circle(-500 * s, 12)
        lt(140)
        circle(550 * s, 110)
        lt(27)
        circle(650 * s, 100)
        lt(130)
        circle(-300 * s, 20)
        rt(123)
        circle(220 * s, 57)
        end_fill()
        lt(120)
        fd(280 * s)
        lt(115)
        circle(300 * s, 33)
        lt(180)
        circle(-300 * s, 33)
        for i in range(70):
            rt(1)
            circle(225 * s, 1)
        circle(350 * s, 104)
        lt(90)
        circle(200 * s, 105)
        circle(-500 * s, 63)
        penup()
        goto(170 * s, -330 * s)
        pendown()
        lt(160)
        for i in range(20):
            lt(1)
            circle(2500 * s, 1)
        for i in range(220):
            rt(1)
            circle(250 * s, 1)
        fillcolor('green')
        penup()
        goto(670 * s, -480 * s)
        pendown()
        rt(140)
        begin_fill()
        circle(300 * s, 120)
        lt(60)
        circle(300 * s, 120)
        end_fill()
        penup()
        goto(180 * s, -850 * s)
        pendown()
        rt(85)
        circle(600 * s, 40)
        penup()
        goto(-150 * s, -1300 * s)
        pendown()
        begin_fill()
        rt(120)
        circle(300 * s, 115)
        lt(75)
        circle(300 * s, 100)
        end_fill()
        penup()
        goto(430 * s, -1370 * s)
        pendown()
        rt(30)
        circle(-600 * s, 35)
    
        done()
    
    flower()

     结果:

  • 相关阅读:
    2716 [Violet 3] 天使玩偶
    BZOJ1017魔兽地图DotR 樹形DP
    BZOJ1016最小生成树计数 最小生成树 + 排列组合
    BZOJ1014火星人prefix Splay維護序列 + 字符串哈希
    BZOJ1010玩具裝箱Toy
    BZOJ1009GT考试 DP + KMP + 矩陣快速冪
    BZOJ1008[HNOI2008]越狱
    BZOJ1006神奇的国度 弦圖染色 最大勢算法
    BZOJ1005明明的烦恼 Prufer + 分解質因數 + 高精度
    poj2182(线段树求序列第k小)
  • 原文地址:https://www.cnblogs.com/chushujin/p/12746110.html
Copyright © 2020-2023  润新知