小編寫這篇文章的主要講解的是,關(guān)于python一些案例的介紹,主要是對(duì)五角星+小黃人+櫻花,對(duì)這種方法的一個(gè)具體介紹,下面就給大家做出一個(gè)比較詳細(xì)解答。
前言
這期文章我就為大家介紹關(guān)于python的藝術(shù)美——畫圖神器
在數(shù)學(xué)中有一種美,叫幾何美,我們又稱為藝術(shù)美,用具有規(guī)律的線條,迭代出美麗的圖片,這就是許多藝術(shù)家在創(chuàng)作是的靈感渠道。下面我們就為大家準(zhǔn)備了一個(gè)簡(jiǎn)單的五角星繪制。
涉及第三庫的導(dǎo)入,模塊的導(dǎo)入,還有一些我想我現(xiàn)在也介紹不完,下面就讓我們一起來領(lǐng)略美感吧!
繪制五角星
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í)這個(gè)算法是最簡(jiǎn)單的。下面我就讓大家來見識(shí)一下它的真正的本領(lǐng),感性的魅力。
這個(gè)代碼我是去向大師借來的喲,不是小陳自己寫的,嘻嘻嘻。
小黃人繪制案例
#小黃人繪制案例************************************************************************ 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)行美圖:
小黃人不錯(cuò)吧?。。?/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)于一些知識(shí)要點(diǎn),就為大家介紹到這里了,希望可以為大家?guī)砀嗟膸椭?/p>
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/128180.html
摘要:用的模塊繪制國(guó)旗小黃繪制矩形繪制五角星主程序畫國(guó)旗主體畫大星星畫小星星隱藏海龜顯示繪圖窗口 用Python的turtle模塊繪制國(guó)旗Version: 0.1Author: 小黃Date: 2021.11.22import turtledef draw_rectangle(x, y, width, height)...
摘要:層疊即表示允許以多種方式來描述樣式,一個(gè)元素可以被渲染呈現(xiàn)出多種樣式??梢宰寣傩缘淖兓^程持續(xù)一段時(shí)間,而不是立即生效。比如,將元素的顏色從白色改為黑色,通常這個(gè)改變是立即生效的,使用后,將按一個(gè)曲線速率變化。 showImg(https://segmentfault.com/img/bVZwyL?w=900&h=385); CSS 的全稱是 Cascading Style Sheet...
摘要:基礎(chǔ)概念畫布畫布就是為我們展開用于繪圖區(qū)域我們可以設(shè)置它的大小和初始位置。常用的畫布方法有兩個(gè)和。設(shè)置畫筆的寬度沒有參數(shù)傳入返回當(dāng)前畫筆顏色傳入?yún)?shù)設(shè)置畫筆顏色可以是字符串如也可以是元組。 Turtle庫是Python語言中一個(gè)很流行的繪制圖像的函數(shù)庫,想象一個(gè)小烏龜,在一個(gè)橫軸為x、縱軸為y的坐標(biāo)系原點(diǎn),(0,0)位置開始,它根據(jù)一組函數(shù)指令的控制,在這個(gè)平面坐標(biāo)系中移動(dòng),從而在它爬...
摘要:但今年不能老送同樣的東西啊,那就給大家送上幾棵圣誕樹吧。極簡(jiǎn)版這個(gè)可算是最簡(jiǎn)單的圣誕樹了。例如上面這棵圣誕樹,每一個(gè)樹枝又是一個(gè)小的圣誕樹。這與編程中的遞歸思想很像頂部五角星略過炫彩版一般圣誕樹上都會(huì)掛上的小彩燈。 今天是圣誕節(jié),先祝大家圣誕快樂! 有人要說了,圣誕節(jié)是耶穌誕生的日子,我又不信基督教,有啥好慶祝的。這你就有所不知了,Python 的誕生也跟圣誕節(jié)有關(guān):1989 年,那是...
閱讀 928·2023-01-14 11:38
閱讀 902·2023-01-14 11:04
閱讀 759·2023-01-14 10:48
閱讀 2065·2023-01-14 10:34
閱讀 968·2023-01-14 10:24
閱讀 844·2023-01-14 10:18
閱讀 512·2023-01-14 10:09
閱讀 591·2023-01-14 10:02