基于Python繪制三種不同的中國(guó)結(jié)
前言
今天就來(lái)分享幾個(gè)用python繪制的圖案吧
馬上就要迎來(lái)新年了 就繪制了幾個(gè)中國(guó)結(jié),嘿嘿
話不多說(shuō),直接展示一下代碼和效果圖吧
示例一
效果圖

代碼展示
import turtle
turtle.screensize(600,800)
turtle.pensize(10)
turtle.pencolor("red")
turtle.seth(-45)
turtle.fd(102)
turtle.circle(-6,180)
turtle.fd(102)
turtle.circle(6,180)
turtle.fd(102)
turtle.circle(-6,180)
turtle.fd(102)
turtle.circle(6,180)
turtle.fd(102)
turtle.circle(-6,180)
turtle.fd(102)
turtle.circle(6,180)
turtle.fd(92)
turtle.circle(-6,270)
turtle.fd(92)
turtle.circle(6,180)
turtle.fd(102)
turtle.circle(-6,180)
turtle.fd(102)
turtle.circle(6,180)
turtle.fd(102)
turtle.circle(-6,180)
turtle.fd(102)
turtle.circle(6,180)
turtle.fd(102)
turtle.circle(-6,180)
turtle.fd(88)
turtle.fd(20)
turtle.seth(135)
turtle.fd(20)
turtle.seth(225)
turtle.fd(20)
turtle.seth(315)
turtle.fd(20)
turtle.seth(45)
turtle.fd(20)
turtle.seth(135)
turtle.begin_fill()
turtle.fillcolor("red")
turtle.fd(50)
turtle.seth(45)
turtle.fd(30)
turtle.seth(-45)
turtle.fd(30)
turtle.seth(225)
turtle.fd(30)
turtle.end_fill()
turtle.seth(90)
turtle.fd(40)
turtle.pensize(20)
turtle.fd(10)
turtle.pensize(5)
turtle.seth(105)
turtle.fd(30)
turtle.circle(-8,240)
turtle.circle(20,20)
turtle.fd(5)
turtle.circle(20,60)
turtle.fd(25)
turtle.penup()
turtle.setx(0)
turtle.sety(0)
turtle.goto(2,-127)
turtle.pendown()
turtle.pensize(5)
turtle.begin_fill()
turtle.fillcolor("red")
turtle.seth(0)
turtle.fd(15)
turtle.seth(-90)
turtle.fd(10)
turtle.seth(180)
turtle.fd(15)
turtle.seth(90)
turtle.fd(10)
turtle.seth(0)
turtle.fd(15)
turtle.end_fill()
turtle.pensize(2)
for x in range(6):
turtle.seth(-90)
turtle.fd(50)
turtle.penup()
turtle.seth(90)
turtle.fd(50)
turtle.seth(180)
turtle.fd(3)
turtle.pendown()
???????
turtle.done()示例二
效果圖

