Introduction to Python Turtle Graphics Basics of turtle graphics. Drawing simple shapes (lines, circles, squares).
Introduction to Python Turtle Graphics
Python's turtle module provides a simple yet powerful way to introduce programming concepts through graphical output. Turtle graphics is a popular way for introducing programming to kids and beginners. The turtle moves around the screen, drawing shapes based on the commands provided.
Getting Started with Turtle Graphics
First, you need to import the turtle module:
Python Code
import turtle
Setting Up the Screen and Turtle
Before you start drawing, you need to set up the screen and the turtle (the pen). Here���s how you can do it:
Python Code
# Create a screen object
screen = turtle.Screen()
# Create a turtle object
my_turtle = turtle.Turtle()
Drawing Simple Shapes
Drawing a Line
To draw a line, you can use the forward() method to move the turtle forward and the left() or right() method to change its direction.
Python Code
# Move the turtle forward by 100 units
my_turtle.forward(100)
# Turn the turtle 90 degrees to the right
my_turtle.right(90)
# Move the turtle forward by 100 units
my_turtle.forward(100)
Drawing a Square
A square has four equal sides and four right angles (90 degrees). You can draw a square by moving the turtle forward and turning it 90 degrees four times.
Python Code
for _ in range(4):
my_turtle.forward(100)
my_turtle.right(90)
Drawing a Circle
The circle() method can be used to draw a circle. You need to provide the radius as an argument.
Python Code
# Draw a circle with a radius of 50 units
my_turtle.circle(50)
Drawing a Triangle
A triangle has three sides and the angles add up to 180 degrees. An equilateral triangle has all sides equal and each angle is 60 degrees.
Python Code
for _ in range(3):
my_turtle.forward(100)
my_turtle.left(120)
Complete Example
Here���s a complete example that sets up the screen, draws a line, a square, a circle, and a triangle:
Python Code
import turtle
# Set up the screen
screen = turtle.Screen()
screen.title("Turtle Graphics Example")
# Create a turtle
my_turtle = turtle.Turtle()
my_turtle.speed(1) # Set the turtle speed (1 is slowest, 10 is fastest)
# Draw a line
my_turtle.forward(100)
my_turtle.right(90)
my_turtle.forward(100)
# Move the turtle to a new position
my_turtle.penup()
my_turtle.goto(-150, 0)
my_turtle.pendown()
# Draw a square
for _ in range(4):
my_turtle.forward(100)
my_turtle.right(90)
# Move the turtle to a new position
my_turtle.penup()
my_turtle.goto(150, 0)
my_turtle.pendown()
# Draw a circle
my_turtle.circle(50)
# Move the turtle to a new position
my_turtle.penup()
my_turtle.goto(0, -150)
my_turtle.pendown()
# Draw a triangle
for _ in range(3):
my_turtle.forward(100)
my_turtle.left(120)
# Hide the turtle and display the window
my_turtle.hideturtle()
screen.mainloop()
Summary
This introduction covers the basics of using the turtle module in Python to draw simple shapes. By combining different commands and loops, you can create more complex drawings. The turtle module is an excellent tool for beginners to learn programming concepts in a visual and interactive way.
Top of Form