[python學習]Turtle庫

(一)簡介

python內置圖形化模塊

#一般操作
import Turtle
from Turtle import *

(二)控制畫筆繪製狀態的方法

1.pendown():放下畫筆移到指定點後繼續繪製
2.penup(): 提起畫筆,用於另起一個地方繪製時使用,與pendown()配對
3.pensieze(width) : 設置畫筆的粗細

(三)Turtle的運動狀態

1.forward():沿着當前方向前進指定距離
2.backward():沿着當前方向的反方向後退指定距離
3.right(angle) : 順時針旋轉angle角度
4.left(angle) : 逆時針旋轉angle角度
5.goto(x,y) : 移到絕對座標(x,y)處
6.setx() : 將當前x軸移動到指定位置
7.sety() : 將當前y軸移動到指定位置
8.setheading(angle) : 設置當前朝向爲angle角度
9.home() : 設置當前畫筆位置爲原點,朝向東
10.circle() : 繪製一個指定半徑、角度、以及步驟的圓圈
11.dot(r) : 繪製一個指定直徑和顏色的圓點
12.undo() : 撤銷畫筆的最後一個動作
13.speed() : 設置畫筆的繪製速度,參數爲0-10之間

(四)Turtle顏色和字體繪製方法

1.color():設置畫筆的顏色
2.begin_fill(): 填充顏色前,調用該方法
3.end_fill() : 填充圖形結束
4.filling() : 返回填充狀態,True爲填充,False爲未填充
5.clear() : 清空當前窗口,但不改變當前窗口的位置
6.reset() : 清空當前窗口,並重置位置等狀態爲默認值
7.screensize() : 設置畫布的長和寬
8.hideturtle() : 隱藏畫筆Turtle的形狀
9.showturtle() : 顯示畫筆Turtle的形狀
10.isvisible() : 如果Turtle可見,則返回Ture
11.wirte() : 輸出font字體的字符串

(五)實例

import turtle

def main():
    turtle.pensize(3)
    turtle.penup()
    turtle.goto(-200,-50)
    turtle.pendown()
    turtle.begin_fill()
    turtle.color("red")
    turtle.circle(40, steps=3)
    turtle.end_fill()


    turtle.penup()
    turtle.goto(-100,-50)
    turtle.pendown()
    turtle.begin_fill()
    turtle.color("blue")
    turtle.circle(40, steps=4)
    turtle.end_fill()

    turtle.penup()
    turtle.goto(0,-50)
    turtle.pendown()
    turtle.begin_fill()
    turtle.color("green")
    turtle.circle(40, steps=5)
    turtle.end_fill()

    turtle.penup()
    turtle.goto(100,-50)
    turtle.pendown()
    turtle.begin_fill()
    turtle.color("yellow")
    turtle.circle(40, steps=6)
    turtle.end_fill()

    turtle.penup()
    turtle.goto(200,-50)
    turtle.pendown()
    turtle.begin_fill()
    turtle.color("purple")
    turtle.circle(40)
    turtle.end_fill()

    turtle.color("green")
    turtle.penup()
    turtle.goto(-100,50)
    turtle.pendown()
    turtle.write(("Cool Colorful shapes"),
        font = ("Times", 18, "bold"))
    turtle.hideturtle()

    turtle.done

if __name__ == '__main__':
    main()

程序運行結果

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章