今天就来分享几个用python绘制的图案吧
马上就要迎来新年了 就绘制了几个中国结,嘿嘿
话不多说,直接展示一下代码和效果图吧
效果图:

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()
效果图:
如图所示 在代码里找到文字所在的地方,自己进行一下编辑,是可以加自己想加的话进去的

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,200for 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,200for 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()
效果图:
这个其实跟第二个差不多 就是没有上面那行字体

import turtle as t
from math import sqrtclass chineseKnot:def __init__(self) -> None:# 画笔初始化self.t = tself.t.pensize(10)self.t.setup(700, 700)self.t.pencolor("red")self.t.speed(14)# 结心坐标self.x = 0self.y = 200def 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)# 左侧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)# 右侧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):# 上侧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()
朋友们学习python,有时候不知道怎么学,从哪里开始学。掌握了基本的一些语法或者做了两个案例后,不知道下一步怎么走,不知道如何去学习更加高深的知识。
那么对于这些朋友们,我准备了大量的免费视频教程,PDF电子书籍,以及源代码!直接在文末名片自取即可哦!
今天的分享到这里就结束了
顺便给大家推荐一些Python视频教程,希望对大家有所帮助:
Python零基础教学合集
对文章有问题的,或者有其他关于python的问题,可以在评论区留言或者私信我哦
觉得我分享的文章不错的话,可以关注一下我,或者给文章点赞(/≧▽≦)/