Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Python - turtle.done()
Next article icon

“Turtle” is a Python feature like a drawing board, which lets us command a turtle to draw all over it! We can use functions like turtle.forward(...) and turtle.right(...) which can move the turtle around. Commonly used turtle methods are :
 

MethodParameterDescription
Turtle()NoneCreates and returns a new turtle object
forward()amountMoves the turtle forward by the specified amount
backward()amountMoves the turtle backward by the specified amount
right()angleTurns the turtle clockwise
left()angleTurns the turtle counterclockwise
penup()NonePicks up the turtle's Pen
pendown()NonePuts down the turtle's Pen
up()NonePicks up the turtle's Pen
down()NonePuts down the turtle's Pen
color()Color nameChanges the color of the turtle's pen
fillcolor()Color nameChanges the color of the turtle will use to fill a polygon
heading()NoneReturns the current heading
position()NoneReturns the current position
goto()x, yMove the turtle to position x,y
begin_fill()NoneRemember the starting point for a filled polygon
end_fill()NoneClose the polygon and fill with the current fill color
dot()NoneLeave the dot at the current position
stamp()NoneLeaves an impression of a turtle shape at the current location
shape()shapenameShould be 'arrow', 'classic', 'turtle' or 'circle'


Plotting using Turtle

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:  

  1. Import the turtle module
  2. Create a turtle to control.
  3. Draw around using the turtle methods.
  4. Run turtle.done().

So as stated above, before we can use turtle, we need to import it. We import it as : 

from turtle import *# orimport 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. 
 

Shape 1: Square

Python
# Python program to draw square# using Turtle Programmingimportturtleskk=turtle.Turtle()foriinrange(4):skk.forward(50)skk.right(90)turtle.done()

Output:

Shape 2: Star

Python3
# Python program to draw star# using Turtle Programmingimportturtlestar=turtle.Turtle()star.right(75)star.forward(100)foriinrange(4):star.right(144)star.forward(100)turtle.done()

Output:

Shape 3: Hexagon

Python
# Python program to draw hexagon# using Turtle Programmingimportturtlepolygon=turtle.Turtle()num_sides=6side_length=70angle=360.0/num_sidesforiinrange(num_sides):polygon.forward(side_length)polygon.right(angle)turtle.done()

Output:

Shape 4: parallelogram

Python
importturtle# Initialize the turtlet=turtle.Turtle()# Set the turtle's speedt.speed(1)# Draw the parallelogramforiinrange(2):t.forward(100)t.left(60)t.forward(50)t.left(120)

Output:

 

Shape 5 : Circle

Python
importturtle# Set up the turtle screen and set the background color to whitescreen=turtle.Screen()screen.bgcolor("white")# Create a new turtle and set its speed to the fastest possiblepen=turtle.Turtle()pen.speed(0)# Set the fill color to redpen.fillcolor("red")pen.begin_fill()# Draw the circle with a radius of 100 pixelspen.circle(100)# End the fill and stop drawingpen.end_fill()pen.hideturtle()# Keep the turtle window open until it is manually closedturtle.done()

Output:

 

Visitpythonturtle.org to get a taste of Turtle without having python pre-installed. The shell in PythonTurtle is a full Python shell, and you can do with it almost anything you can with a standard Python shell. You can make loops, define functions, create classes, etc. 

Some amazing Turtle Programs


1. Spiral Square Outside In and Inside Out 

Python
importturtle#Inside_Outwn=turtle.Screen()wn.bgcolor("light green")skk=turtle.Turtle()skk.color("blue")defsqrfunc(size):foriinrange(4):skk.fd(size)skk.left(90)size=size+5sqrfunc(6)sqrfunc(26)sqrfunc(46)sqrfunc(66)sqrfunc(86)sqrfunc(106)sqrfunc(126)sqrfunc(146)

Output: 
 


2. User Input Pattern 

Python
# Python program to user input pattern# using Turtle Programmingimportturtle#Outside_Inimportturtleimporttimeimportrandomprint("This program draws shapes based on the number you enter in a uniform pattern.")num_str=input("Enter the side number of the shape you want to draw: ")ifnum_str.isdigit():squares=int(num_str)angle=180-180*(squares-2)/squaresturtle.upx=0y=0turtle.setpos(x,y)numshapes=8forxinrange(numshapes):turtle.color(random.random(),random.random(),random.random())x+=5y+=5turtle.forward(x)turtle.left(y)foriinrange(squares):turtle.begin_fill()turtle.down()turtle.forward(40)turtle.left(angle)turtle.forward(40)print(turtle.pos())turtle.up()turtle.end_fill()time.sleep(11)turtle.bye()

3. Spiral Helix Pattern 

Python
# Python program to draw# Spiral  Helix Pattern# using Turtle ProgrammingimportturtleloadWindow=turtle.Screen()turtle.speed(2)foriinrange(100):turtle.circle(5*i)turtle.circle(-5*i)turtle.left(i)turtle.exitonclick()

Output: 


4. Rainbow Benzene 

Python
# Python program to draw# Rainbow Benzene# using Turtle Programmingimportturtlecolors=['red','purple','blue','green','orange','yellow']t=turtle.Pen()turtle.bgcolor('black')forxinrange(360):t.pencolor(colors[x%6])t.width(x//100+1)t.forward(x)t.left(59)

Output: 


 

Trees using Turtle Programming


 References: 



A

Amartya Saikia
Improve
Article Tags :
Practice Tags :

Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp