Moment For Technology

✨ Python introduction ✨ 3️ draw a big sun ☀️, and even a crayon little ❤️ | Python theme month

Posted on Dec. 2, 2022, 7:46 p.m. by 張宜君
Category: The back-end Tag: python The back-end

This article is participating in Python Theme Month. See the link to the event for more details

preface

The last one wrote a small program of temperature conversion, quite a little sense of achievement. I learned that Python can also draw pictures, so I asked him to use a ☝? function (turtle graphics) : turtle -- Turtle graphics.

So, I said to the coach: this time I want to draw a big sun ☀️~

Refuze ??ヾ(◍° °◍) new air

You read that right, it is the big sun!!

The text start

Before we get started, let's briefly introduce the turtle drawing function.

Please refer to the official tutorial:Docs.python.org/zh-cn/3/lib...

Here are some of the methods you might use in this article:

The setup () sets the showturtle | st () () shows that sea turtles pensize () | width () brush thickness pencolor () the brush color penup | pu | up () () () brush up the pendown | pd () () | down () the brush down the goto () | setpos () | setposition () to/positioning setheading () | Seth () to set the color to color () forward () | fd () on the left () | lt () left begin_fill () to fill end_fill () to end filling circle () circle exitonclick () when click exitCopy the code

Roughly understand the line, start the actual combat!

1 Create a project file

The first step, ☝?, is to turn on PyCharm and quickly create a new project followed by a Python file ? :

2 Clear logic

No matter before compiling what program, my habit: logic must first wisp clear!!

Since the turtle drawing function can only be completed in one stroke, it may not be able to achieve the display effect of ☀️; So I sketched it:☀️ at a glance, roughly composed: 1 circle and 8 equilateral triangles. So, presumably, the logic is:

  • Step 1: Cut the figure into 8 parts, each part is as shown below:

  • Step 2: Use turtle drawings to draw the section, dividing it into an equilateral triangle and an arc:
from turtle import *
# Brush up
pu()
# Position the start position
goto(-30.100)
# Brush down
pd()
# Set orientation
seth(0)
pencolor('#EEC211')
# 80 steps forward
fd(80)
120 degrees to the left
lt(120)
# 80 steps forward
fd(80)
120 degrees to the left
lt(120)
# 80 steps forward
fd(80)
# 143 degrees left
lt(143)
# Draw down an arc of radius 100 68 steps
circle(-100.68)
The window is closed manually by clicking
exitonclick()
Copy the code

After running, the effect is as follows:

  • Step 3: Repeat the for loop 8 times to splice the full sun shape:
from turtle import *
# Brush up
pu()
# Position the start position
goto(-30.100)
# Brush down
pd()
# Set orientation
seth(0)
pencolor('#EEC211')

# Loop 8 times
for i in range(8) :# 80 steps forward
    fd(80)
    120 degrees to the left
    lt(120)
    # 80 steps forward
    fd(80)
    120 degrees to the left
    lt(120)
    # 80 steps forward
    fd(80)
    # 143 degrees left
    lt(143)
    # Draw down an arc of radius 100 68 steps
    circle(-100.68)

The window is closed manually by clicking
exitonclick()
Copy the code

The running effect is as follows:

  • Step 4: Color the drawing
from turtle import *
# Brush up
pu()
# Position the start position
goto(-30.100)
# Brush down
pd()
# Set orientation
seth(0)
pencolor('#EEC211')
# Set color
color('#EEC211')

# Loop 8 times
for i in range(8) :# Start filling color
    begin_fill()
    # 80 steps forward
    fd(80)
    120 degrees to the left
    lt(120)
    # 80 steps forward
    fd(80)
    120 degrees to the left
    lt(120)
    # 80 steps forward
    fd(80)
    # 143 degrees left
    lt(143)
    # Draw down an arc of radius 100 68 steps
    circle(-100.68)
    # End fill
    end_fill()

The window is closed manually by clicking
exitonclick()
Copy the code

The running effect is as follows: Found a small problem, the middle part has no fill color, need to find a way to add it.

  • Step 5: Fill the center with color

Just to give you a general idea, draw another circle in the middle and then overlay the fill color.

Color the center part
# Set color
color('#EEC211')
begin_fill()
left(12)
circle(-145)
end_fill()
exitonclick()
Copy the code

The running effect is as follows:

  • Final step: Integrate the code and encapsulate the functions
# to draw the sun
def drawSun() :
    for i in range(8) :# Start filling color
        begin_fill()
        # 80 steps forward
        fd(80)
        120 degrees to the left
        lt(120)
        # 80 steps forward
        fd(80)
        120 degrees to the left
        lt(120)
        # 80 steps forward
        fd(80)
        # 143 degrees left
        lt(143)
        # Draw down an arc of radius 100 68 steps
        circle(-100.68)
        # End fill
        end_fill()

# main program
def main() :
    # Brush Color
    pencolor('#EEC211')
    # Brush up
    pu()
    # Position the start position
    goto(-20.100)
    # Brush down
    pd()
    # Set orientation
    seth(0)
    # Set color
    color('#EEC211')
    # to draw the sun
    drawSun()
    Color the center part
    begin_fill()
    left(12)
    circle(-145)
    end_fill()
    exitonclick()
Copy the code

The running effect is as follows: Oak, we're done! ✌️ That's all for today's study, see you next time ~ ?

The complete code is as follows:

from turtle import *

def drawSun() :
    for i in range(8) :# Start filling color
        begin_fill()
        # 80 steps forward
        fd(80)
        120 degrees to the left
        lt(120)
        # 80 steps forward
        fd(80)
        120 degrees to the left
        lt(120)
        # 80 steps forward
        fd(80)
        # 143 degrees left
        lt(143)
        # Draw down an arc of radius 100 68 steps
        circle(-100.68)
        # End fill
        end_fill()


def main() :
    # Brush Color
    pencolor('#EEC211')
    # Brush up
    pu()
    # Position the start position
    goto(-20.100)
    # Brush down
    pd()
    # Set orientation
    seth(0)
    # Set color
    color('#EEC211')
    # to draw the sun
    drawSun()
    Color the center part
    begin_fill()
    left(12)
    circle(-145)
    end_fill()
    exitonclick()

main()
Copy the code

Write in the last

I was surprised to learn that Python can not only crawl, but also draw, it is very interesting, I have seen friends drawCrayon small newAnd share it with your friends:How interesting!! Really is the god also ~ ?


That's the end of this sharing

If you think the article is helpful to you, like, collect, follow, comment, one key four support, your support is the biggest motivation for my creation.

Search
About
mo4tech.com (Moment For Technology) is a global community with thousands techies from across the global hang out!Passionate technologists, be it gadget freaks, tech enthusiasts, coders, technopreneurs, or CIOs, you would find them all here.