小白教程

 找回密码
 立即注册
查看: 6540|回复: 1

小白教程:Python绘制的爱心树与表白代码

[复制链接]

176

主题

185

帖子

663

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
663
发表于 2021-4-9 12:36:46 | 显示全部楼层 |阅读模式
用的的开发工具为pycham,pycham也是广泛用于做Python开发的工具。运用的turtle库,当然了如果是安装了anaconda3这个库那更好,以为这里面有我们会做Python程序设计时用到的大部分的库,turtle它是python中一个绘制图像的函数库,可以用它来绘制很多的东西,比如简单的小黄人、玫瑰花等,这个库也可以生说是一只马良的神笔的吧。比如以下是为女朋友准备的小小的惊喜吧。
1、爱心树


  1. import turtle
  2. import random

  3. def love(x,y):#在(x,y)处画爱心lalala
  4.     lv=turtle.Turtle()

  5.     lv.hideturtle()

  6.     lv.up()

  7.     lv.goto(x,y)#定位到(x,y)

  8.     def curvemove():#画圆弧
  9.         for i in range(20):
  10.             lv.right(10)
  11.             lv.forward(2)
  12.     lv.color('red','pink')

  13.     lv.speed(100)

  14.     lv.pensize(1)

  15.     #开始画爱心lalala

  16.     lv.down()

  17.     lv.begin_fill()

  18.     lv.left(140)

  19.     lv.forward(22)

  20.     curvemove()

  21.     lv.left(120)

  22.     curvemove()

  23.     lv.forward(22)

  24.     lv.write("杨幂",font=("Arial",12,"normal"),align="center")#写上表白的人的名字

  25.     lv.left(140)#画完复位

  26.     lv.end_fill()

  27. def tree(branchLen,t):
  28.     if branchLen > 5:#剩余树枝太少要结束递归
  29.         if branchLen<20:

  30.             t.color("green")

  31.             t.pensize(random.uniform((branchLen + 5) / 4 - 2, (branchLen + 6) / 4 + 5))

  32.             t.down()

  33.             t.forward(branchLen)

  34.             love(t.xcor(),t.ycor())#传输现在turtle的坐标

  35.             t.up()

  36.             t.backward(branchLen)

  37.             t.color("brown")

  38.             return

  39.         t.pensize(random.uniform((branchLen+5)/4-2,(branchLen+6)/4+5))

  40.         t.down()

  41.         t.forward(branchLen)

  42.         # 以下递归

  43.         ang=random.uniform(15,45)

  44.         t.right(ang)

  45.         tree(branchLen-random.uniform(12,16),t)#随机决定减小长度

  46.         t.left(2*ang)

  47.         tree(branchLen-random.uniform(12,16),t)#随机决定减小长度

  48.         t.right(ang)

  49.         t.up()

  50.         t.backward(branchLen)

  51. myWin = turtle.Screen()

  52. t = turtle.Turtle()

  53. t.hideturtle()

  54. t.speed(1000)

  55. t.left(90)

  56. t.up()

  57. t.backward(200)

  58. t.down()

  59. t.color("brown")

  60. t.pensize(32)

  61. t.forward(60)

  62. tree(100,t)

  63. myWin.exitonclick()
复制代码

2、画桃心

  1. # -*- coding:utf-8 -*-
  2. import turtle
  3. import time


  4. # 画爱心的顶部
  5. def LittleHeart():
  6.     for i in range(200):
  7.         turtle.right(1)
  8.         turtle.forward(2)


  9. # 输入表白的语句,默认I Love you
  10. love = input('请输入表白语句,默认为输入为"I Love you": ')
  11. # 输入署名或者赠谁,没有不执行
  12. me = input('请输入您心上人的姓名或者昵称: ')
  13. if love == '':
  14.     love = 'I Love you'
  15. # 窗口大小
  16. turtle.setup(width=800, height=500)
  17. # 颜色
  18. turtle.color('red', 'pink')
  19. # 笔粗细
  20. turtle.pensize(5)
  21. # 速度
  22. turtle.speed(1)
  23. # 提笔
  24. turtle.up()
  25. # 隐藏笔
  26. turtle.hideturtle()
  27. # 去到的坐标,窗口中心为0,0
  28. turtle.goto(0, -180)
  29. turtle.showturtle()
  30. # 画上线
  31. turtle.down()
  32. turtle.speed(1)
  33. turtle.begin_fill()
  34. turtle.left(140)
  35. turtle.forward(224)
  36. # 调用画爱心左边的顶部
  37. LittleHeart()
  38. # 调用画爱右边的顶部
  39. turtle.left(120)
  40. LittleHeart()
  41. # 画下线
  42. turtle.forward(224)
  43. turtle.end_fill()
  44. turtle.pensize(5)
  45. turtle.up()
  46. turtle.hideturtle()
  47. # 在心中写字 一次
  48. turtle.goto(0, 0)
  49. turtle.showturtle()
  50. turtle.color('#CD5C5C', 'pink')
  51. # 在心中写字 font可以设置字体自己电脑有的都可以设 align开始写字的位置
  52. turtle.write(love, font=('gungsuh', 30,), align="center")
  53. turtle.up()
  54. turtle.hideturtle()
  55. time.sleep(2)
  56. # 在心中写字 二次
  57. turtle.goto(0, 0)
  58. turtle.showturtle()
  59. turtle.color('red', 'pink')
  60. turtle.write(love, font=('gungsuh', 30,), align="center")
  61. turtle.up()
  62. turtle.hideturtle()
  63. # 写署名
  64. if me != '':
  65.     turtle.color('black', 'pink')
  66.     time.sleep(2)
  67.     turtle.goto(180, -180)
  68.     turtle.showturtle()
  69.     turtle.write(me, font=(20,), align="center", move=True)

  70. # 点击窗口关闭
  71. window = turtle.Screen()
  72. window.exitonclick()
