turtle python Python libraries and learning drawing program

Python library

Python language Java language similar to C, can be used a large number of external Libraries library in the installation package:

For example math, random, turtle and other libraries, other libraries installation by the user according to the code requirements.

Python way of library references

The first way

import <database name>

For example: Import Turtle ( Turtle library is a graphics rendering library in Python, which contains a variety of methods to graph )

If you need to use the function library function, use:
<library name> <function name>.

For example the use of a straight line drawn inside the library turtle FD () method, you have to use turtle.fd () format.

例  >> >import turtle
>> > turtle.fd(100)

The second way

from <database name> import <function name> from <library name> import *

Calling function does not require <library name> directly <function name>
Example>>> Import from Turtle *
>>> FD (100)

The difference with the way | two primers

Both reference no difference between running the program, you need to pay attention to: the function name if the first approach, user-defined

And you can function in the library of the same name, for example, the program can define your own fd () function if the second approach, the user

Program-defined function name can not be used in the library.

For example: the program can not define new fd () function, because the turtle in the library fd () function is called directly by fd ().

For starters, it is recommended to use the first approach, while not confusing yourself and others clearly know the called function

Provenance.

Draw a small python

Code:

 import turtle

def drawSnake (rad, angle, curtain, neckrad): 

      for i in range (1en) :
           turtle. circle (rad, angle)

           turtle. circle( -rad, angle)

      turtle . circle (rad, angle/2)

      turtle . fd (rad)
      turtle . circle (neckrad+1, 180)

      turtle . fd (rad*2/3) 

def main() :
      turtle. setup (1300, 800, 0, 0)

      pythonsize = 30
      turtle . pensize (pythonsize)

      turtle . pencolor ("blue")

      turtle . seth(-40)
      drawSnake (40 , 80, 5, pythonsize/2)

main ()

operation result:

 

 

 

 

 Finishing ideas:

(1) We want to draw a small snake, turtle must first call the library, and then define a function drawSnake draw pythons (rad, angle, len, neckrad) :,

A method which comprises drawing the python. According to the trajectory of a small snake, we need to define the paint strokes arc radius (here is the vector radius, directional),

And angle. Cycles to complete quote for painting the arc part. After completion of the loop portion, followed by a short arc, followed by a short straight line, followed by 180

The reversal, and finally a short straight line. turtle.circle is drawn from the arc, turtle.fd is a method of drawing a straight line.

(2) finished rendering method defined main function, to () function is initialized in the main, first, the window size and position of the drawing is initialized,

Then the thickness, the color of the brush is a brush, then initializes brush start direction. Finally, the function

drawSnake (rad, angle, len, neckrad): where the parameter assignment.

An inlet (3) program main ().

Guess you like

Origin www.cnblogs.com/yangbiao6/p/11520508.html