在Python中,我們可以使用turtle庫(kù)來(lái)繪制圖形,其中包括風(fēng)車。下面是繪制纖細(xì)風(fēng)車的代碼:
import turtle turtle.speed(0) turtle.pensize(2) # 左邊的三角形 turtle.penup() turtle.goto(-50, 0) turtle.pendown() turtle.fillcolor("red") turtle.begin_fill() turtle.goto(-30, 30) turtle.goto(-30, 0) turtle.goto(-50, 0) turtle.end_fill() # 右邊的三角形 turtle.penup() turtle.goto(-30, 30) turtle.pendown() turtle.fillcolor("blue") turtle.begin_fill() turtle.goto(0, 0) turtle.goto(-30, 0) turtle.goto(-30, 30) turtle.end_fill() # 矩形 turtle.penup() turtle.goto(-20, -20) turtle.pendown() turtle.fillcolor("green") turtle.begin_fill() turtle.goto(20, -20) turtle.goto(20, 20) turtle.goto(-20, 20) turtle.goto(-20, -20) turtle.end_fill() # 線條 turtle.penup() turtle.goto(0, -20) turtle.pendown() turtle.goto(0, 20) turtle.penup() turtle.goto(-20, 0) turtle.pendown() turtle.goto(20, 0) turtle.hideturtle() turtle.done()
代碼中使用了turtle庫(kù)的函數(shù)來(lái)移動(dòng)畫筆和繪制圖形,其中penup和pendown來(lái)切換畫筆狀態(tài),fillcolor和begin_fill,end_fill用來(lái)填充顏色,goto用來(lái)移動(dòng)畫筆,speed用來(lái)設(shè)置繪制速度(0為最快),hideturtle用來(lái)隱藏畫筆,done用來(lái)保持圖形不關(guān)閉。