0% found this document useful (0 votes)
3 views18 pages

Python Turtle

The document explains the concept of coordinates and the XY plane, detailing how they are used in Python Turtle for drawing and positioning. It covers various aspects of Python Turtle, including window control, turtle movement, drawing shapes, filling colors, and creating animations. Additionally, it provides examples and explanations of using loops and interactive features in Turtle graphics.

Uploaded by

Umer Arain
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views18 pages

Python Turtle

The document explains the concept of coordinates and the XY plane, detailing how they are used in Python Turtle for drawing and positioning. It covers various aspects of Python Turtle, including window control, turtle movement, drawing shapes, filling colors, and creating animations. Additionally, it provides examples and explanations of using loops and interactive features in Turtle graphics.

Uploaded by

Umer Arain
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

What are Coordinates & XY Plane (for Python Turtle)

1. What are Coordinates?

 Coordinates mean the exact address or position of a point on a screen or graph.


 They are written as two numbers → (x, y)
 x → left or right
 y → up or down

Example:
(100, 50) → move 100 right and 50 up from the center.

2. What is the XY Plane?

 XY plane is a flat surface used for drawing or positioning.


 It has two axes:
o X-axis → horizontal line (left / right)
o Y-axis → vertical line (up / down)
 The point where both axes meet is called the Origin (0, 0).

↑ +Y (Up)
|
-X ← 0,0 → +X (Right)
|
↓ -Y (Down)

3. Why do we use Coordinates in Python Turtle?

 Turtle needs exact directions to move on the screen.


 We tell Turtle where to go using (x, y) coordinates
 Just like giving a location or address to a robot.

Example in Python:

import turtle
t = [Link]()
[Link](100, 50) # Turtle moves to (x=100, y=50)
What are Quadrants in XY Plane?
When the X-axis and Y-axis divide the XY plane, they create 4 sections.
These sections are called Quadrants.

Each quadrant has different signs (+ or − ) for X and Y values.


Q2 | Q1
(-x , +y) | (+x , +y)
-----------------------------→
Q3 | Q4
(-x , -y) | (+x , -y)

PYTHON TURTLE FROM ZERO TO MASTER

1. Introduction & Basics

 What is Python Turtle?


 What is Turtle Library?
 Where is Turtle used in real-world? (education, animation, logos, games)
 Importing turtle & basic setup
 Turtle vs Screen vs Canvas — difference explained clearly

2. Turtle Window Control

 Creating screen window


 Changing window size & title
 Changing background color & background image

3. Turtle (pen) Control

 Creating turtle object


 Forward, backward, left, right
 Understanding angles & directions (0°, 90°, 180°, 270°)
 Speed control (slow, fast, fastest)
 Penup(), Pendown()
 Hide turtle, Show turtle

4. Colors and Drawing Styling

 Change pen color


 Change pen thickness (pensize)
 Change background color
 RGB color mode custom colors
 Change turtle shape (arrow, turtle, circle)

5. Basic Shapes

 Drawing line
 Drawing square, rectangle, triangle, polygon (manual method)
 circle(), dot(), stamp() functions

6. Fill with Color

 begin_fill(), end_fill()
 Filling circle, rectangle, star, custom shape
7. Text & Labeling

 [Link]() function
 Font style, size, alignment
 Writing name/signature/logo

8. Loops with Turtle

 Using for loop to draw repeated shapes


 Drawing 36 rotated squares
 Simple Mandala design
 Flower patterns
 Starburst design

9. Advanced & Animation

 Move turtle smoothly (speed animations)


 Multiple turtles in one screen
 Turtle following circular path
 Turtle drawing spiral

10. Events & Interactivity

 Keyboard controls (arrow key movement - mini game)


 Mouse click drawing
 Make turtle drawing app
 Animation projects

11. Clear & Reset

 clear(), reset(), bye(), undo()


 Save drawing as image (screenshots)

12. MINI PROJECTS

 Write name with styles


 Draw Pakistan Flag
 Draw Heart + Name Text
 Digital Clock with Turtle
 Logo Design
 Kids Drawing App
What is Python Turtle?
Python Turtle is a drawing tool in Python.
It allows us to draw shapes, graphics, animations, games, logos, flags, etc. just by writing code.