如圖所示 在代碼里找到文字所在的地方,自己進(jìn)行一下編輯,是可以加自己想加的話進(jìn)去的
代碼展示
import turtle as t
def goto(x,y):
t.penup()
t.goto(x,y)
t.pendown()
def init():
t.setup(800,800)
t.pensize(10)
t.pencolor("red")
t.speed(14)
def jiexin():
m,n=0,200
for i in range(11):
goto(m,n)
t.seth(-45)
t.fd(200)
m-=20/pow(2,0.5)
n-=20/pow(2,0.5)
m,n=0,200
for j in range(11):
goto(m,n)
t.seth(-135)
t.fd(200)
m+=20/pow(2,0.5)
n-=20/pow(2,0.5)
def jiexiaoban():
m=-20/pow(2,0.5)
n=200-20/pow(2,0.5)
for k in range(4):
goto(m,n)
t.seth(135)
t.fd(20)
t.circle(10,180)
t.fd(20)
m-=40/pow(2,0.5)
n-=40/pow(2,0.5)
m=20/pow(2,0.5)
n=200-20/pow(2,0.5)
for k in range(4):
goto(m,n)
t.seth(45)
t.fd(20)
t.circle(-10,180)
t.fd(20)
m+=40/pow(2,0.5)
n-=40/pow(2,0.5)
m=20/pow(2,0.5)
n=200-200*pow(2,0.5)+20/pow(2,0.5)
for k in range(4):
goto(m,n)
t.seth(-45)
t.fd(20)
t.circle(10,180)
t.fd(20)
m+=40/pow(2,0.5)
n+=40/pow(2,0.5)
m=-20/pow(2,0.5)
n=200-200*pow(2,0.5)+20/pow(2,0.5)
for k in range(4):
goto(m,n)
t.seth(-135)
t.fd(20)
t.circle(-10,180)
t.fd(20)
m-=40/pow(2,0.5)
n+=40/pow(2,0.5)
def waiyuan():
goto(90*pow(2,0.5),200-110*pow(2,0.5))
t.seth(-45)
t.circle(20,270)
goto(-90*pow(2,0.5),200-110*pow(2,0.5))
t.seth(-135)
t.circle(-20,270)
goto(80*pow(2,0.5),200-120*pow(2,0.5))
t.seth(-45)
t.circle(40,270)
goto(-80*pow(2,0.5),200-120*pow(2,0.5))
t.seth(-135)
t.circle(-40,270)
def shengzi():
goto(0,200)
t.pensize(20)
t.seth(90)
t.fd(60)
goto(0,320)
t.pensize(12)
t.seth(180)
t.circle(30,360)
def hanzi():
goto(-150,325)
t.write("Python",font=("Arial",40,"normal"))
def main():
init()
jiexin()
jiexiaoban()
waiyuan()
shengzi()
hanzi()
t.hideturtle()
t.done()
main()示例三
效果圖

