python画图代码大全画雪人,用python画雪人的编程

大家好,小编来为大家解答以下问题,python画图代码大全画雪人,用python画雪人的编程,今天让我们一起来看看吧!

大家好,小编为大家解答python画图代码大全画雪人的问题。很多人还不知道用python画雪人编程代码,现在让我们一起来看看吧!

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

希望您给个关注给个赞,也算对我们的支持了python简单代码画爱心。



class Shape:     # 基类(雪人各部件(形状)共有的属性)
    def __init__(self, cvns, points, fill):     # 构造方法  画布  位置坐标  颜色
          = cvns                 # 画布
         self.points = points             # 坐标(x1, y1, x2, y2)
          = fill
          = None                  # 当前图形的id

    def delete(self):         # 删除图形
         if :
             .delete()


class ShapeAngles(Shape):     # 继承基类(增加了角度))
    def __init__(self, cvns, points, fill, angles=(10, 170)):        # angles:角度值,带默认参数
        super(ShapeAngles, self).__init__(cvns, points, fill)   # 调用基类构造: cvns,points,fill
        self.angles = {'start':angles[0], 'extent':angles[1]}  # 构造自己的属性:angles


class HatTop(Shape):      # 帽子顶部
    
    def draw(self):
#         = .create_oval(self.points, fill='white')       # 椭圆形
         = .create_oval(self.points, )       # 椭圆形


class HatBottom(Shape):    # 帽子底部
    
    def draw(self):
         = .create_polygon(self.points)     # 绘多边形的方法


class Hat:         # 帽子整体(组合顶部和底部)
    def __init__(self, cvns, start_point, fill, w, h):    # w,h是帽子的宽、高
         = cvns                            # 初始化
        self.start_point = start_point
        self.w = w
         = fill
        self.h = h
         = HatTop(, _cacu(), )        # 实例化顶部
         = HatBottom(, _cacu(), )         # 实例化底部
        
    def draw(self):                # 绘制
        ()              # 调用顶部方法绘制
        ()              # 调用底部方法绘制

    def delete(self):
       .delete()
       # .delete()

    def ht_cacu(self):             # 计算顶部坐标
        r = self.h / 3 / 2
        x1 = self.start_point[0] + self.w / 2 - r
        y1 = self.start_point[1] + 20 - r
        x2 = x1 + 2 * r
        y2 = y1 + 2 * r
        return x1, y1, x2, y2

    def hb_cacu(self):              # 计算底部坐标(三角形的三个点的坐标)
        x1 = self.start_point[0] + self.w / 2
        y1 = self.start_point[1] + self.h / 3
        x2 = self.start_point[0] + self.w / 3
        y2 = self.start_point[1] + self.h + 13
        x3 = self.start_point[0] + self.w / 3 * 2
        y3 = y2
        return x1, y1, x2, y2, x3, y3


class Sense(ShapeAngles):                # 五官(眼、口扇形图形)
    def draw(self):
         = .create_arc(*self.points, **self.angles, fill='red')    # 绘制弧线

class Face(HatTop):   # 脸
    pass


class Head:         # 头部
    def __init__(self, cvns, start_point, fill, w, h):    # 此处的w,h是头的
         = cvns
        self.start_point = start_point
         = fill
        self.w = w
        self.h = h
        eye0_points = self.eye0_cacu()     # 眼睛1坐标
        dx = self.h / 3 + self.h / 9
        eye1_points = (eye0_points[0] + dx, eye0_points[1],    # 眼睛2坐标
                       eye0_points[2] + dx, eye0_points[3])
         = Face(, _cacu(), )          # 脸:带参数的实例
        self.eye0 = Sense(, eye0_points, fill='blue')              # 眼1:带参数的实例
        self.eye1 = Sense(, eye1_points, )              # 眼2:带参数的实例
        self.mouth = Sense(, self.mouth_cacu(), (-10, -170))  # 口:带参数的实例

    def draw(self):
        # 绘制脸部各部位
        ()
        ()
        ()
        ()

    def face_cacu(self):             # 脸坐标计算
        x1 = self.start_point[0] + (self.w - self.h) / 2
        y1 = self.start_point[1]
        x2 = x1 + self.h
        y2 = y1 + self.h
        return x1, y1, x2, y2

    def eye0_cacu(self):              # 眼0坐标计算
        left_point = (self.start_point[0] + (self.w - self.h) / 2 - 5, self.start_point[1])
        x1 = left_point[0] + self.h / 6
        y1 = left_point[1] + self.h / 3
        x2 = x1 + self.h / 3
        y2 = left_point[1] + self.h / 2
        return x1, y1, x2, y2

    def mouth_cacu(self):            # 口坐标计算
        left_point = (self.start_point[0] + (self.w - self.h) / 2, self.start_point[1])
        x1 = left_point[0] + self.h / 3
        y1 = left_point[1] + 2 * self.h / 3 + 25      # +25后口的位置靠下,并且图形更大了
        x2 = x1 + self.h / 3
        y2 = left_point[1] + self.h / 2
        return x1, y1, x2, y2