Imagine a small robot (called Turtle) holding a pen.


Whatever instructions we give using Python, the turtle moves on the screen and draws.

✔ It is mainly used for:

 Teaching kids and beginners programming visually


 Making animations & fun projects
 Creating shapes, patterns, flags, logos
 Building mini games like Snake, Car Game, Maze

✅ It comes built-in with Python, no extra installation needed.

Example:

import turtle
t = [Link]()
[Link](100) # turtle moves forward and draws a line

This will open a window and draw a straight line.

2. Turtle Window Control


1) Creating the Screen Window

This is the first step before drawing anything — we must create a drawing window.

import turtle

screen = [Link]() # creates the turtle window

✅ This opens a blank white window where our turtle will draw.

2) Changing Window Title & Size

You can customize the window title and size:

[Link]("My First Turtle Program") # window name


[Link](width=800, height=600) # width x height in pixels

✅ Output:
 Title on top will change
 Window will resize as you want

3) Changing Background Color


[Link]("lightblue") # any color name

✅ You can use colors like: "black", "white", "yellow", "pink", "grey", etc.

4) Setting a Background Image (Optional + Advanced)


[Link]("[Link]") # Only .gif image supported

➡️You must have the image in the same folder as your Python file.

✅ Final Example Code


import turtle

screen = [Link]() # create window


[Link]("My Turtle Window") # set window title
[Link](800, 600) # window size
[Link]("lightgreen") # background color

t = [Link]() # create turtle


[Link](100) # draw

Turtle (Pen) Control


1) Creating the Turtle Object
import turtle

t = [Link]() # creates a turtle (pen)

Now this t is like a little ROBOT holding a PEN.

2) Turtle Position (X, Y Coordinates)


Every turtle starts at the center of the screen.

📍 Starting Position = (0, 0)


This is called the Origin Point.

 Right side = +X direction


 Left side = –X direction
 Up direction = +Y
 Down direction = –Y

📌 Example:

[Link](100, 50) # Go to X=100, Y=50

✅ Turtle directly jumps to that exact location.

3) Turtle Angle & Directions

By default, turtle always faces EAST (right side) at 0°

Direction Angle
East (right) → 0°
North (up) ↑ 90°
West (left) ← 180°
South (down) ↓ 270°

✅ So, movement depends on current ANGLE.

4) Movement Commands
[Link](100) # move 100 pixels forward
[Link](100) # move backward
[Link](90) # turn left by 90 degrees
[Link](45) # turn right by 45 degrees

5) Speed Control
[Link](1) # very slow
[Link](5) # medium
[Link](10) # fast
[Link](0) # fastest (no animation)
6) Pen Up / Pen Down
[Link]() # stop drawing while moving
[Link](100, 100)
[Link]() # start drawing again

Useful when you want to move without drawing.

7) Hide & Show Turtle


[Link]() # hides the turtle (only drawing visible)
[Link]() # shows him again

Colors and Drawing Styling


1) Change Pen Color (Line Color)
[Link]("red")
[Link](100)

➡️You can use any color name like "blue", "green", "yellow", "purple", etc.

2) Change Pen Thickness (Line Width)


[Link](5) # thicker line
[Link](100)

➡️Default size is 1. You can increase to draw bold shapes.

3) Change Background Color


[Link]("lightblue")

➡️This changes the entire window background.

4) RGB Color Mode (Custom Colors)

If basic color names are not enough, you can use RGB values (0–255 range).
[Link](255) # enable RGB mode
[Link](255, 0, 0) # pure red
[Link](100)

➡️Example RGB colors:

 (255, 0, 0) → Red
 (0, 255, 0) → Green
 (0, 0, 255) → Blue
 (255, 255, 0) → Yellow

5) Change Turtle Shape

The turtle cursor can look like different shapes:

[Link]("turtle") # looks like a turtle


# other options:
# "arrow", "circle", "square", "triangle", "classic"

Example:

[Link]("turtle")
[Link]("green")
[Link](3)
[Link](100)

✅ Now turtle looks like a real turtle moving forward 🐢

5. Basic Shapes
1) Drawing a Simple Line
[Link](100) # draws a 100-pixel straight line forward

