脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - 利用Python如何画一颗心、小人发射爱心

利用Python如何画一颗心、小人发射爱心

2021-09-07 00:28一个超会写Bug的安太狼 Python

这篇文章主要给大家介绍了关于利用Python如何画一颗心、小人发射爱心的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

源码:

  1. #!/usr/bin/env python
  2.  
  3. # -*- coding:utf-8 -*-
  4.  
  5. import turtle
  6. import time
  7.  
  8. # 画心形圆弧
  9.  
  10. def hart_arc():
  11.  
  12. for i in range(200):
  13.  
  14. turtle.right(1)
  15.  
  16. turtle.forward(2)
  17.  
  18. def move_pen_position(x, y):
  19.  
  20. turtle.hideturtle() # 隐藏画笔(先)
  21.  
  22. turtle.up() # 提笔
  23.  
  24. turtle.goto(x, y) # 移动画笔到指定起始坐标(窗口中心为0,0)
  25.  
  26. turtle.down() # 下笔
  27.  
  28. turtle.showturtle() # 显示画笔
  29.  
  30. # 初始化
  31.  
  32. turtle.setup(width=800, height=500) # 窗口(画布)大小
  33.  
  34. turtle.color('red', 'pink') # 画笔颜色
  35.  
  36. turtle.pensize(3) # 画笔粗细
  37.  
  38. turtle.speed(1) # 描绘速度
  39.  
  40. # 初始化画笔起始坐标
  41.  
  42. move_pen_position(x=0,y=-180) # 移动画笔位置
  43.  
  44. turtle.left(140) # 向左旋转140度
  45.  
  46. turtle.begin_fill() # 标记背景填充位置
  47.  
  48. # 画心形直线( 左下方 )
  49. turtle.forward(224) # 向前移动画笔,长度为224
  50.  
  51. # 画爱心圆弧
  52.  
  53. hart_arc() # 左侧圆弧
  54. turtle.left(120) # 调整画笔角度
  55. hart_arc() # 右侧圆弧
  56.  
  57. # 画心形直线( 右下方 )
  58.  
  59. turtle.forward(224)
  60.  
  61. turtle.end_fill() # 标记背景填充结束位置
  62.  
  63. # 点击窗口关闭程序
  64.  
  65. window = turtle.Screen()
  66.  
  67. window.exitonclick()

效果图:

利用Python如何画一颗心、小人发射爱心

源码:

  1. import turtle as t
  2. from time import sleep
  3. def go_to(x, y):
  4. t.up()
  5. t.goto(x, y)
  6. t.down()
  7. def head(x, y, r):
  8. go_to(x, y)
  9. t.speed(20)
  10. t.circle(r)
  11. leg(x, y)
  12. def leg(x, y):
  13. t.right(90)
  14. t.forward(180)
  15. t.right(30)
  16. t.forward(100)
  17. t.left(120)
  18. go_to(x, y - 180)
  19. t.forward(100)
  20. t.right(120)
  21. t.forward(100)
  22. t.left(120)
  23. hand(x, y)
  24. def hand(x, y):
  25. go_to(x, y - 60)
  26. t.forward(100)
  27. t.left(60)
  28. t.forward(100)
  29. go_to(x, y - 90)
  30. t.right(60)
  31. t.forward(100)
  32. t.right(60)
  33. t.forward(100)
  34. t.left(60)
  35. eye(x, y)
  36. def eye(x, y):
  37. go_to(x - 50, y + 130)
  38. t.right(90)
  39. t.forward(50)
  40. go_to(x + 40, y + 130)
  41. t.forward(50)
  42. t.left(90)
  43. def big_Circle(size):
  44. t.speed(20)
  45. for i in range(150):
  46. t.forward(size)
  47. t.right(0.3)
  48. def line(size):
  49. t.speed(20)
  50. t.forward(51 * size)
  51. def small_Circle(size):
  52. t.speed(20)
  53. for i in range(210):
  54. t.forward(size)
  55. t.right(0.786)
  56. def heart(x, y, size):
  57. go_to(x, y)
  58. t.left(150)
  59. t.begin_fill()
  60. line(size)
  61. big_Circle(size)
  62. small_Circle(size)
  63. t.left(120)
  64. small_Circle(size)
  65. big_Circle(size)
  66. line(size)
  67. t.end_fill()
  68. def main():
  69. t.pensize(2)
  70. t.color('red', 'pink')
  71. head(-120, 100, 100)
  72. heart(250, -80, 1)
  73. go_to(100, -300)
  74. t.write("To: 智慧与美貌并存的", move=True, align="left", font=("楷体", 20, "normal"))
  75. t.done()
  76. main()

效果图:

利用Python如何画一颗心、小人发射爱心

总结

到此这篇关于利用Python如何画一颗心、小人发射爱心的文章就介绍到这了,更多相关Python画小人发射爱心内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/m0_46278037/article/details/113819989

延伸 · 阅读

精彩推荐