Python Turtle Graphics
In this page:
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?
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
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
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
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
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
Chapter Quiz — Complete all 15 topics to unlock
0/15 topics done
Complete these topics first:
- Python PEP 8 Style Guide
- Python Debugging Techniques
- Python Testing with unittest
- Python Common Mistakes
- Python Interview Questions
- Python map() & filter()
- Python reduce()
- Python zip() & enumerate()
- Python sorted() & key Functions
- Python Comprehensions Advanced
- Python Turtle Graphics
- Python tkinter Introduction
- Python tkinter Widgets
- Python pygame Introduction
- Python Mini Projects