python画爱心树(使用Python怎么绘制一个爱心树)

发布时间:2025-12-11 02:37:46 浏览次数:2

1、爱心树

importturtleimportrandomdeflove(x,y):#在(x,y)处画爱心lalalalv=turtle.Turtle()lv.hideturtle()lv.up()lv.goto(x,y)#定位到(x,y)defcurvemove():#画圆弧foriinrange(20):lv.right(10)lv.forward(2)lv.color('red','pink')lv.speed(100)lv.pensize(1)#开始画爱心lalalalv.down()lv.begin_fill()lv.left(140)lv.forward(22)curvemove()lv.left(120)curvemove()lv.forward(22)lv.write("杨幂",font=("Arial",12,"normal"),align="center")#写上表白的人的名字lv.left(140)#画完复位lv.end_fill()deftree(branchLen,t):ifbranchLen>5:#剩余树枝太少要结束递归ifbranchLen<20:t.color("green")t.pensize(random.uniform((branchLen+5)/4-2,(branchLen+6)/4+5))t.down()t.forward(branchLen)love(t.xcor(),t.ycor())#传输现在turtle的坐标t.up()t.backward(branchLen)t.color("brown")returnt.pensize(random.uniform((branchLen+5)/4-2,(branchLen+6)/4+5))t.down()t.forward(branchLen)#以下递归ang=random.uniform(15,45)t.right(ang)tree(branchLen-random.uniform(12,16),t)#随机决定减小长度t.left(2*ang)tree(branchLen-random.uniform(12,16),t)#随机决定减小长度t.right(ang)t.up()t.backward(branchLen)myWin=turtle.Screen()t=turtle.Turtle()t.hideturtle()t.speed(1000)t.left(90)t.up()t.backward(200)t.down()t.color("brown")t.pensize(32)t.forward(60)tree(100,t)myWin.exitonclick()

2、画桃心

#-*-coding:utf-8-*-importturtleimporttime#画爱心的顶部defLittleHeart():foriinrange(200):turtle.right(1)turtle.forward(2)#输入表白的语句,默认ILoveyoulove=input('请输入表白语句,默认为输入为"ILoveyou":')#输入署名或者赠谁,没有不执行me=input('请输入您心上人的姓名或者昵称:')iflove=='':love='ILoveyou'#窗口大小turtle.setup(width=800,height=500)#颜色turtle.color('red','pink')#笔粗细turtle.pensize(5)#速度turtle.speed(1)#提笔turtle.up()#隐藏笔turtle.hideturtle()#去到的坐标,窗口中心为0,0turtle.goto(0,-180)turtle.showturtle()#画上线turtle.down()turtle.speed(1)turtle.begin_fill()turtle.left(140)turtle.forward(224)#调用画爱心左边的顶部LittleHeart()#调用画爱右边的顶部turtle.left(120)LittleHeart()#画下线turtle.forward(224)turtle.end_fill()turtle.pensize(5)turtle.up()turtle.hideturtle()#在心中写字一次turtle.goto(0,0)turtle.showturtle()turtle.color('#CD5C5C','pink')#在心中写字font可以设置字体自己电脑有的都可以设align开始写字的位置turtle.write(love,font=('gungsuh',30,),align="center")turtle.up()turtle.hideturtle()time.sleep(2)#在心中写字二次turtle.goto(0,0)turtle.showturtle()turtle.color('red','pink')turtle.write(love,font=('gungsuh',30,),align="center")turtle.up()turtle.hideturtle()#写署名ifme!='':turtle.color('black','pink')time.sleep(2)turtle.goto(180,-180)turtle.showturtle()turtle.write(me,font=(20,),align="center",move=True)#点击窗口关闭window=turtle.Screen()window.exitonclick()

3、一箭穿心