class hand(HatTop):            # 手
    pass


class BodyOutline(HatTop):      # 身体轮廓,因没有特别的形状,继承了基类,类体为空
    pass


class Button(HatTop):            # 钮扣
    pass

class Body:                      # 身体

    def __init__(self, cvns, start_point, fill, w, h):
         = cvns
        self.start_point = start_point
        self.w = w
        self.h = h
         = fill
        self._button_size = 10        # 钮扣的大小
        self.buttons = []
         = BodyOutline(, _cacu(), )      # 身体轮廓实例
#         = hand(, (15, 500, 45, 240), )           # 左手轮廓实例,坐标为矩形的两个对角顶点的坐标为准画的圆/椭圆
         = hand(, self.bd_cacu(0), )           # 左手轮廓实例,坐标为矩形的两个对角顶点的坐标为准画的圆/椭圆
        2 = hand(, self.bd_cacu(self.w), )  # 右手
        for pnts in self.all_button_points():
            self.buttons.append(Button(, pnts, ))

    def bd_cacu(self, w):  # 计算手的坐标
        x1 = 15 + w
        y1 = self.start_point[1] + self.h / 2
        x2 = x1 + 30
        y2 = y1 - 26 * self._button_size
        return x1, y1, x2, y2

    def draw(self):
        ()                # 身体绘制
        ()                # 手1绘制
        ()               # 手2绘制
        for bttn in self.buttons:    # 各钮扣绘制
            ()

    def body_cacu(self):           # 计算身体轮廓坐标
        x1, y1 = self.start_point
        x2 = x1 + self.w
        y2 = y1 + self.h
        return x1, y1, x2, y2

    def button0_cacu(self):        # 计算第0个钮扣的坐标
        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         # 2决定钮扣的园形形状
        y2 = y1 + 2 * self._button_size
        return x1, y1, x2, y2

    def move_dy(self, points, size):   # 钮扣移动的方法
        y1 = points[1] + size
        y2 = points[3] + size
        return points[0], y1, points[2], y2

    def all_button_points(self):          # 绘制每个钮扣的坐标
        b0_points = self.button0_cacu()
        size = self.h / 6                   # 身高/钮扣数+1
        points = []                         # 列表
        for i in range(5):                 # 钮扣的个数
            points.append(self.move_dy(b0_points, i * size))   # 各钮扣的移动数据存入列表points
        return points                   # 返回列表值

    # def set_button_size(self, size):
    #     self._button_size = size


class Snow:           # 组装成雪人

    def __init__(self, cvns, points, fill, w=150, h=450):       # points为雪人的坐标其与帽子坐标一致(见雪人图)
         = cvns
        self.points = points
        self.w = w
        self.h = h
         = fill
         = Head(, (self.points[0], self.points[1] + self.h / 6), , self.w, self.h / 3)   # 实例化头部
         = Body(, (self.points[0], self.points[1] + self.h / 2), , self.w, self.h / 2)   # 实例化身体
         = 'red'                                                            # 帽子顶部颜色
         = Hat(, self.points, , self.w, self.h / 6)        # 绘帽子                             # 实例化帽子

    def draw(self):
        ()         # 绘制帽子
        ()        # 绘制头
        ()        # 绘制身体

if __name__ == '__main__':
    import tkinter
    root = ()         # 建立根窗口
    cvns = tkinter.Canvas(root, width=400, height=700, bg='white')   # 调用画布
    ()                 # 将画布添加到窗口
    snow = Snow(cvns, (30, 15), 'white', 320, 660)   # 雪人的实例化(传入画布对象、起始坐标、宽、高)
    snow = ()          # 绘制
    root.mainloop()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值