current position:Home>Using Python to draw the midautumn festival moon cake case, everybody happy Mid-Autumn festival

Using Python to draw the midautumn festival moon cake case, everybody happy Mid-Autumn festival

2022-09-09 01:24:38Entering code...

文章目录

2022.9.10 As the Mid-Autumn festival and teacher's day,Here I wish you all family,中秋快乐,I wish all the teachers plum garden,教师节快乐!For everybody present here is a Python 小项目 —— 使用 turtle 绘制月饼.

月.光辉,皎洁.Yao qiankun,Still open.A complete Mid-Autumn festival,Play for poem zhe.Yutu dysprosium difficult to wear,Cassia twig, fold.Vientiane as is selfless,Joan units have covered jun to restrain.Holding a harp to play don't crane sound,Don't cut not bosom friend.——【唐】李绅《Assign a month》

环境准备

macbook 运行环境准备

mac 默认 Python 版本为 3.8.x,在运行 turtle 时,Black screen flashing would happen.为了正常运行 turtle,We need to install updates Python 版本.

安装过程很简单,双击安装包

Always click to continue.

然后设置 python 和 pip Two commands to python3.10.6 和 pip3.10 ,The aim is to change systems mainly use Python 的版本,这样在命令行中输入 Python Will display the following:

Check is already installed on the command line Pyhton 包:

那么,到底是怎么实现的呢?在家目录下(比如: /Users/zhangjiaming ),创建两个文件( touch .zshrc 、 touch bash_profile ),内容如下:

turtle 库

turtle 官网: https://pythonturtle.org

PythonTurtleCommitted to providing learning(Professor or)PythonThe threshold method programming language software development.Students use interactivePython shell(Similar to the free development environment)并使用PythonTurtles function mobile screen.An illustrated help to introduce students to the screenPython编程的基础知识,Demonstrates how to move a turtle.

turtle Is a very convenient Python 绘图库,Only need to use a simple way to process,Can draw many interesting graphics.

turtle To simulate the process of pen drawing:

  • 落笔 —— turtle.pendown()
  • 起笔 —— turtle.penup()
  • 画直线 —— turtle.forward(xxx)
  • The size of the pen —— turtle.pensize(xxx)
  • How many degrees to the left —— turtle.left(xxx)

Encapsulates an own function

Like a painting when,There is a pen——移动——The process of put pen to paper,We use these three steps to implement the following function to,否则,Brush brush will show the mobile trajectory.

def goto(x, y):
    turtle.penup()
    turtle.goto(x, y)
    turtle.pendown()

The moon cake body

Moon cakes are divided into the following several part of us:

  1. Most of the lateral lace

  2. The inside of the lace

  3. 圆形

  4. 矩形

  5. Center of petals

  6. 文字

def draw():
    
    # Most of the lateral lace
    turtle.pensize(20)
    goto(0, 0)
    turtle.color("#83572F") 
    for _ in range(30):
        turtle.left(12)
        turtle.begin_fill()
        turtle.forward(200)
        turtle.circle(25, 180)
        turtle.goto(0, 0)
        turtle.left(180)
        turtle.end_fill()
    
    # The inside of the lace
    goto(0, 0)
    turtle.color("#E1A43B") 
    for _ in range(30):
        turtle.left(12)
        turtle.begin_fill()
        turtle.forward(180)
        turtle.circle(25, 180)
        turtle.goto(0, 0)
        turtle.left(180)
        turtle.end_fill()
    
    # 圆形
    goto(0, -180)
    turtle.color("#F2C54D") 
    turtle.begin_fill()
    turtle.circle(180)
    turtle.end_fill()
    
    # 矩形
    x = 125
    y = 2*x
    turtle.pensize(15)
    turtle.color("#C97E2E") 
    goto(-x, x)
    turtle.pensize(10)
    turtle.forward(y)
    goto(-x, x-y)
    turtle.forward(y)
    goto(-x, x)
    turtle.right(90)
    turtle.forward(y)
    goto(x, x)
    turtle.forward(y)
    
    # Center of petals
    goto(0, 0)
    turtle.pensize(10)
    turtle.color("#36210E") 
    for _ in range(30):
        turtle.left(12)
        # turtle.begin_fill()
        turtle.forward(100)
        turtle.circle(25)
        turtle.goto(0, 0)

写一段文字

turtle Implements the rendering text function,We can call directly to achieve text effects.

def wirte():
    goto(200, -270) # 从哪里开始写
    turtle.color("black") # 画笔颜色
    turtle.write("祝:\n\n各位中秋节快乐,\n吉祥如意!\n\n , font=("Time",20,"bold")) # 书写内容、文字样式

完整代码:

import turtle

def goto(x, y):
    turtle.penup()
    turtle.goto(x, y)
    turtle.pendown()

def draw():
    
    turtle.pensize(20)
    goto(0, 0)
    turtle.color("#83572F") 
    for _ in range(30):
        turtle.left(12)
        turtle.begin_fill()
        turtle.forward(200)
        turtle.circle(25, 180)
        turtle.goto(0, 0)
        turtle.left(180)
        turtle.end_fill()
    
    goto(0, 0)
    turtle.color("#E1A43B") 
    for _ in range(30):
        turtle.left(12)
        turtle.begin_fill()
        turtle.forward(180)
        turtle.circle(25, 180)
        turtle.goto(0, 0)
        turtle.left(180)
        turtle.end_fill()
    
    goto(0, -180)
    turtle.color("#F2C54D") 
    turtle.begin_fill()
    turtle.circle(180)
    turtle.end_fill()
    
    x = 125
    y = 2*x
    turtle.pensize(15)
    turtle.color("#C97E2E") 
    goto(-x, x)
    turtle.pensize(10)
    turtle.forward(y)
    goto(-x, x-y)
    turtle.forward(y)
    goto(-x, x)
    turtle.right(90)
    turtle.forward(y)
    goto(x, x)
    turtle.forward(y)
        
    goto(0, 0)
    turtle.pensize(10)
    turtle.color("#36210E") 
    for _ in range(30):
        turtle.left(12)
        # turtle.begin_fill()
        turtle.forward(100)
        turtle.circle(25)
        turtle.goto(0, 0)
        # turtle.left(180)
        # turtle.end_fill()
    
    
        
def wirte():
    goto(200, -270)
    turtle.color("black")
    turtle.write("祝:\n\n各位中秋节快乐,\n吉祥如意!\n\n    Jia ming", font=("Time",20,"bold"))


if __name__ == '__main__':
    turtle.speed(9000)
    turtle.hideturtle() # 隐藏笔头
    
    draw()
    wirte()
    turtle.done()

最终效果

Click on the below business card for oh~备注“月饼” 

copyright notice
author[Entering code...],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/252/202209090113032382.html

Random recommended