#coding=gbk
class Shape:#shape 不带角度的基类
def __init__(self,cvns,points):#cvns 绘制图形的画布参数 points坐标参数
self.cvns =cvns
self.points=points
self.pid= None #在画布当中的id号
def delete(self): #将当前的图形删除
ifself.pid:
self.cvns.delete(self.pid)class ShapeAngles(Shape):#带角度的基类
def __init__(self,cvns,points,angles=(10,170)):#angles 是角度 图形角度
super().__init__(cvns,points)
self.angles= {'start':angles[0],'extent':angles[1]}class HatTop(Shape):#无角度类
def draw(self):#绘制函数
self.pid = self.cvns.create_oval(*self.points)#绘制圆形的方法,返回id
classHatBottom(Shape):defdraw(self):
self.pid= self.cvns.create_polygon(*self.points)class Hat:#帽子组合类
def __init__(self,cvns,start_point,w,h):
self.cvns=cvns
self.start_point=start_point
self.w=w
self.h=h
self.ht= HatTop(self.cvns,self.ht_cacu())#实例化帽子顶部
self.hb = HatBottom(self.cvns,self.hb_cacu())#实例化帽子底部
defdraw(self):
self.ht.draw()
self.hb.draw()defdelete(self):
self.ht.delete()
self.hb.delete()def ht_cacu(self):#计算位置
r = self.h / 3 / 2x1= self.start_point[0] + self.w /2 -r
y1= self.start_point[1]
x2= x1 + 2 *r
y2= y1 + 2 *rreturnx1,y1,x2,y2defhb_cacu(self):
x1= self.start_point[0] + self.w / 2y1= self.start_point[1] + self.h / 3x2= self.start_point[0] + self.w / 3y2= self.start_point[1] +self.h
x3= self.start_point[0] + self.w / 3 * 2y3=y2returnx1,y1,x2,y2,x3,y3classSense(ShapeAngles):defdraw(self):
self.pid= self.cvns.create_arc(*self.points,**self.angles)#绘制一条弧线
class Face(HatTop):#圆和帽子顶部类似
pass
classHead:def __init__(self,cvns,start_point,w,h):
self.cvns=cvns
self.start_point=start_point
self.w=w
self.h=h
eye0_points=self.eye0_cacu()
dx= self.h / 3 + self.h / 9eye1_points= (eye0_points[0] + dx,eye0_points[1],
eye0_points[2] + dx,eye0_points[3])
self.face=Face(self.cvns,self.face_cacu())
self.eye0=Sense(self.cvns,eye0_points)
self.eye1=Sense(self.cvns,eye1_points)
self.mouth= Sense(self.cvns,self.mouth_cacu(),(-10,-170))defdraw(self):
self.face.draw()
self.eye0.draw()
self.eye1.draw()
self.mouth.draw()defface_cacu(self):
x1= self.start_point[0] + (self.w - self.h) / 2y1= self.start_point[1]
x2= x1 +self.h
y2= y1 +self.hreturnx1,y1,x2,y2defeye0_cacu(self):
left_point= (self.start_point[0] + (self.w - self.h) / 2,self.start_point[1])
x1= left_point[0] + self.h / 6y1= left_point[1] + self.h / 3x2= x1 + self.h / 3y2= left_point[1] + self.h / 2
returnx1,y1,x2,y2defmouth_cacu(self):
left_point= (self.start_point[0] + (self.w - self.h) / 2,self.start_point[1])
x1= left_point[0] + self.h / 3y1= left_point[1] + 2 * self.h / 3x2= x1 + self.h / 3y2= y1 + self.h / 3 / 2
returnx1,y1,x2,y2class BodyOutline(HatTop):#圆
pass
class Button(HatTop):#圆
pass
classBody:def __init__(self,cvns,start_point,w,h):
self.cvns=cvns
self.start_point=start_point
self.w=w
self.h=h
self._button_size= 10self.buttons=[]
self.bo=BodyOutline(self.cvns,self.body_cacu())for pnts inself.all_button_points():
self.buttons.append(Button(self.cvns,pnts))defdraw(self):
self.bo.draw()for bttn inself.buttons:
bttn.draw()defbody_cacu(self):
x1,y1=self.start_point
x2= x1 +self.w
y2= y1 +self.hreturnx1,y1,x2,y2defbutton0_cacu(self):
x1= self.start_point[0] + self.w / 2 -self._button_size
y1= self.start_point[1] + self.h / 5 -self._button_size
x2= x1 + 2 *self._button_size
y2= y1 + 2 *self._button_sizereturnx1,y1,x2,y2defmove_dy(self,points,size):
y1= points[1] +size
y2= points[3] +sizereturn points[0],y1,points[2],y2defall_button_points(self):
b0_points=self.button0_cacu()
size= self.h / 5points=[]for i in range(4):
points.append(self.move_dy(b0_points,i*size))returnpointsdefset_button_size(self,size):
self._button_size=sizeclassSnow:def __init__(self,cvns,points,w=150,h=450):
self.cvns=cvns
self.points=points
self.w=w
self.h=h
self.hat= Hat(self.cvns,self.points,self.w,self.h / 6)
self.head= Head(self.cvns,(self.points[0],self.points[1] + self.h / 6),self.w,self.h / 3)
self.body= Body(self.cvns,(self.points[0],self.points[1] + self.h / 2),self.w,self.h / 2)defdraw(self):
self.hat.draw()
self.head.draw()
self.body.draw()if __name__ == '__main__':importtkinter
root=tkinter.Tk()
cvns= tkinter.Canvas(root,width=600,height=665,bg='white')
cvns.pack()
snow= Snow(cvns,(10,5),300,660)
snow=snow.draw()
root.mainloop()