Python是一種優(yōu)秀的編程語言,它能夠輕松地完成許多任務(wù)。其中一個很好的例子是用Python畫宮殿,讓我們來看看如何實(shí)現(xiàn)。
import turtle
# 創(chuàng)建一個turtle實(shí)例
turt = turtle.Turtle()
# 啟用速度
turt.speed(10)
# 開始繪圖
def draw():
# 繪制主體
turt.color('beige', 'lightblue')
turt.begin_fill()
turt.forward(300)
turt.right(90)
turt.forward(200)
turt.right(90)
turt.forward(300)
turt.right(90)
turt.forward(200)
turt.right(90)
turt.end_fill()
# 繪制屋頂
turt.color('gold')
turt.begin_fill()
turt.right(30)
turt.forward(210)
turt.right(120)
turt.forward(210)
turt.right(120)
turt.forward(210)
turt.right(150)
turt.end_fill()
# 繪制門
turt.color('black', 'darkgrey')
turt.begin_fill()
turt.right(60)
turt.forward(100)
turt.left(90)
turt.forward(80)
turt.left(90)
turt.forward(100)
turt.left(90)
turt.forward(80)
turt.left(90)
turt.end_fill()
# 繪制門把手
turt.color('black', 'gold')
turt.begin_fill()
turt.right(90)
turt.penup()
turt.forward(25)
turt.right(90)
turt.forward(10)
turt.pendown()
turt.circle(7)
turt.end_fill()
turt.penup()
turt.backward(10)
turt.left(90)
turt.backward(25)
turt.left(90)
turt.pendown()
turt.begin_fill()
turt.circle(7)
turt.end_fill()
# 調(diào)用函數(shù)
draw()
# 點(diǎn)擊窗口關(guān)閉
turtle.exitonclick()
上面的代碼使用了Python的turtle庫,其中函數(shù)draw()繪制了宮殿的主體、屋頂和門。通過不同的顏色和填充實(shí)現(xiàn)了各個部分的繪制。最后,調(diào)用函數(shù)并在完成后等待點(diǎn)擊窗口關(guān)閉。
上一篇php jmail 亂碼
下一篇ajax傳值后臺如何接受