22 Python 3 - Turtle
Turtle is a special feathers of Python. Using Turtle, we can easily draw in a drawing board.
First we import the turtle module. Then create a window, next we create turtle object and using turtle method we can draw in the drawing board.
import turtle # moves the pen in the # forward direction by # 110 pixels turtle.forward(110) # changes the direction of # the pen by 110 degrees in the # left direction turtle.left(110) # moves the pen in the # forward direction in # the new direction by # 110 pixels turtle.forward(110)
To make use of the turtle methods and functionalities, we need to import turtle.”turtle” comes packed with the standard Python package and need not be installed externally. The roadmap for executing a turtle program follows 4 steps:
- Import the turtle module
- Create a turtle to control.
- Draw around using the turtle methods.
- Run turtle.done().
So as stated above, before we can use turtle, we need to import it. We import it as :
from turtle import * # or import turtle
After importing the turtle library and making all the turtle functionalities available to us, we need to create a new drawing board(window) and a turtle. Let’s call the window as wn and the turtle as skk. So we code as:
wn = turtle.Screen() wn.bgcolor("light green") wn.title("Turtle") skk = turtle.Turtle()
Now that we have created the window and the turtle, we need to move the turtle. To move forward 100 pixels in the direction skk is facing, we code:
skk.forward(100)
We have moved skk 100 pixels forward, Awesome! Now we complete the program with the done() function and We’re done!
turtle.done()
So, we have created a program that draws a line 100 pixels long. We can draw various shapes and fill different colors using turtle methods. There’s plethora of functions and programs to be coded using the turtle library in python. Let’s learn to draw some of the basic shapes.
Some turtle method
METHOD | PARAMETER | DESCRIPTION |
---|---|---|
Turtle() | None | It creates and returns a new turtle object |
forward() | amount | It moves the turtle forward by the specified amount |
backward() | amount | It moves the turtle backward by the specified amount |
right() | angle | It turns the turtle clockwise |
left() | angle | It turns the turtle counter clockwise |
penup() | None | It picks up the turtle’s Pen |
pendown() | None | Puts down the turtle’s Pen |
up() | None | Picks up the turtle’s Pen |
down() | None | Puts down the turtle’s Pen |
color() | Color name | Changes the color of the turtle’s pen |
fillcolor() | Color name | Changes the color of the turtle will use to fill a polygon |
heading() | None | It returns the current heading |
position() | None | It returns the current position |
goto() | x, y | It moves the turtle to position x,y |
begin_fill() | None | Remember the starting point for a filled polygon |
end_fill() | None | It closes the polygon and fills with the current fill color |
dot() | None | Leaves the dot at the current position |
stamp() | None | Leaves an impression of a turtle shape at the current location |
shape() | shapename | Should be ‘arrow’, ‘classic’, ‘turtle’ or ‘circle’ |
Example code
# import turtle library import turtle my_window = turtle.Screen() my_window.bgcolor("blue") # creates a graphics window my_pen = turtle.Turtle() my_pen.forward(150) my_pen.left(90) my_pen.forward(75) my_pen.color("white") my_pen.pensize(12)
Output
Draw a Square
Example code
# import turtle library import turtle my_pen = turtle.Turtle() for i in range(4): my_pen.forward(50) my_pen.right(90) turtle.done()
Output
Draw a star
Example code
# import turtle library import turtle my_pen = turtle.Turtle() for i in range(50): my_pen.forward(50) my_pen.right(144) turtle.done()
Output
Draw a Hexagon
Example code
# import turtle library import turtle polygon = turtle.Turtle() my_num_sides = 6 my_side_length = 70 my_angle = 360.0 / my_num_sides for i in range(my_num_sides): polygon.forward(my_side_length) polygon.right(my_angle) turtle.done()
Output
Draw a square inside another square box.
Example code
# import turtle library import turtle my_wn = turtle.Screen() my_wn.bgcolor("light blue") my_wn.title("Turtle") my_pen = turtle.Turtle() my_pen.color("black") def my_sqrfunc(size): for i in range(4): my_pen.fd(size) my_pen.left(90) size = size - 5 my_sqrfunc(146) my_sqrfunc(126) my_sqrfunc(106) my_sqrfunc(86) my_sqrfunc(66) my_sqrfunc(46) my_sqrfunc(26)
Output
Drawing of another pattern
Example code
# import turtle library import turtle my_wn = turtle.Screen() turtle.speed(2) for i in range(30): turtle.circle(5*i) turtle.circle(-5*i) turtle.left(i) turtle.exitonclick()
Output
Drawing of another pattern
Example code
# import turtle library import turtle colors = [ "red","purple","blue","green","orange","yellow"] my_pen = turtle.Pen() turtle.bgcolor("black") for x in range(360): my_pen.pencolor(colors[x % 6]) my_pen.width(x/100 + 1) my_pen.forward(x) my_pen.left(59)
Output
REF
https://www.tutorialspoint.com/turtle-programming-in-python
https://www.geeksforgeeks.org/turtle-programming-python/
https://www.geeksforgeeks.org/python-turtle-tutorial/
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 因为Apifox不支持离线,我果断选择了Apipost!
· 通过 API 将Deepseek响应流式内容输出到前端
2021-04-01 如何在excel中查找问号“?”星号“*”和“~”号
2021-04-01 通过GO的全名描述获得GO ID
2019-04-01 java.lang.ClassNotFoundException: org.apache.commons.fileupload.FileItemFactory
2015-04-01 pylab.show()没有显示图形图像(python的matplotlib画图包)
2012-04-01 如何在网上查某个基因的转录因子及启动子
2010-04-01 Windows 2003中IIS 6.0应用程序池回收和工作进程
2010-04-01 Reportviewer - Error: ASP.NET session has expired