importturtledefgetPosition(x,y):turtle.setx(x)turtle.sety(y)print(x,y)classPikachu:def__init__(self):self.t=turtle.Turtle()t=self.tt.pensize(3)t.speed(9)t.ondrag(getPosition)defnoTrace_goto(self,x,y):self.t.penup()self.t.goto(x,y)self.t.pendown()defleftEye(self,x,y):self.noTrace_goto(x,y)t=self.tt.seth(0)t.fillcolor('#333333')t.begin_fill()t.circle(22)t.end_fill()self.noTrace_goto(x,y+10)t.fillcolor('#000000')t.begin_fill()t.circle(10)t.end_fill()self.noTrace_goto(x+6,y+22)t.fillcolor('#ffffff')t.begin_fill()t.circle(10)t.end_fill()defrightEye(self,x,y):self.noTrace_goto(x,y)t=self.tt.seth(0)t.fillcolor('#333333')t.begin_fill()t.circle(22)t.end_fill()self.noTrace_goto(x,y+10)t.fillcolor('#000000')t.begin_fill()t.circle(10)t.end_fill()self.noTrace_goto(x-6,y+22)t.fillcolor('#ffffff')t.begin_fill()t.circle(10)t.end_fill()defmouth(self,x,y):self.noTrace_goto(x,y)t=self.tt.fillcolor('#88141D')t.begin_fill()#下嘴唇l1=[]l2=[]t.seth(190)a=0.7foriinrange(28):a+=0.1t.right(3)t.fd(a)l1.append(t.position())self.noTrace_goto(x,y)t.seth(10)a=0.7foriinrange(28):a+=0.1t.left(3)t.fd(a)l2.append(t.position())#上嘴唇t.seth(10)t.circle(50,15)t.left(180)t.circle(-50,15)t.circle(-50,40)t.seth(233)t.circle(-50,55)t.left(180)t.circle(50,12.1)t.end_fill()#舌头self.noTrace_goto(17,54)t.fillcolor('#DD716F')t.begin_fill()t.seth(145)t.circle(40,86)t.penup()forposinreversed(l1[:20]):t.goto(pos[0],pos[1]+1.5)forposinl2[:20]:t.goto(pos[0],pos[1]+1.5)t.pendown()t.end_fill()#鼻子self.noTrace_goto(-17,94)t.seth(8)t.fd(4)t.back(8)#红脸颊defleftCheek(self,x,y):turtle.tracer(False)t=self.tself.noTrace_goto(x,y)t.seth(300)t.fillcolor('#DD4D28')t.begin_fill()a=2.3foriinrange(120):if0<=i<30or60<=i<90:a-=0.05t.lt(3)t.fd(a)else:a+=0.05t.lt(3)t.fd(a)t.end_fill()turtle.tracer(True)defrightCheek(self,x,y):t=self.tturtle.tracer(False)self.noTrace_goto(x,y)t.seth(60)t.fillcolor('#DD4D28')t.begin_fill()a=2.3foriinrange(120):if0<=i<30or60<=i<90:a-=0.05t.lt(3)t.fd(a)else:a+=0.05t.lt(3)t.fd(a)t.end_fill()turtle.tracer(True)defcolorLeftEar(self,x,y):t=self.tself.noTrace_goto(x,y)t.fillcolor('#000000')t.begin_fill()t.seth(330)t.circle(100,35)t.seth(219)t.circle(-300,19)t.seth(110)t.circle(-30,50)t.circle(-300,10)t.end_fill()defcolorRightEar(self,x,y):t=self.tself.noTrace_goto(x,y)t.fillcolor('#000000')t.begin_fill()t.seth(300)t.circle(-100,30)t.seth(35)t.circle(300,15)t.circle(30,50)t.seth(190)t.circle(300,17)t.end_fill()defbody(self):t=self.tt.fillcolor('#F6D02F')t.begin_fill()#右脸轮廓t.penup()t.circle(130,40)t.pendown()t.circle(100,105)t.left(180)t.circle(-100,5)#右耳朵t.seth(20)t.circle(300,30)t.circle(30,50)t.seth(190)t.circle(300,36)#上轮廓t.seth(150)t.circle(150,70)#左耳朵t.seth(200)t.circle(300,40)t.circle(30,50)t.seth(20)t.circle(300,35)#print(t.pos())#左脸轮廓t.seth(240)t.circle(105,95)t.left(180)t.circle(-105,5)#左手t.seth(210)t.circle(500,18)t.seth(200)t.fd(10)t.seth(280)t.fd(7)t.seth(210)t.fd(10)t.seth(300)t.circle(10,80)t.seth(220)t.fd(10)t.seth(300)t.circle(10,80)t.seth(240)t.fd(12)t.seth(0)t.fd(13)t.seth(240)t.circle(10,70)t.seth(10)t.circle(10,70)t.seth(10)t.circle(300,18)t.seth(75)t.circle(500,8)t.left(180)t.circle(-500,15)t.seth(250)t.circle(100,65)#左脚t.seth(320)t.circle(100,5)t.left(180)t.circle(-100,5)t.seth(220)t.circle(200,20)t.circle(20,70)t.seth(60)t.circle(-100,20)t.left(180)t.circle(100,20)t.seth(300)t.circle(10,70)t.seth(60)t.circle(-100,20)t.left(180)t.circle(100,20)t.seth(10)t.circle(100,60)#横向t.seth(180)t.circle(-100,10)t.left(180)t.circle(100,10)t.seth(5)t.circle(100,10)t.circle(-100,40)t.circle(100,35)t.left(180)t.circle(-100,10)#右脚t.seth(290)t.circle(100,55)t.circle(10,50)t.seth(120)t.circle(100,20)t.left(180)t.circle(-100,20)t.seth(0)t.circle(10,50)t.seth(110)t.circle(100,20)t.left(180)t.circle(-100,20)t.seth(30)t.circle(20,50)t.seth(100)t.circle(100,40)#右侧身体轮廓t.seth(200)t.circle(-100,5)t.left(180)t.circle(100,5)t.left(30)t.circle(100,75)t.right(15)t.circle(-300,21)t.left(180)t.circle(300,3)#右手t.seth(43)t.circle(200,60)t.right(10)t.fd(10)t.circle(5,160)t.seth(90)t.circle(5,160)t.seth(90)t.fd(10)t.seth(90)t.circle(5,180)t.fd(10)t.left(180)t.left(20)t.fd(10)t.circle(5,170)t.fd(10)t.seth(240)t.circle(50,30)t.end_fill()self.noTrace_goto(130,125)t.seth(-20)t.fd(5)t.circle(-5,160)t.fd(5)#手指纹self.noTrace_goto(166,130)t.seth(-90)t.fd(3)t.circle(-4,180)t.fd(3)t.seth(-90)t.fd(3)t.circle(-4,180)t.fd(3)#尾巴self.noTrace_goto(168,134)t.fillcolor('#F6D02F')t.begin_fill()t.seth(40)t.fd(200)t.seth(-80)t.fd(150)t.seth(210)t.fd(150)t.left(90)t.fd(100)t.right(95)t.fd(100)t.left(110)t.fd(70)t.right(110)t.fd(80)t.left(110)t.fd(30)t.right(110)t.fd(32)t.right(106)t.circle(100,25)t.right(15)t.circle(-300,2)###############print(t.pos())t.seth(30)t.fd(40)t.left(100)t.fd(70)t.right(100)t.fd(80)t.left(100)t.fd(46)t.seth(66)t.circle(200,38)t.right(10)t.fd(10)t.end_fill()#尾巴花纹t.fillcolor('#923E24')self.noTrace_goto(126.82,-156.84)t.begin_fill()t.seth(30)t.fd(40)t.left(100)t.fd(40)t.pencolor('#923e24')t.seth(-30)t.fd(30)t.left(140)t.fd(20)t.right(150)t.fd(20)t.left(150)t.fd(20)t.right(150)t.fd(20)t.left(130)t.fd(18)t.pencolor('#000000')t.seth(-45)t.fd(67)t.right(110)t.fd(80)t.left(110)t.fd(30)t.right(110)t.fd(32)t.right(106)t.circle(100,25)t.right(15)t.circle(-300,2)t.end_fill()#帽子、眼睛、嘴巴、脸颊self.cap(-134.07,147.81)self.mouth(-5,25)self.leftCheek(-126,32)self.rightCheek(107,63)self.colorLeftEar(-250,100)self.colorRightEar(140,270)self.leftEye(-85,90)self.rightEye(50,110)t.hideturtle()defcap(self,x,y):self.noTrace_goto(x,y)t=self.tt.fillcolor('#CD0000')t.begin_fill()t.seth(200)t.circle(400,7)t.left(180)t.circle(-400,30)t.circle(30,60)t.fd(50)t.circle(30,45)t.fd(60)t.left(5)t.circle(30,70)t.right(20)t.circle(200,70)t.circle(30,60)t.fd(70)#print(t.pos())t.right(35)t.fd(50)t.circle(8,100)t.end_fill()self.noTrace_goto(-168.47,185.52)t.seth(36)t.circle(-270,54)t.left(180)t.circle(270,27)t.circle(-80,98)t.fillcolor('#444444')t.begin_fill()t.left(180)t.circle(80,197)t.left(58)t.circle(200,45)t.end_fill()self.noTrace_goto(-58,270)t.pencolor('#228B22')t.dot(35)self.noTrace_goto(-30,280)t.fillcolor('#228B22')t.begin_fill()t.seth(100)t.circle(30,180)t.seth(190)t.fd(15)t.seth(100)t.circle(-45,180)t.right(90)t.fd(15)t.end_fill()t.pencolor('#000000')defstart(self):self.body()defmain():print('PaintingthePikachu...')turtle.screensize(800,600)turtle.title('Pikachu')pikachu=Pikachu()pikachu.start()turtle.mainloop()if__name__=='__main__':main()

看完上述内容,你们对使用Python怎么绘制一个爱心树有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注本站行业资讯频道,感谢大家的支持。

python画爱心树
需要做网站?需要网络推广?欢迎咨询客户经理 13272073477