PYTHON TURTLE🐢 WITH PRACTICAL
PYTHON TURTLE LIBRARY
- Programming With turtle
- Moving the Turtle
- Drawing a Shape
- Drawing Preset Figures
- Changing the Screen Color
- Changing the Screen Title
- Changing the Turtle Size
- Changing the Pen Size
- Changing the Turtle and Pen Color
- Filling in an Image
- Changing the Turtle Shape
- Changing the Pen Speed
- Customizing in One Line
- Picking the Pen Up and Down
- Undoing Changes
- Clearing the Screen
- Resetting the Environment
- Leaving a Stamp
- Cloning Your Turtle
Now, to open the turtle
screen, you initialize a variable for it in the following way:
You should see a separate window open up:
![Python Turtle Initial Screen New](https://files.realpython.com/media/Screenshot_2019-12-10_at_7.40.34_AM.86e4071c3bb4.png)
Programming With turtle
Moving the Turtle
There are four directions that a turtle can move in:
![Python Turtle Moving Updated](https://files.realpython.com/media/Update_-_Moving_Turtle_VIDEO_GIF.61623cf40fed.gif)
You can use the shortened versions of these commands as well:
t.rt()
instead oft.right()
t.fd()
instead oft.forward()
t.lt()
instead oft.left()
t.bk()
instead oft.backward()
![Python Turtle Coordinates New](https://files.realpython.com/media/Turtle_EDIT_Graph.790c213ce0ba.jpg)
Your output will look like this:
![Python Turtle GOTO NEWER](https://files.realpython.com/media/TURTLE_EDIT_GOTO_GIF.ac9b7de34b40.gif)
You’ve drawn a line from your current position to the point (100,100)
on the screen.
To bring the turtle back to its home position, you type the following:
Drawing a Shape
Your output will look like this:
![Python Turtle Square Edit Newer](https://files.realpython.com/media/TURTLE_SQUARE_EDIT.626bc3fccd67.gif)
Drawing Preset Figures
You’ll get an output like this:
![Python Turtle Circle Updated](https://files.realpython.com/media/Update_-_Turtle_Circle_GIF.14906fdf5060.gif)
You’ll get a filled-in circle like this:
![Python Turtle Dot Update](https://files.realpython.com/media/Turtle_Dot_Pic.8f171e2c7d98.png)
Changing the Screen Color
You can replace "blue"
with any other color. Try "green"
or "red"
. You’ll get a result like this:
![Python Turtle Background Color](https://files.realpython.com/media/1-BG_COLOR-GIF.8619d9e1783f.gif)
Changing the Screen Title
Your title bar will now display this:
![Python Turtle Screen Title Updated](https://files.realpython.com/media/Change_in_Screen_Title_UPDATE.bf645f90e3d0.jpg)
In this way, you can change the heading of your screen according to your preference.
Changing the Turtle Size
Your outputs will look like this:
![Python Turtle Shape Size Updated](https://files.realpython.com/media/Turtle_Shape_Size_Updated_GIF.3f31c5f85340.gif)
The numbers given are the parameters for the size of the turtle:
Changing the Pen Size
This results in an outcome like this:
![Python Turtle Pen Size More NEW](https://files.realpython.com/media/Pen_Size_GIF.4d1fb1beefd6.gif)
Changing the Turtle and Pen Color
- Change the color of the turtle: This changes the fill color.
- Change the color of the pen: This changes the outline or the ink color.
Now, to change the color of the turtle (or the fill), you type the following:
Your turtle will look like this:
![Python Turtle Fill Color Red](https://files.realpython.com/media/Turtle_Fill_Color_Red_Update.216d34fcf201.png)
To change the color of the pen (or the outline), you type the following:
Your turtle will look like this:
![Python Turtle Pen Color Updated Green](https://files.realpython.com/media/Turtle_Pen_Color_Updated.362202ac18cb.png)
To change the color of both, you type the following:
Your turtle will look like this:
![Python Turtle Color Single Line Updated](https://files.realpython.com/media/Turtle_Color_One_Line_Green_and_Red_Updated.060568e73634.png)
Filling in an Image
When you execute this code, you’ll get a triangle that’s filled in with a solid color, like this:
![Python Turtle Begin Fill End Fill New](https://files.realpython.com/media/Turtle_Begin_End_Fill_GIF.849f73374a22.gif)
Changing the Turtle Shape
The shape of the turtle will change accordingly, like this:
![Python Turtle Shapes](https://files.realpython.com/media/4-Turtle_Shape-Gif.daf6a648bd8c.gif)
You have a couple of other options that you can try as well:
Changing the Pen Speed
![Python Turtle Speed Updated](https://files.realpython.com/media/Update_-_Turtle_Speed_1_and_10.a35c56e8f016.gif)
Customizing in One Line
Suppose you want to set your turtle’s characteristics to the following:
From what you’ve just learned, the code should look something like this:
It’s pretty long, but not that bad, right?
This will give you a result like this:
![Python Turtle Single Line Pen Newer](https://files.realpython.com/media/TURTLE_EDIT_SINGLE_LINE_CUSTOMISATION_GIF.c30c0839af72.gif)
Picking the Pen Up and Down
When you run this code, your output will look like this:
![Python Turtle Pen Up Pen Down Edit](https://files.realpython.com/media/Screenshot_2019-10-01_at_9.22.37_PM.20eeea07c674.png)
Undoing Changes
Clearing the Screen
Resetting the Environment
Leaving a Stamp
Your output will look like this:
![Python Turtle Stamps Edit](https://files.realpython.com/media/RP_STAMPS_GIF.05609e29d05f.gif)
Cloning Your Turtle
The output will look like this:
![Python Turtle Clone NEWER](https://files.realpython.com/media/TURTLE_EDIT_CLONE_GIF.1736204d0292.gif)
Comments
Post a Comment