Python是一種流行的編程語言,可以用它來繪制各種圖形和圖像,包括我們今天要介紹的房屋和周邊地區。我們將使用Python的turtle庫,它是一個Python自帶的繪圖庫,非常適合初學者。
import turtle # 繪制房屋 turtle.pensize(4) turtle.penup() turtle.goto(-200, 0) turtle.pendown() # 繪制前墻 turtle.begin_fill() turtle.color("#ffa07a") turtle.forward(200) turtle.left(90) turtle.forward(150) turtle.left(90) turtle.forward(200) turtle.left(90) turtle.forward(150) turtle.end_fill() # 繪制屋頂 turtle.begin_fill() turtle.color("#8b4513") turtle.left(30) turtle.forward(200) turtle.left(120) turtle.forward(200) turtle.left(120) turtle.forward(200) turtle.end_fill() # 繪制門和窗戶 turtle.penup() turtle.goto(-100, -150) turtle.pendown() turtle.color("#663300") turtle.begin_fill() turtle.forward(50) turtle.left(90) turtle.forward(100) turtle.left(90) turtle.forward(50) turtle.left(90) turtle.forward(100) turtle.end_fill() turtle.penup() turtle.goto(50, -120) turtle.pendown() turtle.begin_fill() turtle.color("#4d4d4d") turtle.circle(30) turtle.end_fill() # 繪制周邊 turtle.penup() turtle.goto(250, 0) turtle.pendown() turtle.color("#008000") turtle.begin_fill() turtle.circle(100) turtle.end_fill() turtle.penup() turtle.goto(230, 100) turtle.pendown() turtle.color("#00ff00") turtle.begin_fill() turtle.circle(50) turtle.end_fill() turtle.penup() turtle.goto(300, -45) turtle.pendown() turtle.color("#00ff00") turtle.begin_fill() turtle.circle(35) turtle.end_fill() turtle.penup() turtle.goto(150, -150) turtle.pendown() turtle.color("#000080") turtle.begin_fill() turtle.circle(80) turtle.end_fill() turtle.done()
以上是我們的Python代碼,運行后會繪制出一個房屋和周邊環境,包括樹木和草地。通過turtle庫的各種函數和方法,我們可以輕松繪制出各種各樣的圖形和圖像。我們要掌握的是Python的基礎語法和turtle庫的使用方法,才能在編寫程序時得心應手,流暢自如地進行編寫。