✅ Turtle moves straight and draws a line.

2) Drawing a Square (Manual Method using loop)


for i in range(4):
[Link](100) # move forward
[Link](90) # turn right 90 degrees

✅ 4 equal sides + 90° turn = square.


3) Drawing a Rectangle (manual)
for i in range(2):
[Link](150) # length
[Link](90)
[Link](80) # width
[Link](90)

✅ Opposite sides equal.

4) Drawing a Triangle (manual)


for i in range(3):
[Link](120)
[Link](120)

✅ 3 sides, 120° turning.

5) Drawing Any Polygon (Hexagon Example — 6 sides)


for i in range(6): # change number of sides
[Link](80)
[Link](60) # 360 / number of sides = angle

✅ Formula: Turn angle = 360 ÷ sides

6) Draw Circle — built-in function


[Link](100) # draws circle of radius 100

✅ Very easy — no need to loop.

7) Draw Dot
[Link](50) # draws filled dot of size 50

✅ Dot = filled circle.


8) Stamp (print turtle shape)
[Link]("turtle")
[Link]() # leaves turtle's shape on screen

✅ Stamp = like printing turtle footprint 🐢

Fill with Color in Turtle


We use two commands to fill shapes:

 begin_fill() → start filling


 end_fill() → stop filling & apply color

Before that, we also set the fill color using:

[Link]("red")

✅ Example 1: Fill a Square with Color


[Link]("yellow") # Set fill color
t.begin_fill() # Start filling

for i in range(4): # Draw square


[Link](100)
[Link](90)

t.end_fill() # End filling

✅ The square will be filled with yellow.

✅ Example 2: Fill a Circle


[Link]("pink")
t.begin_fill()
[Link](80) # radius 80
t.end_fill()

✅ Pink circle is created.

✅ Example 3: Fill a Star


[Link]("orange")
t.begin_fill()
for i in range(5):
[Link](150)
[Link](144) # perfect star angle

t.end_fill()

✅ Star will be filled with orange color.

✅ Example 4: ANY CUSTOM SHAPE

You can fill any kind of drawing, not just simple shapes.

[Link]("red")
t.begin_fill()

[Link](100)
[Link](60)
[Link](100)
[Link](120)
[Link](100)

t.end_fill()

✅ As long as you close the shape, it will fill properly.

Text & Labeling


1) [Link]() Function

This function is used to write text on screen.

[Link]("Hello Turtle")

✅ It will write plain text at current turtle location.

2) Font Style, Size & Alignment


[Link]("Hello World", font=("Arial", 20, "bold"), align="center")

Breakdown:

 "Arial" → Font name


 20 → Font size
 "bold" → can be "normal", "bold", "italic"
 align= → "left", "center", "right"
3) Writing Name / Signature / Logo
[Link]()
[Link](0, -50) # move to bottom of screen
[Link]()
[Link]("By Aftix Labs", font=("Times New Roman", 24, "italic"), align="center")

✅ You can use this to add your name, signature, logo text, brand text under any design.

Loops with Turtle


Loops allow us to repeat shapes automatically without manually writing the same code again and again.

1) Using for loop to draw repeated shapes

Example: Draw 4 lines (like square pattern start)

for i in range(4):
[Link](100)
[Link](90)

✅ This draws a square in 4 lines of code instead of 8.

2) Drawing 36 Rotated Squares (Advanced pattern)


for i in range(36):
for j in range(4):
[Link](100)
[Link](90)
[Link](10) # rotate the square before drawing the next

✅ Beautiful spiral-square mandala effect.

3) Simple Mandala Design


for i in range(12):
[Link](100)
[Link](30)

✅ Draws 12 circles in a rotating pattern → Mandala art.


4) Flower Pattern
for i in range(18):
[Link](80)
[Link](20)

✅ Looks like a flower with petals.

5) Starburst Design
for i in range(36):
[Link](200)
[Link](200)
[Link](10)

✅ Beautiful bursting sun/star

7. Fill with Color (continued)

We already discussed:

 begin_fill(), end_fill() — tells turtle “start storing shape” and “now fill it with current pen color”.
 Any closed shape drawn between these two will be filled.

