← Back to Python Course | Chapter 14: Advanced Python & Tools | Lesson 11 of 15

Python Turtle Graphics

Turtle graphics आपको screen पर एक छोटे turtle को घुमाकर तस्वीरें बनाने देता है, बिल्कुल commands से किसी pen को guide करने जैसा। Forward, turn, और आपने एक shape बना ली।
Syntax
python
import turtle

t = turtle.Turtle()
t.forward(distance)
t.left(angle)
turtle.done()

Turtle Graphics क्या है?

Turtle graphics programming concepts को visually सीखने का एक beginner-friendly तरीका है: आप screen पर मौजूद एक turtle को drawing commands से control करते हैं, और उसका trail उसके चलने के साथ shapes बनाता जाता है।

इसे चलाने के लिए एक असली graphical display चाहिए -- Pyodide जैसे browser-based environments इसे सीधे web page में render कर सकते हैं।

उदाहरण: What is Turtle Graphics?

python
import turtle
t = turtle.Turtle()  # the on-screen turtle that draws as it moves
print(type(t))

Turtle को हिलाना

Turtle को जिस दिशा में वह अभी face कर रहा है उस दिशा में travel करने के लिए forward() और backward() से हिलाया जाता है, और अगले forward movement से पहले उसे किसी दिए गए degrees तक घुमाने के लिए left()/right() इस्तेमाल होता है -- यही relative movement model (absolute x/y coordinates नहीं) है जो turtle graphics को shapes बनाने के लिए intuitive बनाता है।

उदाहरण: Moving the Turtle

python
import turtle
t = turtle.Turtle()
t.forward(100)  # moves in the direction it's currently facing
t.left(90)  # rotates 90 degrees before the next move
t.forward(50)

Basic Shapes बनाना

एक loop के अंदर forward-and-turn commands की एक छोटी sequence को दोहराना regular shapes बनाने का तरीका है: एक square 'आगे बढ़ो, 90 degrees मुड़ो' की चार repetitions है, और कोई भी regular polygon अलग turn angle और repeat count के साथ उसी pattern को follow करता है।

उदाहरण: Drawing Basic Shapes

python
import turtle
t = turtle.Turtle()
for _ in range(4):  # four repetitions traces out a square
    t.forward(100)
    t.left(90)

Colors और Pens को Customize करना

color() pen का drawing color set करता है, pensize() set करता है कि खींची गई line कितनी मोटी है, और speed() control करता है कि turtle कितनी तेज़ी से अपना movement animate करता है -- ये purely cosmetic controls हैं जो shape को प्रभावित नहीं करते, सिर्फ यह कि वह कैसी दिखती है और कितनी जल्दी दिखाई देती है।

उदाहरण: Customizing Colors and Pens

python
import turtle
t = turtle.Turtle()
t.color("blue")  # sets the pen's drawing color
t.pensize(3)  # sets the line thickness
t.speed(1)  # controls animation speed, purely cosmetic
t.forward(100)

Color से Shapes भरना

begin_fill() किसी shape की outline को fill करने के लिए record करना शुरू करता है, और end_fill() उस outline को बंद करके enclosed area को current color से भर देता है।

begin_fill() के बाद end_fill() call करना भूल जाने से shape outlined तो रहती है पर unfilled रह जाती है, क्योंकि fill तभी असल में render होता है जब यह जोड़ी पूरी होती है।

उदाहरण: Filling Shapes with Color

python
import turtle
t = turtle.Turtle()
t.color("red")
t.begin_fill()  # starts recording the outline to fill
for _ in range(4):
    t.forward(100)
    t.left(90)
t.end_fill()  # closes the outline and fills it with the current color
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.