Python, turtle drawing study notes and examples

A , since this time the protagonist is talking turtle library, it certainly had to first understand what it is

  turtle Python language library is a library in popular drawing an image, imagine a small turtle, a horizontal axis is x, the vertical axis represents the y coordinate origin, (0,0) starting position, in accordance with a set of functions that control command, is moved in the plane coordinate system, so as to draw a pattern on its path crawling.

Two , basic knowledge turtle drawing

1. canvas (Canvas)

Turtle canvas is a drawing area for us to expand, we can set its initial size and position.

Set the canvas size

turtle.screensize (canvwidth = None, canvheight = None, bg = None), parameters are width (in pixels), high color canvas background.

如:turtle.screensize(800,600,"green")

turtle.screensize () # returns the default size (400, 300)

turtle.setup (width = 0.5, height = 0.75, startx = None, starty = None), parameters: width, height: width and height of the input integer, represents a pixel; is a decimal, represents the ratio of occupying computer screen, ( startx, starty): this indicates the position coordinates of the top left vertex of a rectangular window, if empty, the center of the screen window.

如:turtle.setup(width=0.6,height=0.6)

turtle.setup(width=800,height=800,startx=100,starty=100)

2. Brush

2.1 Paintbrush state

On the canvas, by default a canvas coordinate origin is the center axis, has an x-axis positive direction facing the coordinate origin small turtles . Here we describe the use of a small turtle two words: the origin of coordinates (position), facing the positive x-axis direction (direction), Turtle drawing, a state is small turtles described use position (brush) of.

2.2 properties of a pen

Brushes (brush attributes, color, width, line drawing, etc.)

1) turtle.pensize (): Set the pen width;

2) turtle.pencolor (): none is passed, returns the current pen color, incoming pen color parameters, it can be a string, such as "green", "red", may be RGB3 tuple.

3) turtle.speed (speed): setting the moving speed of the brush, brushed speed range [0,10] integer, the larger the number, the faster.

2.3 drawing commands

Actuating turtle drawing has many commands that can be divided into three types: one for the motion commands , one for the brush control command , there is a global control command .

(1) brush movement command

 

(2) a control command to the brush

 

 

 (3) a global control command

 

 

 

 (4) Other commands

 

 

 

3. Detailed command

3.1turtle.circle(radius,extent=None,steps=None)

Description: draw a circle with a given radius

parameter:

RADIUS (radius): radius of positive (negative), the center circle represents the left side of the brush (right);

extent (radian) (optional);

steps (optional) (do radius circle radius of an inscribed regular polygon, the polygon number of sides steps).

For example:

circle(50)#整圆;

circle(50,steps=3)#三角形;

circle(120,180)#半圆

4. Examples

1, sunflowers

 

 

 

 

 

2. The five-pointed star

 

 

 

 

 

 

 Summary: with turtle drawing that you want to remember to use each command, which is the most basic. But want to draw a good map or need logical thinking, mind programming is really important!

Guess you like

Origin www.cnblogs.com/ngxt/p/11519610.html