Pyhton confession code - romantic Christmas

Christmas coming up soon, so this time to write a confession by a small turtle module program

Development time: 2019-12-15

Development Tools: Sublime

Development Module: turtle

Here used knowledge turtle library, if not familiar with can see my previous blog:

turtle library related knowledge

 

First, we need to create a function to carry all initialization data, including the size of the canvas, brush size, color, and the moving speed of the brush

The picture shows to demonstrate examples, the canvas is provided a 600 * 800 size, the width of the red pen 5, at 10

Code as follows:

def initdata():
    t.setup(800,600)
    t.pencolor('red')
    t.pensize(5)
    t.speed(10)

 

 

After setting the relevant attributes, you can start painting the

First brush located in the center, if you want to move the pen will leave marks on the canvas, which is painted on the line,

But some lines, painting itself does not need, and this needs to be done so that at the same time moving the brush, the canvas without leaving any traces

Need to use up () and down (); two functions, such as the literal translation is generally acts as a brush to lift and put down their paintbrushes

So the operation to lift the brush - moved to a designated place - lay down his brushes

Code as follows:

ef move_pen(x,y):
    t.hideturtle()
    t.up()
    t.goto(x,y)
    t.down()
    t.showturtle()

 

Note: hideturtle () and showturtle () to hide and show the brush brush. There is no impact on the painting itself

 

Drawing figure presentation, where the hardest part?

Must be two semi-circular upper half of the love, it is convenient, we talk about this part of the proposed function alone do draw a semicircle

It did not draw function curve in turtle library, the closest only a circle.

But we can draw a circle by cutting method, that is, these arc of a circle as a polygon side length short

So we have to draw a for loop, continue through the small line deflection direction component

code show as below:

def hart_arc():
    for i in range(200):
        t.right(1)
        t.forward(2)

 

 

Now we begin to draw graphics draw ()

First, there must be confession confession confession persons and persons it, so let's create a name to store the name and sign

After to obtain information by calling the initialization function

Finally, you can manipulate the pen to start drawing.

First brush move to (0, -180) position, that is, the lower tip of love, which uses mobile functions just created,

After adjusting the direction of brush control, drawn from the completion of movement of the right side 224 of the lower portion of the front of caring

Then draw the upper part love call hart_arc () plotted on the left half of love,

Then adjust the direction of deflection of the brush 120 degrees to the left, then draw another semicircle

Finally, return to the initial position of the pen, draw a good first love, which was filled with pink.

The same can draw a second love, and finally to a "stone's throw double ❤" on it

 

After drawing is completed, we will record the output of the two names just

This need to use the write () function

code show as below:

DEF Draw (): 
    name = the INPUT ( " Please enter the name of confession: " ) 
    Sign = the INPUT ( " Please enter your name: " ) 
    initdata () 
    move_pen (0, -180 ) 
    t.left ( 140 ) 
    t.fillcolor ( " Pink " ) 
    t.begin_fill ()   
    t.forward ( 224 )   
    hart_arc () 
    t.left ( 120 ) 
    hart_arc () 
    t.forward ( 224 ) 
    t.end_fill ()   
    move_pen (X = 70, Y = 160.  )
    t.left (185) 
    t.circle(-110,185)  
    t.forward(50)
    move_pen(-180,-180)
    t.left(180) 
    t.forward(600)  
    move_pen(0,50)
    t.hideturtle() 
    t.color('#CD5C5C', 'red')  
    t.write(name, font=('Arial', 20, 'bold'), align="center")
    t.color('red', 'pink')
    time.sleep(2)
    move_pen(220, -180)
    t.hideturtle()
    t.write(sign, font=('Arial', 20), align="center")

 

Such confession code production is complete

 

 Thank you for support, the overall code is as follows:

import turtle as t
import time

def initdata():
    t.setup(800,600)
    t.pencolor('red')
    t.pensize(5)
    t.speed(10)

def move_pen(x,y):
    t.hideturtle()
    t.up()
    t.goto(x,y)
    t.down()
    t.showturtle()

def hart_arc():
    for i in range(200):
        t.right(1)
        t.forward(2)

def draw():
    name=input("请输入表白姓名:")
    sign=input("请输入你的大名:")
    initdata()
    move_pen(0,-180)
    t.left(140) 
    t.fillcolor("pink")
    t.begin_fill()  
    t.forward(224)  
    hart_arc() 
    t.left(120) 
    hart_arc() 
    t.forward(224)
    t.end_fill()  
    move_pen(x=70, y=160) 
    t.left(185) 
    t.circle(-110,185)  
    t.forward(50)
    move_pen(-180,-180)
    t.left(180) 
    t.forward(600)  
    move_pen(0,50)
    t.hideturtle() 
    t.color('#CD5C5C', 'red')  
    t.write(name, font=('Arial', 20, 'bold'), align="center")
    t.color('red', 'pink')
    time.sleep(2)
    move_pen(220, -180)
    t.hideturtle()
    t.write(sign, font=('Arial', 20), align="center")
def main():
    draw()
    time.sleep(5)
if __name__ == '__main__':
    main()

Guess you like

Origin www.cnblogs.com/lyy135146/p/12043733.html