复制代码
3、一箭穿心

  1. import turtle


  2. def getPosition(x, y):
  3.     turtle.setx(x)
  4.     turtle.sety(y)
  5.     print(x, y)


  6. class Pikachu:

  7.     def __init__(self):
  8.         self.t = turtle.Turtle()
  9.         t = self.t
  10.         t.pensize(3)
  11.         t.speed(9)
  12.         t.ondrag(getPosition)

  13.     def noTrace_goto(self, x, y):
  14.         self.t.penup()
  15.         self.t.goto(x, y)
  16.         self.t.pendown()

  17.     def leftEye(self, x, y):
  18.         self.noTrace_goto(x, y)
  19.         t = self.t
  20.         t.seth(0)
  21.         t.fillcolor('#333333')
  22.         t.begin_fill()
  23.         t.circle(22)
  24.         t.end_fill()

  25.         self.noTrace_goto(x, y + 10)
  26.         t.fillcolor('#000000')
  27.         t.begin_fill()
  28.         t.circle(10)
  29.         t.end_fill()

  30.         self.noTrace_goto(x + 6, y + 22)
  31.         t.fillcolor('#ffffff')
  32.         t.begin_fill()
  33.         t.circle(10)
  34.         t.end_fill()

  35.     def rightEye(self, x, y):
  36.         self.noTrace_goto(x, y)
  37.         t = self.t
  38.         t.seth(0)
  39.         t.fillcolor('#333333')
  40.         t.begin_fill()
  41.         t.circle(22)
  42.         t.end_fill()

  43.         self.noTrace_goto(x, y + 10)
  44.         t.fillcolor('#000000')
  45.         t.begin_fill()
  46.         t.circle(10)
  47.         t.end_fill()

  48.         self.noTrace_goto(x - 6, y + 22)
  49.         t.fillcolor('#ffffff')
  50.         t.begin_fill()
  51.         t.circle(10)
  52.         t.end_fill()

  53.     def mouth(self, x, y):
  54.         self.noTrace_goto(x, y)
  55.         t = self.t

  56.         t.fillcolor('#88141D')
  57.         t.begin_fill()
  58.         # 下嘴唇
  59.         l1 = []
  60.         l2 = []
  61.         t.seth(190)
  62.         a = 0.7
  63.         for i in range(28):
  64.             a += 0.1
  65.             t.right(3)
  66.             t.fd(a)
  67.             l1.append(t.position())

  68.         self.noTrace_goto(x, y)

  69.         t.seth(10)
  70.         a = 0.7
  71.         for i in range(28):
  72.             a += 0.1
  73.             t.left(3)
  74.             t.fd(a)
  75.             l2.append(t.position())

  76.         # 上嘴唇

  77.         t.seth(10)
  78.         t.circle(50, 15)
  79.         t.left(180)
  80.         t.circle(-50, 15)

  81.         t.circle(-50, 40)
  82.         t.seth(233)
  83.         t.circle(-50, 55)
  84.         t.left(180)
  85.         t.circle(50, 12.1)
  86.         t.end_fill()

  87.         # 舌头
  88.         self.noTrace_goto(17, 54)
  89.         t.fillcolor('#DD716F')
  90.         t.begin_fill()
  91.         t.seth(145)
  92.         t.circle(40, 86)
  93.         t.penup()
  94.         for pos in reversed(l1[:20]):
  95.             t.goto(pos[0], pos[1] + 1.5)
  96.         for pos in l2[:20]:
  97.             t.goto(pos[0], pos[1] + 1.5)
  98.         t.pendown()
  99.         t.end_fill()

  100.         # 鼻子
  101.         self.noTrace_goto(-17, 94)
  102.         t.seth(8)
  103.         t.fd(4)
  104.         t.back(8)

  105.     # 红脸颊
  106.     def leftCheek(self, x, y):
  107.         turtle.tracer(False)
  108.         t = self.t
  109.         self.noTrace_goto(x, y)
  110.         t.seth(300)
  111.         t.fillcolor('#DD4D28')
  112.         t.begin_fill()
  113.         a = 2.3
  114.         for i in range(120):
  115.             if 0 <= i < 30 or 60 <= i < 90:
  116.                 a -= 0.05
  117.                 t.lt(3)
  118.                 t.fd(a)
  119.             else:
  120.                 a += 0.05
  121.                 t.lt(3)
  122.                 t.fd(a)
  123.         t.end_fill()
  124.         turtle.tracer(True)

  125.     def rightCheek(self, x, y):
  126.         t = self.t
  127.         turtle.tracer(False)
  128.         self.noTrace_goto(x, y)
  129.         t.seth(60)
  130.         t.fillcolor('#DD4D28')
  131.         t.begin_fill()
  132.         a = 2.3
  133.         for i in range(120):
  134.             if 0 <= i < 30 or 60 <= i < 90:
  135.                 a -= 0.05
  136.                 t.lt(3)
  137.                 t.fd(a)
  138.             else:
  139.                 a += 0.05
  140.                 t.lt(3)
  141.                 t.fd(a)
  142.         t.end_fill()
  143.         turtle.tracer(True)

  144.     def colorLeftEar(self, x, y):
  145.         t = self.t
  146.         self.noTrace_goto(x, y)
  147.         t.fillcolor('#000000')
  148.         t.begin_fill()
  149.         t.seth(330)
  150.         t.circle(100, 35)
  151.         t.seth(219)
  152.         t.circle(-300, 19)
  153.         t.seth(110)
  154.         t.circle(-30, 50)
  155.         t.circle(-300, 10)
  156.         t.end_fill()

  157.     def colorRightEar(self, x, y):
  158.         t = self.t
  159.         self.noTrace_goto(x, y)
  160.         t.fillcolor('#000000')
  161.         t.begin_fill()
  162.         t.seth(300)
  163.         t.circle(-100, 30)
  164.         t.seth(35)
  165.         t.circle(300, 15)
  166.         t.circle(30, 50)
  167.         t.seth(190)
  168.         t.circle(300, 17)
  169.         t.end_fill()

  170.     def body(self):
  171.         t = self.t

  172.         t.fillcolor('#F6D02F')
  173.         t.begin_fill()
  174.         # 右脸轮廓
  175.         t.penup()
  176.         t.circle(130, 40)
  177.         t.pendown()
  178.         t.circle(100, 105)
  179.         t.left(180)
  180.         t.circle(-100, 5)

  181.         # 右耳朵
  182.         t.seth(20)
  183.         t.circle(300, 30)
  184.         t.circle(30, 50)
  185.         t.seth(190)
  186.         t.circle(300, 36)

  187.         # 上轮廓
  188.         t.seth(150)
  189.         t.circle(150, 70)

  190.         # 左耳朵
  191.         t.seth(200)
  192.         t.circle(300, 40)
  193.         t.circle(30, 50)
  194.         t.seth(20)
  195.         t.circle(300, 35)
  196.         # print(t.pos())

  197.         # 左脸轮廓
  198.         t.seth(240)
  199.         t.circle(105, 95)
  200.         t.left(180)
  201.         t.circle(-105, 5)

  202.         # 左手
  203.         t.seth(210)
  204.         t.circle(500, 18)
  205.         t.seth(200)
  206.         t.fd(10)
  207.         t.seth(280)
  208.         t.fd(7)
  209.         t.seth(210)
  210.         t.fd(10)
  211.         t.seth(300)
  212.         t.circle(10, 80)
  213.         t.seth(220)
  214.         t.fd(10)
  215.         t.seth(300)
  216.         t.circle(10, 80)
  217.         t.seth(240)
  218.         t.fd(12)
  219.         t.seth(0)
  220.         t.fd(13)
  221.         t.seth(240)
  222.         t.circle(10, 70)
  223.         t.seth(10)
  224.         t.circle(10, 70)
  225.         t.seth(10)
  226.         t.circle(300, 18)

  227.         t.seth(75)
  228.         t.circle(500, 8)
  229.         t.left(180)
  230.         t.circle(-500, 15)
  231.         t.seth(250)
  232.         t.circle(100, 65)

  233.         # 左脚
  234.         t.seth(320)
  235.         t.circle(100, 5)
  236.         t.left(180)
  237.         t.circle(-100, 5)
  238.         t.seth(220)
  239.         t.circle(200, 20)
  240.         t.circle(20, 70)

  241.         t.seth(60)
  242.         t.circle(-100, 20)
  243.         t.left(180)
  244.         t.circle(100, 20)
  245.         t.seth(300)
  246.         t.circle(10, 70)

  247.         t.seth(60)
  248.         t.circle(-100, 20)
  249.         t.left(180)
  250.         t.circle(100, 20)
  251.         t.seth(10)
  252.         t.circle(100, 60)

  253.         # 横向
  254.         t.seth(180)
  255.         t.circle(-100, 10)
  256.         t.left(180)
  257.         t.circle(100, 10)
  258.         t.seth(5)
  259.         t.circle(100, 10)
  260.         t.circle(-100, 40)
  261.         t.circle(100, 35)
  262.         t.left(180)
  263.         t.circle(-100, 10)

  264.         # 右脚
  265.         t.seth(290)
  266.         t.circle(100, 55)
  267.         t.circle(10, 50)

  268.         t.seth(120)
  269.         t.circle(100, 20)
  270.         t.left(180)
  271.         t.circle(-100, 20)

  272.         t.seth(0)
  273.         t.circle(10, 50)

  274.         t.seth(110)
  275.         t.circle(100, 20)
  276.         t.left(180)
  277.         t.circle(-100, 20)

  278.         t.seth(30)
  279.         t.circle(20, 50)

  280.         t.seth(100)
  281.         t.circle(100, 40)

  282.         # 右侧身体轮廓
  283.         t.seth(200)
  284.         t.circle(-100, 5)
  285.         t.left(180)
  286.         t.circle(100, 5)
  287.         t.left(30)
  288.         t.circle(100, 75)
  289.         t.right(15)
  290.         t.circle(-300, 21)
  291.         t.left(180)
  292.         t.circle(300, 3)

  293.         # 右手
  294.         t.seth(43)
  295.         t.circle(200, 60)

  296.         t.right(10)
  297.         t.fd(10)

  298.         t.circle(5, 160)
  299.         t.seth(90)
  300.         t.circle(5, 160)
  301.         t.seth(90)

  302.         t.fd(10)
  303.         t.seth(90)
  304.         t.circle(5, 180)
  305.         t.fd(10)

  306.         t.left(180)
  307.         t.left(20)
  308.         t.fd(10)
  309.         t.circle(5, 170)
  310.         t.fd(10)
  311.         t.seth(240)
  312.         t.circle(50, 30)

  313.         t.end_fill()
  314.         self.noTrace_goto(130, 125)
  315.         t.seth(-20)
  316.         t.fd(5)
  317.         t.circle(-5, 160)
  318.         t.fd(5)

  319.         # 手指纹
  320.         self.noTrace_goto(166, 130)
  321.         t.seth(-90)
  322.         t.fd(3)
  323.         t.circle(-4, 180)
  324.         t.fd(3)
  325.         t.seth(-90)
  326.         t.fd(3)
  327.         t.circle(-4, 180)
  328.         t.fd(3)

  329.         # 尾巴
  330.         self.noTrace_goto(168, 134)
  331.         t.fillcolor('#F6D02F')
  332.         t.begin_fill()
  333.         t.seth(40)
  334.         t.fd(200)
  335.         t.seth(-80)
  336.         t.fd(150)
  337.         t.seth(210)
  338.         t.fd(150)
  339.         t.left(90)
  340.         t.fd(100)
  341.         t.right(95)
  342.         t.fd(100)
  343.         t.left(110)
  344.         t.fd(70)
  345.         t.right(110)
  346.         t.fd(80)
  347.         t.left(110)
  348.         t.fd(30)
  349.         t.right(110)
  350.         t.fd(32)

  351.         t.right(106)
  352.         t.circle(100, 25)
  353.         t.right(15)
  354.         t.circle(-300, 2)
  355.         ##############
  356.         # print(t.pos())
  357.         t.seth(30)
  358.         t.fd(40)
  359.         t.left(100)
  360.         t.fd(70)
  361.         t.right(100)
  362.         t.fd(80)
  363.         t.left(100)
  364.         t.fd(46)
  365.         t.seth(66)
  366.         t.circle(200, 38)
  367.         t.right(10)
  368.         t.fd(10)
  369.         t.end_fill()

  370.         # 尾巴花纹
  371.         t.fillcolor('#923E24')
  372.         self.noTrace_goto(126.82, -156.84)
  373.         t.begin_fill()

  374.         t.seth(30)
  375.         t.fd(40)
  376.         t.left(100)
  377.         t.fd(40)
  378.         t.pencolor('#923e24')
  379.         t.seth(-30)
  380.         t.fd(30)
  381.         t.left(140)
  382.         t.fd(20)
  383.         t.right(150)
  384.         t.fd(20)
  385.         t.left(150)
  386.         t.fd(20)
  387.         t.right(150)
  388.         t.fd(20)
  389.         t.left(130)
  390.         t.fd(18)
  391.         t.pencolor('#000000')
  392.         t.seth(-45)
  393.         t.fd(67)
  394.         t.right(110)
  395.         t.fd(80)
  396.         t.left(110)
  397.         t.fd(30)
  398.         t.right(110)
  399.         t.fd(32)
  400.         t.right(106)
  401.         t.circle(100, 25)
  402.         t.right(15)
  403.         t.circle(-300, 2)
  404.         t.end_fill()

  405.         # 帽子、眼睛、嘴巴、脸颊
  406.         self.cap(-134.07, 147.81)
  407.         self.mouth(-5, 25)
  408.         self.leftCheek(-126, 32)
  409.         self.rightCheek(107, 63)
  410.         self.colorLeftEar(-250, 100)
  411.         self.colorRightEar(140, 270)
  412.         self.leftEye(-85, 90)
  413.         self.rightEye(50, 110)
  414.         t.hideturtle()

  415.     def cap(self, x, y):
  416.         self.noTrace_goto(x, y)
  417.         t = self.t
  418.         t.fillcolor('#CD0000')
  419.         t.begin_fill()
  420.         t.seth(200)
  421.         t.circle(400, 7)
  422.         t.left(180)
  423.         t.circle(-400, 30)
  424.         t.circle(30, 60)
  425.         t.fd(50)
  426.         t.circle(30, 45)
  427.         t.fd(60)
  428.         t.left(5)
  429.         t.circle(30, 70)
  430.         t.right(20)
  431.         t.circle(200, 70)
  432.         t.circle(30, 60)
  433.         t.fd(70)
  434.         # print(t.pos())
  435.         t.right(35)
  436.         t.fd(50)
  437.         t.circle(8, 100)
  438.         t.end_fill()
  439.         self.noTrace_goto(-168.47, 185.52)
  440.         t.seth(36)
  441.         t.circle(-270, 54)
  442.         t.left(180)
  443.         t.circle(270, 27)
  444.         t.circle(-80, 98)

  445.         t.fillcolor('#444444')
  446.         t.begin_fill()
  447.         t.left(180)
  448.         t.circle(80, 197)
  449.         t.left(58)
  450.         t.circle(200, 45)
  451.         t.end_fill()

  452.         self.noTrace_goto(-58, 270)
  453.         t.pencolor('#228B22')
  454.         t.dot(35)

  455.         self.noTrace_goto(-30, 280)
  456.         t.fillcolor('#228B22')
  457.         t.begin_fill()
  458.         t.seth(100)
  459.         t.circle(30, 180)
  460.         t.seth(190)
  461.         t.fd(15)
  462.         t.seth(100)
  463.         t.circle(-45, 180)
  464.         t.right(90)
  465.         t.fd(15)
  466.         t.end_fill()
  467.         t.pencolor('#000000')

  468.     def start(self):
  469.         self.body()


  470. def main():
  471.     print('Painting the Pikachu... ')
  472.     turtle.screensize(800, 600)
  473.     turtle.title('Pikachu')
  474.     pikachu = Pikachu()
  475.     pikachu.start()
  476.     turtle.mainloop()


  477. if __name__ == '__main__':
  478.     main()
复制代码



本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

1

主题

4

帖子

9

积分

新手上路

Rank: 1

积分
9
发表于 2021-5-18 02:05:11 | 显示全部楼层
您也许可以编写实现这三种方法的Mixin类。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|小白教程 ( 粤ICP备20019910号 )

GMT+8, 2024-9-20 14:56 , Processed in 0.049714 second(s), 25 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc. Template By 【未来科技】【 www.wekei.cn 】

快速回复 返回顶部 返回列表