欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python畫圖小案例之小雪人超詳細(xì)源碼注釋

 更新時間:2021年09月30日 09:54:03   作者:zhulin1028  
在看了很多Python教程之后,覺得是時候做點(diǎn)什么小項(xiàng)目來練練手了,于是想來想去,用python寫了一個小雪人,代碼注釋無比詳細(xì)清楚,快來看看吧

一步步教你怎么用Python畫雪人,進(jìn)一步熟悉Python的基礎(chǔ)畫圖操作,廢話不多說,上代碼。

希望您給個關(guān)注給個贊,也算對我們的支持了。

class Shape:     # 基類(雪人各部件(形狀)共有的屬性)
    def __init__(self, cvns, points, fill):     # 構(gòu)造方法  畫布  位置坐標(biāo)  顏色
         self.cvns = cvns                 # 畫布
         self.points = points             # 坐標(biāo)(x1, y1, x2, y2)
         self.fill = fill
         self.pid = None                  # 當(dāng)前圖形的id
 
    def delete(self):         # 刪除圖形
         if self.pid:
             self.cvns.delete(self.pid)
 
 
class ShapeAngles(Shape):     # 繼承基類(增加了角度))
    def __init__(self, cvns, points, fill, angles=(10, 170)):        # angles:角度值,帶默認(rèn)參數(shù)
        super(ShapeAngles, self).__init__(cvns, points, fill)   # 調(diào)用基類構(gòu)造: cvns,points,fill
        self.angles = {'start':angles[0], 'extent':angles[1]}  # 構(gòu)造自己的屬性:angles
 
 
class HatTop(Shape):      # 帽子頂部
    
    def draw(self):
#        self.pid = self.cvns.create_oval(self.points, fill='white')       # 橢圓形
        self.pid = self.cvns.create_oval(self.points, fill=self.fill)       # 橢圓形
 
 
class HatBottom(Shape):    # 帽子底部
    
    def draw(self):
        self.pid = self.cvns.create_polygon(self.points)     # 繪多邊形的方法
 
 
class Hat:         # 帽子整體(組合頂部和底部)
    def __init__(self, cvns, start_point, fill, w, h):    # w,h是帽子的寬、高
        self.cvns = cvns                            # 初始化
        self.start_point = start_point
        self.w = w
        self.fill = fill
        self.h = h
        self.ht = HatTop(self.cvns, self.ht_cacu(), fill=self.fill)        # 實(shí)例化頂部
        self.hb = HatBottom(self.cvns, self.hb_cacu(), self.fill)         # 實(shí)例化底部
        
    def draw(self):                # 繪制
        self.ht.draw()              # 調(diào)用頂部方法繪制
        self.hb.draw()              # 調(diào)用底部方法繪制
 
    def delete(self):
       self.ht.delete()
       # self.hb.delete()
 
    def ht_cacu(self):             # 計算頂部坐標(biāo)
        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):              # 計算底部坐標(biāo)(三角形的三個點(diǎn)的坐標(biāo))
        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):
        self.pid = self.cvns.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是頭的
        self.cvns = cvns
        self.start_point = start_point
        self.fill = fill
        self.w = w
        self.h = h
        eye0_points = self.eye0_cacu()     # 眼睛1坐標(biāo)
        dx = self.h / 3 + self.h / 9
        eye1_points = (eye0_points[0] + dx, eye0_points[1],    # 眼睛2坐標(biāo)
                       eye0_points[2] + dx, eye0_points[3])
        self.face = Face(self.cvns, self.face_cacu(), self.fill)          # 臉:帶參數(shù)的實(shí)例
        self.eye0 = Sense(self.cvns, eye0_points, fill='blue')              # 眼1:帶參數(shù)的實(shí)例
        self.eye1 = Sense(self.cvns, eye1_points, self.fill)              # 眼2:帶參數(shù)的實(shí)例
        self.mouth = Sense(self.cvns, self.mouth_cacu(), (-10, -170))  # 口:帶參數(shù)的實(shí)例
 
    def draw(self):
        # 繪制臉部各部位
        self.face.draw()
        self.eye0.draw()
        self.eye1.draw()
        self.mouth.draw()
 
    def face_cacu(self):             # 臉坐標(biāo)計算
        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坐標(biāo)計算
        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):            # 口坐標(biāo)計算
        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):
        self.cvns = cvns
        self.start_point = start_point
        self.w = w
        self.h = h
        self.fill = fill
        self._button_size = 10        # 鈕扣的大小
        self.buttons = []
        self.bo = BodyOutline(self.cvns, self.body_cacu(), self.fill)      # 身體輪廓實(shí)例
