您好,登錄后才能下訂單哦!
這篇文章主要介紹“怎么使用python繪制神器五角星+小黃人+櫻花”,在日常操作中,相信很多人在怎么使用python繪制神器五角星+小黃人+櫻花問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”怎么使用python繪制神器五角星+小黃人+櫻花”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!
import turtle def draw_recursive_pentargram(size): ''' 迭代繪制五角星 ''' count = 1 while count <= 5: turtle.backward(size) turtle.right(144) count += 1 # 五角星繪制完成,更新參數(shù) size += 10 if size <= 100: draw_recursive_pentargram(size) def main(): ''' 主函數(shù) ''' turtle.speed(0) # turtle.penup() turtle.forward(40) # turtle.pendown() turtle.pensize(2) turtle.pencolor("red") turtle.bgcolor('black') size = 50 draw_recursive_pentargram(size) turtle.exitonclick() if __name__ == "__main__": main()
是不是感覺挺神奇的,其實(shí)這個算法是最簡單的。下面我就讓大家來見識一下它的真正的本領(lǐng),感性的魅力。
這個代碼我是去向大師借來的喲,不是小王自己寫的,嘻嘻嘻。
# 小黃人繪制案例************************************************************************ import turtle t = turtle.Turtle() wn = turtle.Screen() turtle.colormode(255) t.hideturtle() t.speed(10) t.penup() t.pensize(4) t.goto(100,0) t.pendown() t.left(90) t.color((0,0,0),(255,255,0)) #身體繪制上色 t.begin_fill() t.forward(200) t.circle(100,180) t.forward(200) t.circle(100,180) t.end_fill() #右眼睛繪制上色 t.pensize(12) t.penup() t.goto(-100,200) t.pendown() t.right(100) t.circle(500,23) t.pensize(3) t.penup() t.goto(0,200) t.pendown() t.seth(270) t.color("black","white") t.begin_fill() t.circle(30) t.end_fill() t.penup() t.goto(15,200) t.pendown() t.color("black","black") t.begin_fill() t.circle(15) t.end_fill() t.penup() t.goto(35,205) t.color("black","white") t.begin_fill() t.circle(5) t.end_fill() #左眼睛繪制上色 t.pensize(3) t.penup() t.goto(0,200) t.pendown() t.seth(90) t.color("black","white") t.begin_fill() t.circle(30) t.end_fill() t.penup() t.goto(-15,200) t.pendown() t.color("black","black") t.begin_fill() t.circle(15) t.end_fill() t.penup() t.goto(-35,205) t.color("black","white") t.begin_fill() t.circle(5) t.end_fill() #嘴繪制上色 t.penup() t.goto(-20,100) t.pendown() t.seth(270) t.color("black","white") t.begin_fill() t.circle(20,180) t.left(90) t.forward(40) t.end_fill() #褲子繪制上色 t.penup() t.goto(-100,0) t.pendown() t.seth(0) t.color("black","blue") t.begin_fill() t.forward(20) t.left(90) t.forward(40) t.right(90) t.forward(160) t.right(90) t.forward(40) t.left(90) t.forward(20) t.seth(270) t.penup() t.goto(-100,0) t.circle(100,180) t.end_fill() #左褲子腰帶 t.penup() t.goto(-70,20) t.pendown() t.color("black","blue") t.begin_fill() t.seth(45) t.forward(15) t.left(90) t.forward(60) t.seth(270) t.forward(15) t.left(40) t.forward(50) t.end_fill() t.left(180) t.goto(-70,30) t.dot() #右褲腰帶 t.penup() t.goto(70,20) t.pendown() t.color("black","blue") t.begin_fill() t.seth(135) t.forward(15) t.right(90) t.forward(60) t.seth(270) t.forward(15) t.right(40) t.forward(50) t.end_fill() t.left(180) t.goto(70,30) t.dot() #腳 t.penup() t.goto(4,-100) t.pendown() t.seth(270) t.color("black","black") t.begin_fill() t.forward(30) t.left(90) t.forward(40) t.seth(20) t.circle(10,180) t.circle(400,2) t.seth(90) t.forward(20) t.goto(4,-100) t.end_fill() t.penup() t.goto(-4,-100) t.pendown() t.seth(270) t.color("black","black") t.begin_fill() t.forward(30) t.right(90) t.forward(40) t.seth(20) t.circle(10,-225) t.circle(400,-3) t.seth(90) t.forward(21) t.goto(-4,-100) t.end_fill() #左手 t.penup() t.goto(-100,50) t.pendown() t.seth(225) t.color("black","yellow") t.begin_fill() t.forward(40) t.left(90) t.forward(35) t.seth(90) t.forward(50) t.end_fill() #右手 t.penup() t.goto(100,50) t.pendown() t.seth(315) t.color("black","yellow") t.begin_fill() t.forward(40) t.right(90) t.forward(36) t.seth(90) t.forward(50) t.end_fill() # t.penup() t.goto(0,-100) t.pendown() t.forward(30) # t.penup() t.goto(0,-20) t.pendown() t.color("yellow") t.begin_fill() t.seth(45) t.forward(20) t.circle(10,180) t.right(90) t.circle(10,180) t.forward(20) t.end_fill() # t.penup() t.color("black") t.goto(-100,-20) t.pendown() t.circle(30,90) t.penup() t.goto(100,-20) t.pendown() t.circle(30,-90) #頭頂 t.penup() t.goto(2,300) t.pendown() t.begin_fill() t.seth(135) t.circle(100,40) t.end_fill() t.penup() t.goto(2,300) t.pendown() t.begin_fill() t.seth(45) t.circle(100,40) t.end_fill() turtle.exitonclick()
看似復(fù)雜而又繁瑣的代碼,它其實(shí)是由規(guī)律的,不信你可以自己看看它的寫法。
運(yùn)行美圖:
小黃人不錯吧!?。?/p>
import turtle as T import random import time # 畫櫻花的軀干(60,t) def Tree(branch, t): time.sleep(0.0005) if branch > 3: if 8 <= branch <= 12: if random.randint(0, 2) == 0: t.color('snow') # 白 else: t.color('lightcoral') # 淡珊瑚色 t.pensize(branch / 3) elif branch < 8: if random.randint(0, 1) == 0: t.color('snow') else: t.color('lightcoral') # 淡珊瑚色 t.pensize(branch / 2) else: t.color('sienna') # 赭(zhě)色 t.pensize(branch / 10) # 6 t.forward(branch) a = 1.5 * random.random() t.right(20 * a) b = 1.5 * random.random() Tree(branch - 10 * b, t) t.left(40 * a) Tree(branch - 10 * b, t) t.right(20 * a) t.up() t.backward(branch) t.down() # 掉落的花瓣 def Petal(m, t): for i in range(m): a = 200 - 400 * random.random() b = 10 - 20 * random.random() t.up() t.forward(b) t.left(90) t.forward(a) t.down() t.color('lightcoral') # 淡珊瑚色 t.circle(1) t.up() t.backward(a) t.right(90) t.backward(b) # 繪圖區(qū)域 t = T.Turtle() # 畫布大小 w = T.Screen() t.hideturtle() # 隱藏畫筆 t.getscreen().tracer(5, 0) w.screensize(bg='wheat') # wheat小麥 t.left(90) t.up() t.backward(150) t.down() t.color('sienna') # 畫櫻花的軀干 Tree(60, t) # 掉落的花瓣 Petal(200, t) w.exitonclick()
到此,關(guān)于“怎么使用python繪制神器五角星+小黃人+櫻花”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注億速云網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。