Python case 2 - turtle library to draw pentagram V_1.0

Hello everyone, I'm cabbage carrot balls

Life is too short, Python is a song

Achieve 100 million technicians

Drawing a five-pointed star


project import

Version V1.0: Drawing pentagrams in Python with turtle library

Project analysis - clarifying ideas

1. Analyze the problem: Analyze the computational part of the
problem 2. Identify the problem: Divide the input, processing, and output parts of the problem (IPO)
3. Design the algorithm: The core of the computational part

  • Import turtle library for graphics drawing (import turtle)

  • Using the functions in the turtle library to realize the drawing of the five-pointed star
    Draw the mind map of Pentagram V1.0

Knowledge point learning – from the ground up

Differences between the Turtle library and the previous program

  • input( ) and output( ) are not shown
  • no assignment statement
  • Most of the statements are in the form of ab( ) to use the method b() in a to call the function b() in the function library a

Turtle library drawing coordinate system

Turtle library drawing coordinate system


Turtle library shape drawing functions

  • turtle.forword(distance) the brush moves forward distance distance
  • turtle.backword(distance) The brush moves back distance distance
  • turtle.right(degree) The drawing direction is rotated to the right by degrees
  • turtle.exitonclick() Click to close the graphics window

Hands-on programming – hands-on, let the program run

reference code

"""
作者:白菜胡萝丸子
版本1.0:使用turtle库在python中绘制五角星
"""

import turtle


def main():

    turtle.speed(1)
    # 绘制五角星的第一条边
    turtle.forward(100)
    turtle.right(144)

    # 绘制五角星的第二条边
    turtle.forward(100)
    turtle.right(144)
    # 绘制五角星的第三条边
    turtle.forward(100)
    turtle.right(144)
    # 绘制五角星的第四条边
    turtle.forward(100)
    turtle.right(144)
    # 绘制五角星的第五条边
    turtle.forward(100)
    turtle.right(144)

    turtle.exitonclick()


if __name__ == "__main__":
    main()

Results of the

insert image description here


Take it to the next level - let the program evolve a little bit

  • What problems did you find in the program for drawing five-pointed stars in version V1.0? Is it a lot of repetitive code, please try to use the loop structure to make the code more concise

Guess you like

Origin blog.csdn.net/coco264/article/details/123493013