Python画图基础操作之全注释画雪人

举报
zhulin1028 发表于 2021/10/29 23:21:52 2021/10/29
【摘要】 一步步教你怎么用Python画雪人,进一步熟悉Python的基础画图操作,废话不多说,上代码。 希望您给个关注给个赞,也算对我们的支持了。 class Shape: # 基类(雪人各部件(形状)共有的属性) def __init__(self, cvns, points, fill): # 构造方法 画布...

一步步教你怎么用Python画雪人,进一步熟悉Python的基础画图操作,废话不多说,上代码。

希望您给个关注给个赞,也算对我们的支持了。


  
  1. class Shape: # 基类(雪人各部件(形状)共有的属性)
  2. def __init__(self, cvns, points, fill): # 构造方法 画布 位置坐标 颜色
  3. self.cvns = cvns # 画布
  4. self.points = points # 坐标(x1, y1, x2, y2)
  5. self.fill = fill
  6. self.pid = None # 当前图形的id
  7. def delete(self): # 删除图形
  8. if self.pid:
  9. self.cvns.delete(self.pid)
  10. class ShapeAngles(Shape): # 继承基类(增加了角度))
  11. def __init__(self, cvns, points, fill, angles=(10, 170)): # angles:角度值,带默认参数
  12. super(ShapeAngles, self).__init__(cvns, points, fill) # 调用基类构造: cvns,points,fill
  13. self.angles = {'start':angles[0], 'extent':angles[1]} # 构造自己的属性:angles
  14. class HatTop(Shape): # 帽子顶部
  15. def draw(self):
  16. # self.pid = self.cvns.create_oval(self.points, fill='white') # 椭圆形
  17. self.pid = self.cvns.create_oval(self.points, fill=self.fill) # 椭圆形
  18. class HatBottom(Shape): # 帽子底部
  19. def draw(self):
  20. self.pid = self.cvns.create_polygon(self.points) # 绘多边形的方法
  21. class Hat: # 帽子整体(组合顶部和底部)
  22. def __init__(self, cvns, start_point, fill, w, h): # w,h是帽子的宽、高
  23. self.cvns = cvns # 初始化
  24. self.start_point = start_point
  25. self.w = w
  26. self.fill = fill
  27. self.h = h
  28. self.ht = HatTop(self.cvns, self.ht_cacu(), fill=self.fill) # 实例化顶部
  29. self.hb = HatBottom(self.cvns, self.hb_cacu(), self.fill) # 实例化底部
  30. def draw(self): # 绘制
  31. self.ht.draw() # 调用顶部方法绘制
  32. self.hb.draw() # 调用底部方法绘制
  33. def delete(self):
  34. self.ht.delete()
  35. # self.hb.delete()
  36. def ht_cacu(self): # 计算顶部坐标
  37. r = self.h / 3 / 2
  38. x1 = self.start_point[0] + self.w / 2 - r
  39. y1 = self.start_point[1] + 20 - r
  40. x2 = x1 + 2 * r
  41. y2 = y1 + 2 * r
  42. return x1, y1, x2, y2
  43. def hb_cacu(self): # 计算底部坐标(三角形的三个点的坐标)
  44. x1 = self.start_point[0] + self.w / 2
  45. y1 = self.start_point[1] + self.h / 3
  46. x2 = self.start_point[0] + self.w / 3
  47. y2 = self.start_point[1] + self.h + 13
  48. x3 = self.start_point[0] + self.w / 3 * 2
  49. y3 = y2
  50. return x1, y1, x2, y2, x3, y3
  51. class Sense(ShapeAngles): # 五官(眼、口扇形图形)
  52. def draw(self):
  53. self.pid = self.cvns.create_arc(*self.points, **self.angles, fill='red') # 绘制弧线
  54. class Face(HatTop): # 脸
  55. pass
  56. class Head: # 头部
  57. def __init__(self, cvns, start_point, fill, w, h): # 此处的w,h是头的
  58. self.cvns = cvns
  59. self.start_point = start_point
  60. self.fill = fill
  61. self.w = w
  62. self.h = h
  63. eye0_points = self.eye0_cacu() # 眼睛1坐标
  64. dx = self.h / 3 + self.h / 9
  65. eye1_points = (eye0_points[0] + dx, eye0_points[1], # 眼睛2坐标
  66. eye0_points[2] + dx, eye0_points[3])
  67. self.face = Face(self.cvns, self.face_cacu(), self.fill) # 脸:带参数的实例
  68. self.eye0 = Sense(self.cvns, eye0_points, fill='blue') # 眼1:带参数的实例
  69. self.eye1 = Sense(self.cvns, eye1_points, self.fill) # 眼2:带参数的实例
  70. self.mouth = Sense(self.cvns, self.mouth_cacu(), (-10, -170)) # 口:带参数的实例
  71. def draw(self):
  72. # 绘制脸部各部位
  73. self.face.draw()
  74. self.eye0.draw()
  75. self.eye1.draw()
  76. self.mouth.draw()
  77. def face_cacu(self): # 脸坐标计算
  78. x1 = self.start_point[0] + (self.w - self.h) / 2
  79. y1 = self.start_point[1]
  80. x2 = x1 + self.h
  81. y2 = y1 + self.h
  82. return x1, y1, x2, y2
  83. def eye0_cacu(self): # 眼0坐标计算
  84. left_point = (self.start_point[0] + (self.w - self.h) / 2 - 5, self.start_point[1])
  85. x1 = left_point[0] + self.h / 6
  86. y1 = left_point[1] + self.h / 3
  87. x2 = x1 + self.h / 3
  88. y2 = left_point[1] + self.h / 2
  89. return x1, y1, x2, y2
  90. def mouth_cacu(self): # 口坐标计算
  91. left_point = (self.start_point[0] + (self.w - self.h) / 2, self.start_point[1])
  92. x1 = left_point[0] + self.h / 3
  93. y1 = left_point[1] + 2 * self.h / 3 + 25 # +25后口的位置靠下,并且图形更大了
  94. x2 = x1 + self.h / 3
  95. y2 = left_point[1] + self.h / 2
  96. return x1, y1, x2, y2
  97. class hand(HatTop): # 手
  98. pass
  99. class BodyOutline(HatTop): # 身体轮廓,因没有特别的形状,继承了基类,类体为空
  100. pass
  101. class Button(HatTop): # 钮扣
  102. pass
  103. class Body: # 身体
  104. def __init__(self, cvns, start_point, fill, w, h):
  105. self.cvns = cvns
  106. self.start_point = start_point
  107. self.w = w
  108. self.h = h
  109. self.fill = fill
  110. self._button_size = 10 # 钮扣的大小
  111. self.buttons = []
  112. self.bo = BodyOutline(self.cvns, self.body_cacu(), self.fill) # 身体轮廓实例
  113. # self.hd = hand(self.cvns, (15, 500, 45, 240), self.fill) # 左手轮廓实例,坐标为矩形的两个对角顶点的坐标为准画的圆/椭圆
  114. self.hd = hand(self.cvns, self.bd_cacu(0), self.fill) # 左手轮廓实例,坐标为矩形的两个对角顶点的坐标为准画的圆/椭圆
  115. self.hd2 = hand(self.cvns, self.bd_cacu(self.w), self.fill) # 右手
  116. for pnts in self.all_button_points():
  117. self.buttons.append(Button(self.cvns, pnts, self.fill))
  118. def bd_cacu(self, w): # 计算手的坐标
  119. x1 = 15 + w
  120. y1 = self.start_point[1] + self.h / 2
  121. x2 = x1 + 30
  122. y2 = y1 - 26 * self._button_size
  123. return x1, y1, x2, y2
  124. def draw(self):
  125. self.bo.draw() # 身体绘制
  126. self.hd.draw() # 手1绘制
  127. self.hd2.draw() # 手2绘制
  128. for bttn in self.buttons: # 各钮扣绘制
  129. bttn.draw()
  130. def body_cacu(self): # 计算身体轮廓坐标
  131. x1, y1 = self.start_point
  132. x2 = x1 + self.w
  133. y2 = y1 + self.h
  134. return x1, y1, x2, y2
  135. def button0_cacu(self): # 计算第0个钮扣的坐标
  136. x1 = self.start_point[0] + self.w / 2 - self._button_size
  137. y1 = self.start_point[1] + self.h / 5 - self._button_size
  138. x2 = x1 + 2 * self._button_size # 2决定钮扣的园形形状
  139. y2 = y1 + 2 * self._button_size
  140. return x1, y1, x2, y2
  141. def move_dy(self, points, size): # 钮扣移动的方法
  142. y1 = points[1] + size
  143. y2 = points[3] + size
  144. return points[0], y1, points[2], y2
  145. def all_button_points(self): # 绘制每个钮扣的坐标
  146. b0_points = self.button0_cacu()
  147. size = self.h / 6 # 身高/钮扣数+1
  148. points = [] # 列表
  149. for i in range(5): # 钮扣的个数
  150. points.append(self.move_dy(b0_points, i * size)) # 各钮扣的移动数据存入列表points
  151. return points # 返回列表值
  152. # def set_button_size(self, size):
  153. # self._button_size = size
  154. class Snow: # 组装成雪人
  155. def __init__(self, cvns, points, fill, w=150, h=450): # points为雪人的坐标其与帽子坐标一致(见雪人图)
  156. self.cvns = cvns
  157. self.points = points
  158. self.w = w
  159. self.h = h
  160. self.fill = fill
  161. self.head = Head(self.cvns, (self.points[0], self.points[1] + self.h / 6), self.fill, self.w, self.h / 3) # 实例化头部
  162. self.body = Body(self.cvns, (self.points[0], self.points[1] + self.h / 2), self.fill, self.w, self.h / 2) # 实例化身体
  163. self.fill = 'red' # 帽子顶部颜色
  164. self.hat = Hat(self.cvns, self.points, self.fill, self.w, self.h / 6) # 绘帽子 # 实例化帽子
  165. def draw(self):
  166. self.hat.draw() # 绘制帽子
  167. self.head.draw() # 绘制头
  168. self.body.draw() # 绘制身体
  169. if __name__ == '__main__':
  170. import tkinter
  171. root = tkinter.Tk() # 建立根窗口
  172. cvns = tkinter.Canvas(root, width=400, height=700, bg='white') # 调用画布
  173. cvns.pack() # 将画布添加到窗口
  174. snow = Snow(cvns, (30, 15), 'white', 320, 660) # 雪人的实例化(传入画布对象、起始坐标、宽、高)
  175. snow = snow.draw() # 绘制
  176. root.mainloop()

文章来源: zhulin1028.blog.csdn.net,作者:zhulin1028,版权归原作者所有,如需转载,请联系作者。

原文链接:zhulin1028.blog.csdn.net/article/details/120536554

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。