当前位置:   article > 正文

基于turtle的新年代码_python turtle画图·龙年海报代码

python turtle画图·龙年海报代码

前言:本次项目难度低,学要有Python,并安装turtle,想要开发方便,可以使用pycharm开发 

  1. import turtle
  2. class main():
  3. def __init__(self):
  4. self.shape =turtle.Turtle()
  5. self.writting = turtle.Turtle()
  6. self.length = 0
  7. self.width = 0
  8. self.write_writting=""
  9. self.shape_goto_xy = [0,0]
  10. self.writting_goto_xy =[0,0]
  11. def draw_shape(self):
  12. self.shape.pensize(10)
  13. self.shape.pencolor("red")
  14. self.shape.fillcolor("yellow")
  15. self.shape.penup()
  16. self.shape.goto(self.shape_goto_xy[0],self.shape_goto_xy[1])
  17. self.shape.pendown()
  18. self.shape.begin_fill()
  19. self.shape.right(90)
  20. for i in range(4):
  21. self.shape.left(90)
  22. if i == 0 or i == 2:
  23. self.shape.forward(self.length)
  24. else:
  25. self.shape.forward(self.width)
  26. self.shape.end_fill()
  27. def draw_writting(self):
  28. self.writting.penup()
  29. self.writting.goto(self.writting_goto_xy[0],self.writting_goto_xy[1])
  30. self.writting.pendown()
  31. self.writting.write(self.write_writting, font=("STXINGKA", 50, "normal"))
  32. run = main()
  33. run.length=200
  34. run.width=200
  35. run.shape.left(45)
  36. run.draw_shape()
  37. run.writting_goto_xy = [-35,115]
  38. run.write_writting = "福"
  39. run.draw_writting()
  40. run.shape.penup()
  41. for i in range(2):
  42. run.shape.left(90)
  43. run.shape.forward(200)
  44. run.shape.right(45)
  45. run.shape.forward(200)
  46. run.shape.left(90)
  47. run.shape.forward(200)
  48. run.shape.pendown()
  49. run.shape.begin_fill()
  50. for i in range(5):
  51. run.shape.left(90)
  52. if i == 0 or i == 2 or i == 4:
  53. run.shape.forward(100)
  54. else:
  55. run.shape.forward(400)
  56. run.shape.end_fill()
  57. writting_goto_xy=list(run.shape.pos())
  58. run.writting_goto_xy = [writting_goto_xy[0]+70,writting_goto_xy[1]+10]
  59. run.write_writting="喜气洋洋"
  60. run.draw_writting()
  61. run.width=100
  62. run.length=700
  63. run.shape_goto_xy=[-450,300]
  64. run.draw_shape()
  65. writting = "好运常临平安宅"
  66. write = list(writting)
  67. write_y = 300
  68. for i in range(7):
  69. write_y= write_y-100
  70. run.writting_goto_xy = [-435,write_y]
  71. run.write_writting = write[i]
  72. run.draw_writting()
  73. run.width=700
  74. run.length=100
  75. run.shape_goto_xy=[450,300]
  76. run.draw_shape()
  77. writting = "和气生财幸福家"
  78. write = list(writting)
  79. write_y = 300
  80. for i in range(7):
  81. write_y= write_y-100
  82. run.writting_goto_xy = [365,write_y]
  83. run.write_writting = write[i]
  84. run.draw_writting()
  85. turtle.done()

效果如下: 

 文字可变,作者文采不高,从网上先找的对联,这里面的文字,也都可以更改,十分的方便,turtle自己也可以展现路径,也是很壮观了。而代码十分简单,做起来给朋友炫下技也是可以的

1.对象

在这里我做了一个类,在这个类里,有这个项目所需的函数与变量,用处不一定很大,但是在后期维护与开发会简单很多,一个是画矩形,一个是话文字,而因为一些问题 ,竖着的字没法直接写,后面会讲到,这里横着写文字是完全没问题的

2.函数

矩形部分如下:

  1. def draw_shape(self):
  2. self.shape.pensize(10)
  3. self.shape.pencolor("red")
  4. self.shape.fillcolor("yellow")
  5. self.shape.penup()
  6. self.shape.goto(self.shape_goto_xy[0],self.shape_goto_xy[1])
  7. self.shape.pendown()
  8. self.shape.begin_fill()
  9. self.shape.right(90)
  10. for i in range(4):
  11. self.shape.left(90)
  12. if i == 0 or i == 2:
  13. self.shape.forward(self.length)
  14. else:
  15. self.shape.forward(self.width)

在for循环之前,是一些初始化和调参,用penup让调参不留痕迹,但在这之后,画图是要调用penup进行落笔,fillcolor与pencolor分别表示内部填充颜色与画笔颜色, length代表长,width代表宽,默认画一个横向的长方形,但是画完之后不会自动重置,需要手动调回来。当然,如果长和宽一样的话会绘画正方形。

3.文字: 

文字部分如下

  1. def draw_writting(self):
  2. self.writting.penup()
  3. self.writting.goto(self.writting_goto_xy[0],self.writting_goto_xy[1])
  4. self.writting.pendown()
  5. self.writting.write(self.write_writting, font=("STXINGKA", 50, "normal"))

写字用了自带的函数,write,font为一个类似常量的东西,第一个是字体文件,要找自己用的,但turtle会统一化简,用goto去到指定位置,writting_goto_xy里,以列表储存,总体来说这里是十分简单的。


初始部分的函数写完,下面开始实战。

1.矩形

先用之前的函数画一个正方形,但因为其回到起点,需要再跑半遍,之后绘画横批部分,再画左右对联,参数要自己找,但本人作为初中生,用勾股定理和直角坐标系就推了出来,难度不大,就是后面写文字的时候,横批的参数不是有理数于是用了pos获取坐标调的参。

2.文字:

写的文t我存在了writting_write里面,在写福字和横批时可以直接用,但在对联上犯了难,最后我有单独存在了一个变量,将对联用list拆成了一个个字,在用for循环每次调取一个,用减少的参数来代替下滑的作用,这是作者的最优解,如果有更好的方案还希望大家提出。

最后画的效果就是这样:

2023朋友用 - Trim

注:本人初中在读,在编程方面才学习三年,若有不好的地方,希望大家指正,谢谢

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/127384
推荐阅读
相关标签
  

闽ICP备14008679号