Python Learning Notes Turtle class using __python

Source: Internet
Author: User

First, import
Import Mode one:

From Turtle Import *

If you import this way, you can use all of the methods of the library directly in the file.
For example: (Set a 300*300, brush in 100*100 position, draw a circle)

From turtle Import *
setup (300,300,100,100)/set interface to 300*300
pensize (2)
pencolor (' Blue ')
Circle (30)
Mainloop ()

Import Mode two:

Import Turtle

As in the example above, this method of import is written as follows

Import Turtle
T=turtle. Turtle ()
turtle.setup (300,300,300,100)
t.pensize (2)
t.pencolor (' Blue ')
t.circle
() Turtle.mainloop ()

Among them, turtle. Turtle () is an object of a brush.
The brush's properties are related to drawing and setting brushes.
Turtle.setup and Turtle.mainloop set the properties of the canvas.

Introduction to the two methods
Method of Drawing Canvas:
Setup (Width,height,floatx,floaty) to set the drawing area

Call Tracer before drawing starts (False)
Tracer after drawing finishes (True)
This method is shown directly to the user to draw the results, without the lengthy waiting for the drawing process, this method turtle, Turle. Turtle inside also have, the effect is the same.

Register_shape (Name,shape) & AddShape (Name,shape)
Source

AddShape = Register_shape

So the two methods are actually the same.
Register a graphic
Use:

Import Turtle

T=turtle. Turtle ()

turtle.setup (300,300,300,100)
t.pensize (2)
t.pencolor (' Blue ')
turtle.tracer (False)
t.penup ()
t.begin_poly () #开始记录多边形的顶点 for
I in range (6):
    T.forward (+)
    t.right
t.end _poly ()
#获取多边形
shape=t.get_poly ()
#注册多边形
turtle.register_shape ("shape", shape)
T.pendown ()
t.shape (' shape ') #绘制这个图形
turtle.tracer (True)
Turtle.mainloop ()

bgcolor (color) sets the background color of the screen
Eg:turtle.bgcolor (' Red ')

Bgpic (picname) Set screen background pattern
PS: This has not been set up successfully, in the future try

Start direction of mode (mode) brush
Default for standard, East direction
Logo North Direction
World coordinate system, this way is not very understanding
The source code reads:

' World ' uses userdefined ' worldcoordinates '. *attention*: In this
        mode angles appear distorted if x/y unit-ratio doesn ' t equal 1.
        If the is isn't given, return to the current mode.

It means:
If the x/y unit ratio is not equal to 1, the pattern angle is distorted. It's supposed to resemble Earth projections.

OnTimer (fun,t=0) timer, perform fun function periodically

Title (title) set the caption of the window
Bye () Destroy objects on the screen
Source:

    def _destroy (self):
        root = self._root
        If root is _screen._root:
            turtle._pen = none
            Turtle._screen = None
            _screen._root = none
            _screen._canvas = none
        turtlescreen._running = False
        Root.destroy ()

    def Bye (self): "" "
        shut the Turtlegraphics window.

        Example (for a Turtlescreen instance named screen):
        >>> Screen.bye () "" "
        Self._destroy ()

After you call the bye function, you cannot invoke the brush
eg

Turtle.bye ()
t.circle (60)

After execution:

Traceback (most recent):
  File "e:/python/myturtle.py", line 540, in <module>
    t.circle
  File "D:\Python27\lib\lib-tk\turtle.py", line 1903, in Circle
    self.speed (0)

Clearscreen () Clears the objects drawn in the screen
ColorMode (mode) sets how the brush's color is represented
The value of mode can only be 1 and 255.
The default mode=1 brush color can be set either of the following:

T.pencolor (' red ')
t.pencolor (' #aeffff ')

Of course, if mode=255, the above two settings are still in force.
mode=255 can call the T.pencolor (R,g,b) method, as follows:

T.pencolor (24,2,255)

But the above method is not allowed to be invoked when mode=1.
Delay (t) sets the brush delay time, which is similar to OnTimer, except that OnTimer has a drawing function that specifies its own, and delay calls the drawing function inside the turtle.
Source:

    def delay (self, Delay=none): "" "Return
        or set the drawing delay in milliseconds.

        Optional argument:
        delay-positive integer

        Example (for a Turtlescreen instance named screens):
        >> > Screen.delay
        >>> screen.delay () ""
        If delay is None: return
            self._ Delayvalue
        self._delayvalue = int (delay)

Delay interior will delay to _delayvalue, we look at this value, in fact, is the property of Turtlescreen. This value is used in _update.

def _update (self): "" "
        perform a turtle-data update.
        " " Screen = Self.screen
        If screen._tracing = 0:
            return
        elif screen._tracing = = 1:
            self._update_data ()
            self._drawturtle ()
            screen._update ()                  # turtlescreenbase
            screen._delay (screen._delayvalue) # Turtlescreenbase
        Else:
            self._update_data ()
            If Screen._updatecounter = 0: for
                T in Screen.turtles ():
                    t._drawturtle ()
                screen._update ()


    def _delay (self, delay): "" "
        delay Subsequent canvas actions for delay Ms. "" "
        self.cv.after (Delay)

And then look at the OnTimer function.

def _ontimer (self, Fun, T): "" "
        Install a timer, which calls fun after T milliseconds.
        " " If T = = 0:
            self.cv.after_idle (fun)
        else:
            self.cv.after (T, fun)

Exitonclick () Click the screen to exit

Method of the brush:
Pensize (size) Set brush size
PenColor (color) Set Brush color
Seth (Angle) set the direction of the brush
Just east to counterclockwise 0-360
Circle (Rad,angele) The radius of the Rad circle, to the left of the radius, to the right of the radius
Angle draws an angle value of 360 for a circle
FD (RAD) forward linear crawl distance rad
Penup () Brush lift
Pendown () Brush down
Begin_poly () & End_poly () a group
Record polygon paths
Get_poly () Gets the polygon of the record
Shapesize (stretch_wid,stretch_len,outline) Sets the size of the graphic, parameters: Width stretch, length stretch, graphic width
ResizeMode (Rmode) Set image size mode
Parameter Rmode value: "Auto" adapts the brush's appearance to the Pensize value.
"User" is set by Shapesize () according to the values of the Stretchfactor and Outlinewidth (outline) that fit the brush's appearance. Called by calling a shapeize with parameters.
The appearance of the "noresize" brush does not fit

Continuous finishing ...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.