python繪制神器五角星+小黃人+櫻花
前言
這期文章我就為大家介紹關(guān)于python的藝術(shù)美——畫圖神器
在數(shù)學(xué)中有一種美,叫幾何美,我們又稱為藝術(shù)美,用具有規(guī)律的線條,迭代出美麗的圖片,這就是許多藝術(shù)家在創(chuàng)作是的靈感渠道。下面我們就為大家準(zhǔn)備了一個(gè)簡(jiǎn)單的五角星繪制。
涉及第三庫(kù)的導(dǎo)入,模塊的導(dǎo)入,還有一些我想我現(xiàn)在也介紹不完,下面就讓我們一起來(lái)領(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()
是不是感覺(jué)挺神奇的,其實(shí)這個(gè)算法是最簡(jiǎn)單的。下面我就讓大家來(lái)見(jiàn)識(shí)一下它的真正的本領(lǐng),感性的魅力。
這個(gè)代碼我是去向大師借來(lái)的喲,不是小王自己寫的,嘻嘻嘻。
小黃人繪制案例
# 小黃人繪制案例************************************************************************
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()
喜歡的小伙伴可以去運(yùn)行一下,再次聲明一下,上面兩個(gè)復(fù)雜算法,是其他的人寫的。
到此這篇關(guān)于python繪制神器五角星+小黃人+櫻花的文章就介紹到這了,更多相關(guān)python繪制內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
帶你學(xué)習(xí)Python如何實(shí)現(xiàn)回歸樹(shù)模型
這篇文章主要介紹了Python如何實(shí)現(xiàn)回歸樹(shù)模型,文中講解非常細(xì)致,幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下2020-07-07
python數(shù)據(jù)可視化之初探?Seaborn
Seaborn?是一個(gè)基于?Matplotlib?的?Python?數(shù)據(jù)可視化庫(kù),它提供了更高級(jí)別的接口,使得創(chuàng)建美觀的統(tǒng)計(jì)圖形變得非常簡(jiǎn)單,在這篇文章中,我們將討論?Seaborn?的基礎(chǔ)使用方法,包括如何創(chuàng)建各種常見(jiàn)的統(tǒng)計(jì)圖形2023-07-07
PyTorch中的nn.ConvTranspose2d模塊詳解
nn.ConvTranspose2d是PyTorch中用于實(shí)現(xiàn)二維轉(zhuǎn)置卷積的模塊,廣泛應(yīng)用于生成對(duì)抗網(wǎng)絡(luò)(GANs)和卷積神經(jīng)網(wǎng)絡(luò)(CNNs)的解碼器中。該模塊通過(guò)參數(shù)如輸入輸出通道數(shù)、卷積核大小、步長(zhǎng)、填充等,能控制輸出尺寸和避免棋盤效應(yīng)2024-09-09
python3實(shí)現(xiàn)字符串的全排列的方法(無(wú)重復(fù)字符)
這篇文章主要介紹了python3實(shí)現(xiàn)字符串的全排列的方法(無(wú)重復(fù)字符),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-07-07
利用python中集合的唯一性實(shí)現(xiàn)去重
集合,用{ }表示,集合中所有元素具有唯一性。這篇文章給大家介紹利用python中集合的唯一性實(shí)現(xiàn)去重,感興趣的朋友一起看看吧2020-02-02
pytorch加載語(yǔ)音類自定義數(shù)據(jù)集的方法教程
這篇文章主要給大家介紹了關(guān)于pytorch加載語(yǔ)音類自定義數(shù)據(jù)集的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11
python正則表達(dá)式抓取成語(yǔ)網(wǎng)站
做NLPproject時(shí)需要一個(gè)成語(yǔ)庫(kù),我需要的是純成語(yǔ),網(wǎng)上找的都是有詳細(xì)解釋的。于是自己寫了一個(gè)爬成語(yǔ)的python程序2013-11-11
分享一個(gè)可以生成各種進(jìn)制格式IP的小工具實(shí)例代碼
這篇文章主要給大家分享了一個(gè)可以生成各種進(jìn)制格式IP的小工具,利用的語(yǔ)言是python實(shí)現(xiàn)的一個(gè)小工具,這個(gè)小工具對(duì)大家的日常使用與開(kāi)發(fā)具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面跟著小編來(lái)一起看看吧。2017-07-07

