Chinese Valentine’s Day is here—a romance that belongs to Python, I wish you a successful confession

Record the days we walked together

Tell the stories you experienced with your dear TA

  • those first impressions
  • those romantic beginnings
  • The big and small things that are kept in mind
  • those twists and turns
  • the happiness and joy of those experiences
  • those precious moments
  • Those hopes/plans for the future
    ...

creative code confession

Sprinkle dog food in the way of programmers, professional and romantic, worth having!
insert image description here

insert image description here

turtle

The power of python is that it has many powerful libraries, and turtle is a module that can interactively paint. It is also a secret magic weapon to enhance the fun of learning python for beginners!

As an art idiot, it may be difficult to draw a picture, but with python's turtle library, you can achieve painting with only a few lines of code [1].

Turtle is a library that comes with python, just call it directly.
insert image description here

When using the turtle library, we need to conceive some logic and direction of the project, and some most importantly, 坐标after
familiarizing with the turtle functions, we can complete the composition efficiently and quickly!

import turtle
import time


# 清屏函数
def clear_all():
    turtle.penup()

    turtle.goto(0, 0)
    turtle.color('white')
    turtle.pensize(800)
    turtle.pendown()
    turtle.setheading(0)
    turtle.fd(300)
    turtle.bk(600)


# 重定位海龟的位置
def go_to(x, y, state):
    turtle.pendown() if state else turtle.penup()


    turtle.goto(x, y)


# 画爱心
def draw_heart(size):
    turtle.color('red', 'pink')


    turtle.pensize(2)
    turtle.pendown()
    turtle.setheading(150)
    turtle.begin_fill()
    turtle.fd(size)
    turtle.circle(size * -3.745, 45)
    turtle.circle(size * -1.431, 165)
    turtle.left(120)
    turtle.circle(size * -1.431, 165)
    turtle.circle(size * -3.745, 45)
    turtle.fd(size)
    turtle.end_fill()


# 第一个画面,显示文字
def paintingOne():
    turtle.penup()


    turtle.goto(-300, 0)
    turtle.color('pink')
    turtle.write('时光让我们相遇,七夕快乐!!!', font=('楷体', 32, 'normal'))
    time.sleep(3)


# 画出人物
def draw_people(x, y):
    turtle.penup()


    turtle.goto(x, y)
    turtle.pendown()

    turtle.pensize(4)
    turtle.color('black')

    turtle.setheading(0)
    turtle.circle(60, 360)

    turtle.penup()
    turtle.setheading(90)
    turtle.fd(75)

    turtle.setheading(180)
    turtle.fd(20)

    turtle.pensize(4)
    turtle.pendown()

    turtle.circle(2, 360)
    turtle.setheading(0)

    turtle.penup()
    turtle.fd(40)
    turtle.pensize(4)
    turtle.pendown()
    turtle.circle(-2, 360)

    turtle.penup()
    turtle.goto(x, y)
    turtle.setheading(-90)
    turtle.pendown()

    turtle.fd(20)
    turtle.setheading(0)
    turtle.fd(35)
    turtle.setheading(60)
    turtle.fd(10)

    turtle.penup()
    turtle.goto(x, y)
    turtle.setheading(-90)
    turtle.pendown()

    turtle.fd(40)
    turtle.setheading(0)
    turtle.fd(35)
    turtle.setheading(-60)
    turtle.fd(10)
    turtle.penup()
    turtle.goto(x, y)
    turtle.setheading(-90)
    turtle.pendown()
    turtle.fd(60)
    turtle.setheading(-135)

    turtle.fd(60)
    turtle.bk(60)
    turtle.setheading(-45)

    turtle.fd(30)
    turtle.setheading(-135)

    turtle.fd(35)
    turtle.penup()


# 第二个画面,显示发射爱心的小人
def paintingTwo():
    turtle.speed(10)


    draw_people(-250, 20)

    turtle.penup()
    turtle.goto(-150, -30)
    draw_heart(14)

    turtle.penup()
    turtle.goto(-20, -60)
    draw_heart(25)

    turtle.penup()
    turtle.goto(250, -100)

    draw_heart(45)

    turtle.hideturtle()
    time.sleep(100)


def Main():
    turtle.setup(900, 500)


    paintingOne()
    clear_all()

    paintingTwo()
    clear_all()
    turtle.done()
if __name__ == '__main__':
    Main()

references

Valentine's Day is not mine! ! !
Alas~~~~

Guess you like

Origin blog.csdn.net/qq_44936246/article/details/126167922