Python turtle庫知識小結

2021-09-29 16:39:56 字數 3276 閱讀 2117

turtle庫是python語言中乙個很流行的繪製影象的函式庫

使用之前需要導入庫:import turtle

• turtle.setup(width,height,startx,starty)

-setup() 設定窗體的位置和大小

相對於桌面的起始點的座標以及視窗的寬度高度,若不寫視窗的起始點,則預設在桌面的正中心

窗體的座標原點預設在視窗的中心

• 絕對座標

○ turtle.goto(100,100):指從當前的點指向括號內所給座標

• 海龜座標,把當前點當做座標,有前方向,後方向,左方向,右方向

○ turtle.fd(d):指沿著海龜的前方向執行

○ turtle.bk(d):指沿著海龜的反方向執行

○ turtle.circle(r,angle):指沿著海龜左側的某一點做圓運動

• 絕對角度

○ turtle.seth(angle):只改變海龜的行進方向(角度按逆時針),但不行進,angle為絕對度數

• 海龜角度

○ turtle.left(angle)

○ turtle.right(angle)

畫筆控制函式

1、turtle.penup() 別名turtle.pu()

畫筆抬起,不留下痕跡

2、turtle.pendown() 別名turtle.pd()

畫筆落下,留下

3、turtle.pensize(width) 別名turtle.width(width)

畫筆寬度

4、turtle.pencolor(color)

color為顏色字串或者rgb值

運動控制函式

1、turtle.forword(d) 別名turtle.fd(d)

向前行進

d:行進距離,可以為負數

2、turtle.circle(r,extent=none)

根據半徑r,繪製乙個extent角度的弧度

r:預設圓心在海龜左側r距離的位置

方向控制函式

1、turtle.setheading(angle) 別名turtle.seth(angle)

改變行進方向

2、angle:改變方向的角度(絕對座標下,絕對角度)

3、turtle.left(angle)

4、turtle.right(angle)

angle:當前方向上轉過得角度(海龜角度)

turtle畫國旗例子:

import turtle

def draw_rectangle(x,y,width,height):

""""繪製矩形"""

turtle.goto(x,y)

turtle.pencolor('red')

turtle.fillcolor('red')

turtle.begin_fill( )

for i in range(2) :

turtle.forward(width)

turtle.left(90)

turtle.forward(height)

turtle.left(90)

turtle.end_fill( )

def draw_star(x,y,radius):

""""繪製五角星"""

turtle.setpos(x, y)

pos1 = turtle.pos()

turtle.circle(-radius,72)

pos2 = turtle.pos()

turtle.circle(-radius, 72)

pos3 = turtle.pos()

turtle.circle(-radius,72)

pos4 = turtle.pos()

turtle.circle(-radius, 72)

pos5 = turtle.pos()

turtle.color('yellow','yellow')

turtle.begin_fill()

turtle.goto(pos3)

turtle.goto(pos1)

turtle.goto(pos4)

turtle.goto(pos2)

turtle.goto(pos5)

turtle.end_fill()

def main():

"""主程式"""

turtle.speed(12)

turtle.penup()

x,y = -270, -180

# 畫國旗主體

width, height = 540,360

draw_rectangle(x, y, width, height)

# 畫大星星

pice = 22

center_x, center_y = x + 5 * pice, y + height - pice * 5

turtle.goto(center_x,center_y)

turtle.left(90)

turtle.forward(pice * 3)

turtle.right(90)

draw_star(turtle.xcor(),turtle.ycor(), pice * 3)

x_poses, y_poses = [10, 12, 12, 10], [2,4,7,9]

# 畫小星星

for x_pos, y_pos in zip(x_poses,y_poses):

turtle.goto(x + x_pos * pice, y + height - y_pos * pice)

turtle.left(turtle.towards(center_x,center_y) - turtle.heading())

turtle.forward(pice)

turtle.right(90)

draw_star(turtle.xcor(),turtle.ycor(),pice)

# 隱藏海龜

turtle.ht()

# 顯示繪圖視窗

turtle.mainloop()

if __name__ == '__main__':

main()

python turtle庫的使用

turtle庫是turtle繪圖體系的python實現。誕生於1969年,主要用於程式設計入門,是python語言標準庫之一,入門級繪相簿。import turtle from turtlr import import turtle as t turtle.setup width,height,st...

python turtle庫的認識

os庫 作業系統介面 math庫 數學庫 常見的數學算式 等等 參見我另外一篇csdn 庫就會產生很多方法 介面api 先學會理解使用,再去想為什麼 import turtle 引入模組 庫 turtle t turtle.pen pen 函式。自動建立乙個畫布 turtle.setup width...

python龜庫 Python turtle海龜庫

turtle庫是python語言中乙個很流行的繪製影象的函式庫。在乙個橫軸為x 縱軸為y的座標系原點,0,0 位置開始,想象乙隻面朝x軸正方向小烏龜,它根據一組函式指令的控制,在這個平面座標系中移動,從而在它爬行的路徑上繪製了圖形。標頭檔案import turtle turtle繪圖的基礎知識 1....