Let’s continue explaining more shapes for fill:

✅ Filling a Circle
[Link]("blue") # outline + fill color both
t.begin_fill()
[Link](80) # draw full circle (80 is radius)
t.end_fill()

✔ Circle will be filled automatically — no need to close manually.

✅ Filling a Rectangle / Square


[Link]("red")
t.begin_fill()
for i in range(4):
[Link](100)
[Link](90)
t.end_fill()

✔ Because square ends at the starting point (closed shape).

✅ Filling a Star (custom shape example)


[Link]("yellow")
t.begin_fill()
for i in range(5):
[Link](200)
[Link](144)
t.end_fill()

✔ The 144° automatically creates a star shape — and turtle fills it beautifully.

✅ Filling Custom Shape (Free style)


[Link]("purple")
t.begin_fill()
[Link](100)
[Link](45)
[Link](70)
[Link](135)
[Link](120)
[Link](90)
[Link](50)
t.end_fill()

✔ You can make any weird shape — just make sure it returns back to start!

8. Animation & Movement Speed Control

Now we control how fast turtle moves and make animations smooth or instant.

✅ speed() — Control Turtle Speed


[Link](1) # very slow (like human hand)
[Link](3) # slow
[Link](6) # normal
[Link](10) # very fast
[Link](0) # super fast (no animation, instant)

✔ Speed range is from 0 to 10


✔ speed(0) = animation OFF (instant result)
✔ speed(1) = turtle walks like turtle 🐢 (very slow)
✅ delay() — Control Gap Between Each Movement
[Link](50) # delay in milliseconds (50ms = 0.05 sec)

→ Mostly not needed. Only used when you want step-by-step teaching effect.

✅ hideturtle() and showturtle()


[Link]() # hide turtle arrow, only drawing visible
[Link]() # show turtle arrow again

✔ Useful after drawing, to make shape look clean.

✅ tracer() — Control Animation ON/OFF for COMPLETE Performance Boost


[Link](0) # turn off animation completely
# draw shapes here
[Link]() # force final update on screen

✔ Best for big designs or loops (1000+ lines)


✔ Draws instantly — no animation, super fast

9. Turtle Position & Absolute Movement

Until now turtle was moving relative (forward/backward from current position).
Now we control turtle exact location on screen — like teleporting.

✅ 1. [Link](x, y) → Direct jump to exact coordinates


[Link](100, 50) # move turtle instantly to (x = 100, y = 50)

✔ Origin (0,0) is center of screen


✔ +X → right, −X → left
✔ +Y → up, −Y → down

Example:
[Link]()
[Link](-200, 100) # top left side
[Link]()

✅ 2. [Link](x, y) → Same as goto()

Just another name. Both work same.

✅ 3. [Link]()
[Link]() # back to (0,0), facing EAST (0° direction)

✔ Perfect for resetting position without clearing drawing

✅ 4. [Link](angle) → Change direction without moving


[Link](90) # face toward UP (North)
[Link](180) # face toward LEFT (West)

📌 Useful before drawing big loops or shapes

✅ 5. [Link]() & [Link]()


print([Link]()) # (x, y)
print([Link]()) # current angle

✔ Good for debugging or teaching students how turtle works

10. Draw with Loops — Super Powerful & Fun! 🎨

Instead of drawing 1 shape manually…


We use loops to draw beautiful patterns automatically.

✅ 1. Draw 4 Lines → Square (without loop)


for i in range(4):
[Link](100)
[Link](90)
✔ Draws a perfect square
✔ range(4) = repeat 4 times

✅ 2. Draw 36 Squares → ROTATING SHAPE (Mandala Start)


for i in range(36): # repeat 36 times
for j in range(4): # inner loop → draw square
[Link](100)
[Link](90)
[Link](10) # rotate slightly

🎉 BOOM → Looks like flower / mandala / chakra diagram

✅ 3. Simple Flower Design


for i in range(36):
[Link](100)
[Link](10)

✔ 36 circles → looks like lotus flower 🌸

✅ 4. Starburst Pattern 🎆
for i in range(36):
[Link](150)
[Link](150)
[Link](10)

✔ Looks like sun rays / firework

You might also like