這個(gè)其實(shí)跟第二個(gè)差不多 就是沒(méi)有上面那行字體
代碼展示
import turtle as t
from math import sqrt
class chineseKnot:
def __init__(self) -> None:
# 畫筆初始化
self.t = t
self.t.pensize(10)
self.t.setup(700, 700)
self.t.pencolor("red")
self.t.speed(14)
# 結(jié)心坐標(biāo)
self.x = 0
self.y = 200
def drawKnot(self) -> None:
self.drawBody()
self.drawEdge()
self.drawAdorn()
self.t.hideturtle()
self.t.done()
def drawBody(self) -> None:
for i in range(11):
self.__goto(self.x - i * 10 * sqrt(2), self.y - i * 10 * sqrt(2))
self.t.seth(-45)
self.t.fd(200)
self.__goto(self.x + i * 10 * sqrt(2), self.y - i * 10 * sqrt(2))
self.t.seth(-135)
self.t.fd(200)
def drawEdge(self) -> None:
for i in range(4):
# 左上角
self.__goto(-10 * sqrt(2) - i * 20 * sqrt(2),
200 - 10 * sqrt(2) - i * 20 * sqrt(2))
self.t.seth(135)
self.t.fd(20)
self.t.circle(10, 180)
self.t.fd(20)
# 右上角
self.__goto(10 * sqrt(2) + i * 20 * sqrt(2),
200 - 10 * sqrt(2) - i * 20 * sqrt(2))
self.t.seth(45)
self.t.fd(20)
self.t.circle(-10, 180)
self.t.fd(20)
# 左下角
self.__goto(-10 * sqrt(2) - i * 20 * sqrt(2),
200 - 190 * sqrt(2) + i * 20 * sqrt(2))
self.t.seth(-135)
self.t.fd(20)
self.t.circle(-10, 180)
self.t.fd(20)
# 右下角
self.__goto(10 * sqrt(2) + i * 20 * sqrt(2),
200 - 190 * sqrt(2) + i * 20 * sqrt(2))
self.t.seth(-45)
self.t.fd(20)
self.t.circle(10, 180)
self.t.fd(20)
# 左側(cè)
self.t.seth(-45)
self.__goto(90 * sqrt(2), 200 - 110 * sqrt(2))
self.t.circle(20, 270)
self.__goto(-90 * sqrt(2), 200 - 110 * sqrt(2))
self.t.circle(-20, 270)
# 右側(cè)
self.__goto(80 * sqrt(2), 200 - 120 * sqrt(2))
self.t.circle(40, 270)
self.__goto(-80 * sqrt(2), 200 - 120 * sqrt(2))
self.t.circle(-40, 270)
def drawAdorn(self):
# 上側(cè)
self.__goto(self.x, self.y)
self.t.pensize(14)
self.t.seth(90)
self.t.fd(60)
self.__goto(0, 320)
self.t.seth(180)
self.t.circle(30, 360)
def __goto(self, x: int, y: int) -> None:
self.t.penup()
self.t.goto(x, y)
self.t.pendown()
if __name__ == '__main__':
knot = chineseKnot()
knot.drawKnot()到此這篇關(guān)于基于Python繪制三種不同的中國(guó)結(jié)的文章就介紹到這了,更多相關(guān)Python繪制中國(guó)結(jié)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python中動(dòng)態(tài)創(chuàng)建類實(shí)例的方法
在Java中我們可以通過(guò)反射來(lái)根據(jù)類名創(chuàng)建類實(shí)例,那么在Python我們?cè)趺磳?shí)現(xiàn)類似功能呢?其實(shí)在Python有一個(gè)builtin函數(shù)import,我們可以使用這個(gè)函數(shù)來(lái)在運(yùn)行時(shí)動(dòng)態(tài)加載一些模塊2017-03-03
使用Python快速打開(kāi)一個(gè)百萬(wàn)行級(jí)別的超大Excel文件的方法
這篇文章主要介紹了使用Python快速打開(kāi)一個(gè)百萬(wàn)行級(jí)別的超大Excel文件的方法,本文通過(guò)實(shí)例代碼給大家介紹的非常想詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-03-03
Python if語(yǔ)句知識(shí)點(diǎn)用法總結(jié)
本篇文章給python初學(xué)者總結(jié)了關(guān)于Python之if語(yǔ)句的相關(guān)用法以及知識(shí)點(diǎn)總結(jié),跟著學(xué)習(xí)下吧。2018-06-06
django echarts餅圖數(shù)據(jù)動(dòng)態(tài)加載的實(shí)例
今天小編就為大家分享一篇django echarts餅圖數(shù)據(jù)動(dòng)態(tài)加載的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-08-08
Python 使用 environs 庫(kù)定義環(huán)境變量的方法
這篇文章主要介紹了Python 使用 environs 庫(kù)來(lái)更好地定義環(huán)境變量,本節(jié)我們以 Python 項(xiàng)目為例,說(shuō)說(shuō)環(huán)境變量的設(shè)置。通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-02-02
Python3.9 beta2版本發(fā)布了,看看這7個(gè)新的PEP都是什么
這篇文章主要介紹了Python3.9 beta2版本發(fā)布了,看看這7個(gè)新的PEP都是什么,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2020-06-06
Python爬蟲(chóng)之requests庫(kù)基本介紹
大家好,本篇文章主要講的是Python爬蟲(chóng)之requests庫(kù)基本介紹,感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話記得收藏一下2022-02-02
Python 2與Python 3版本和編碼的對(duì)比
這篇文章主要介紹了Python 2與Python 3版本和編碼的對(duì)比,文中介紹的很詳細(xì),需要的朋友可以參考借鑒,下面來(lái)一起看看吧。2017-02-02
Python?實(shí)現(xiàn)驅(qū)動(dòng)AI機(jī)器人
這篇文章主要介紹了Python?實(shí)現(xiàn)驅(qū)動(dòng)AI機(jī)器人,下文圍繞利用Python?實(shí)現(xiàn)驅(qū)動(dòng)AI機(jī)器人的相關(guān)資料展開(kāi)內(nèi)容,需要的小伙伴可以參考一下2022-02-02