#        self.hd = hand(self.cvns, (15, 500, 45, 240), self.fill)           # 左手輪廓實(shí)例,坐標(biāo)為矩形的兩個對角頂點(diǎn)的坐標(biāo)為準(zhǔn)畫的圓/橢圓
        self.hd = hand(self.cvns, self.bd_cacu(0), self.fill)           # 左手輪廓實(shí)例,坐標(biāo)為矩形的兩個對角頂點(diǎn)的坐標(biāo)為準(zhǔn)畫的圓/橢圓
        self.hd2 = hand(self.cvns, self.bd_cacu(self.w), self.fill)  # 右手
        for pnts in self.all_button_points():
            self.buttons.append(Button(self.cvns, pnts, self.fill))
 
    def bd_cacu(self, w):  # 計算手的坐標(biāo)
        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):
        self.bo.draw()                # 身體繪制
        self.hd.draw()                # 手1繪制
        self.hd2.draw()               # 手2繪制
        for bttn in self.buttons:    # 各鈕扣繪制
            bttn.draw()
 
    def body_cacu(self):           # 計算身體輪廓坐標(biāo)
        x1, y1 = self.start_point
        x2 = x1 + self.w
        y2 = y1 + self.h
        return x1, y1, x2, y2
 
    def button0_cacu(self):        # 計算第0個鈕扣的坐標(biāo)
        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):          # 繪制每個鈕扣的坐標(biāo)
        b0_points = self.button0_cacu()
        size = self.h / 6                   # 身高/鈕扣數(shù)+1
        points = []                         # 列表
        for i in range(5):                 # 鈕扣的個數(shù)
            points.append(self.move_dy(b0_points, i * size))   # 各鈕扣的移動數(shù)據(jù)存入列表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為雪人的坐標(biāo)其與帽子坐標(biāo)一致(見雪人圖)
        self.cvns = cvns
        self.points = points
        self.w = w
        self.h = h
        self.fill = fill
        self.head = Head(self.cvns, (self.points[0], self.points[1] + self.h / 6), self.fill, self.w, self.h / 3)   # 實(shí)例化頭部
        self.body = Body(self.cvns, (self.points[0], self.points[1] + self.h / 2), self.fill, self.w, self.h / 2)   # 實(shí)例化身體
        self.fill = 'red'                                                            # 帽子頂部顏色
        self.hat = Hat(self.cvns, self.points, self.fill, self.w, self.h / 6)        # 繪帽子                             # 實(shí)例化帽子
 
    def draw(self):
        self.hat.draw()         # 繪制帽子
        self.head.draw()        # 繪制頭
        self.body.draw()        # 繪制身體
 
if __name__ == '__main__':
    import tkinter
    root = tkinter.Tk()         # 建立根窗口
    cvns = tkinter.Canvas(root, width=400, height=700, bg='white')   # 調(diào)用畫布
    cvns.pack()                 # 將畫布添加到窗口
    snow = Snow(cvns, (30, 15), 'white', 320, 660)   # 雪人的實(shí)例化(傳入畫布對象、起始坐標(biāo)、寬、高)
    snow = snow.draw()          # 繪制
    root.mainloop()
 

到此這篇關(guān)于Python畫圖小案例之小雪人超詳細(xì)源碼注釋的文章就介紹到這了,更多相關(guān)Python 雪人 內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 一起來看看五條Python中的隱含特性

    一起來看看五條Python中的隱含特性

    這篇文章主要為大家詳細(xì)介紹了Python中的隱含特性,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-01-01
  • 三行Python代碼提高數(shù)據(jù)處理腳本速度

    三行Python代碼提高數(shù)據(jù)處理腳本速度

    Python是一門非常適合處理數(shù)據(jù)和自動化完成重復(fù)性工作的編程語言,我們在用數(shù)據(jù)訓(xùn)練機(jī)器學(xué)習(xí)模型之前,通常都需要對數(shù)據(jù)進(jìn)行預(yù)處理,而Python就非常適合完成這項(xiàng)工作。本文將為大家介紹如何利用Python代碼讓你的數(shù)據(jù)處理腳本快別人4倍,需要的可以參考一下
    2022-03-03
  • python的id()函數(shù)解密過程

    python的id()函數(shù)解密過程

    id()函數(shù)在使用過程中很頻繁,為此本人對此函數(shù)深入研究下,曬出代碼和大家分享下,希望對你們有所幫助
    2012-12-12
  • python中字典取最大值的應(yīng)用方式

    python中字典取最大值的應(yīng)用方式

    這篇文章主要介紹了python中字典取最大值的應(yīng)用方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-02-02
  • python抓取京東價格分析京東商品價格走勢

    python抓取京東價格分析京東商品價格走勢

    本文介紹使用python抓取京東價格的代碼,用于分析京東商品價格走勢或者用于其它,大家參考使用吧
    2014-01-01
  • odoo字段訪問控制的操作方法

    odoo字段訪問控制的操作方法

    在 Odoo 中,可以通過幾種方式實(shí)現(xiàn)字段的訪問控制?0c;包括通過模型安全規(guī)則、記錄規(guī)則和字段屬性來限制字段的訪問,這篇文章主要介紹了odoo字段訪問控制的相關(guān)操作,感興趣的朋友跟隨小編一起看看吧
    2024-03-03
  • pycharm 實(shí)現(xiàn)光標(biāo)快速移動到括號外或行尾的操作

    pycharm 實(shí)現(xiàn)光標(biāo)快速移動到括號外或行尾的操作

    這篇文章主要介紹了pycharm 實(shí)現(xiàn)光標(biāo)快速移動到括號外或行尾的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-02-02
  • 教你利用python實(shí)現(xiàn)企業(yè)微信發(fā)送消息

    教你利用python實(shí)現(xiàn)企業(yè)微信發(fā)送消息

    今天帶大家來練習(xí)python實(shí)戰(zhàn),文中對利用python實(shí)現(xiàn)企業(yè)微信發(fā)送消息作了詳細(xì)的圖文解說及代碼示例,對正在學(xué)習(xí)python的小伙伴很有幫助,需要的朋友可以參考下
    2021-05-05
  • 簡單介紹Python中的decode()方法的使用

    簡單介紹Python中的decode()方法的使用

    這篇文章主要介紹了簡單介紹Python中的decode()方法的使用,是Python入門學(xué)習(xí)當(dāng)中必須掌握的基礎(chǔ)知識,需要的朋友可以參考下
    2015-05-05
  • Python 無限級分類樹狀結(jié)構(gòu)生成算法的實(shí)現(xiàn)

    Python 無限級分類樹狀結(jié)構(gòu)生成算法的實(shí)現(xiàn)

    這篇文章主要介紹了Python 無限級分類樹狀結(jié)構(gòu)生成算法的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-01-01

最新評論