Python (under Windows) comes with a very interesting turtle drawing program (turtle), which is the protagonist of this series of courses.
In Pycharm, create a new project, and then in the Code Editor, enter
1 Import Turtle 2 turtle.shape ("turtle")3 turtle.exitonclick ()
Run this code and you can summon this lovely elf.
In the code above import turtle for importing modules, After importing, you can apply the various functions of the module
turtle. Shape ( "turtle"
Turtle. Exitonclick() The last sentence is to tell the turtle to keep it until it exits with a mouse click (exit on Click). Otherwise the program executes too quickly and we don't have time to see the turtle, and it's done and exited.
Prompted
- Please do not copy, paste the code directly, but against the code, you in the Code Editor 1.1 points to enter
- Python is a case-sensitive programming language, please be careful when entering code, note the case of letters
- One of the special aspects of Python compared to other high-level programming languages is that it enforces indentation, and do not arbitrarily add whitespace characters at the beginning of each line of code (spaces, TAB, etc.)
Now the baby turtle is in the center of the form, and the glans points to the right direction.
Next, we try to get the turtles to take two steps.
Close the window (or click Auto-exit anywhere on the form) to change the code in the editor to look like this
1 Import Turtle 2 turtle.shape ("turtle")3 turtle.forward ($) 4 Turtle.exitonclick ()
Run the code,
We see the little turtle starting from the center of the screen, along the head of the glans (right), crawling (forward) a distance.
This time, the code is not much different from the initial code, adding only 1 statements turtle. ForwardIt means to let the turtle along the current glans point, advance 200 units of distance.
Just now we've learned how to make turtles crawl, and then we'll see how to get the turtles to turn around.
Close the window and change the code to look like this,
1 Import Turtle 2 turtle.shape ("turtle")3 turtle.left (4) Turtle.forward (5) Turtle.exitonclick ()
After running, the following effect is displayed
This time, before the turtle departs (located in the center of the form, glans to the right), we firstTurtle. left (a) rotates the turtle to a 45-degree turn (counterclockwise). At this point, the turtle remains in the center of the form, but the glans points to the top right. Next, let the turtle crawl in the direction of the changed glans, turtle. Forward, a line segment that points to the top right of the screen is drawn.
Practice
Try to get the little turtle to draw a square
Try to draw a square with 3 overlapping squares
"Original link" http://www.ipaomi.com/2017/11/11/python-0 basics-Quick Start-Fun Tutorial-Dr. Mi-turtle painting-turtle-1-God/
Python 0 Basics Quick Start Fun tutorial (Dr. Mi Turtle drawing Turtle) 1. Mystery friend