使用python繪製月餅,慢慢等待將至的中秋節

專注的阿熊發表於2021-09-17

# -*- coding: UTF-8 -*-

"""

# @Time: 2021/9/14 21:13

# @Author: 遠方的星

# @CSDN: https://blog.csdn.net/qq_44921056

"""

import turtle as turtle

import math

turtle.hideturtle()

turtle.speed(10)

class MoonCake(object):

     def __init__(self, name: str):

         self.name = name

     #   畫月餅的花邊

     def external_pattern(self, r: int, n: int):  # r 為外部花邊的圓的半徑; n 為外部花邊的個數

         turtle.penup()

         turtle.goto(0, -r)

         turtle.pendown()

         round_r = math.sin(math.pi / n) * r  # 月餅花圈的半徑

         for i in range(n):

             turtle.penup()  # 畫筆抬起

             turtle.home()  # 恢復為初始位置

             turtle.seth((360/n) * i)  # 外匯跟單gendan5.com 改變畫筆方向,但不前進

             turtle.fd(r)

             turtle.left((360/n) * 0.5)  # 畫筆左轉一定的角度

             turtle.pendown()

             turtle.color('#F0BE7C')  # 設定顏色

             turtle.begin_fill()  # 開始填充顏色

             turtle.circle(round_r, 180)

             turtle.end_fill()

     # 畫內部紋路圖案

     def internal_pattern(self):

         turtle.color('#F5E16F')

         turtle.goto(0, -180)

         for _ in range(8):

             turtle.begin_fill()

             turtle.circle(60, 120)

             turtle.left(180)

             turtle.circle(60, 120)

             turtle.end_fill()

     # 畫圓的子函式,下文需要呼叫

     def draw_circle(self, r: int, pensize: int, color1: str, color2: str):

         turtle.penup()

         turtle.goto(0, -r)

         turtle.seth(0)

         turtle.pendown()

         turtle.pensize(pensize, )

         turtle.color(color1, color2)

         turtle.begin_fill()

         turtle.circle(r)

         turtle.end_fill()

     # 畫月餅內部的框架的子函式,下文需要呼叫

     def draw(self):

         turtle.title(" 提前祝您中秋快樂!!! ")  # 畫板視窗的標題

         self.external_pattern(200, 12)  # 月餅的外花邊

         self.draw_circle(200, 10, '#F0BE7C', '#F0BE7C')  # 畫上大圓圈

         self.draw_circle(180, 10, '#F8CD32', '#FBA92D')  # 畫上小圓圈

         self.internal_pattern()

         self.write_text(-105, -60)

         turtle.done()

     # 填寫月餅中間的文字

     def write_text(self, x: float, y: float):

         turtle.penup()

         turtle.goto(x, y)

         turtle.pendown()

         turtle.color('Gold')

         turtle.write(self.name, font=(" 華文隸書 ", 80, "bold"))  # 寫上文字

if __name__ == '__main__':

     MoonCake(' 團圓 ').draw()


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69946337/viewspace-2792642/,如需轉載,請註明出處,否則將追究法律責任。

相關文章