Python Turtle Graphics Basics Reference Guide
1. Introduction to Turtle Graphics
The Python turtle
module allows you to create pictures and shapes by providing a virtual canvas. It's an excellent way to learn programming and create graphics. In Skulpt, you can use turtle graphics directly in the browser.
2. Setting Up the Turtle Environment
Before you start drawing, you need to import the turtle
module and create a turtle object:
import turtle t = turtle.Turtle()
3. Basic Movement Commands
The turtle can move forward, backward, and turn left or right.
Forward and Backward
t.forward(100) # Moves the turtle forward by 100 units t.backward(50) # Moves the turtle backward by 50 units
Turning Left and Right
t.left(90) # Turns the turtle left by 90 degrees t.right(45) # Turns the turtle right by 45 degrees
[Add image of the turtle's path here]
4. Pen Control
You can control the pen to change its color, size, or lift it up and down.
Changing Pen Color
t.pencolor("red") # Sets the pen color to red
Changing Pen Size
t.pensize(5) # Sets the pen thickness to 5 units
Pen Up and Pen Down
t.penup() # Lifts the pen; turtle moves without drawing t.pendown() # Puts the pen down; turtle draws when moving
5. Drawing Basic Shapes
Drawing a Square
for _ in range(4): t.forward(100) t.right(90)
[Add image of the square here]
Drawing a Circle
t.circle(50) # Draws a circle with radius 50 units
[Add image of the circle here]
6. Using Loops for Patterns
You can create complex patterns by combining loops with turtle commands.
Spiral Pattern Example
for i in range(36): t.forward(i * 5) t.right(144)
[Add image of the spiral pattern here]
7. Setting the Screen
The screen can be customized by setting its background color or size.
screen = turtle.Screen() screen.bgcolor("lightblue") # Sets the background color
8. Functions in Turtle Graphics
You can define functions to reuse drawing code.
Example: Drawing a Star
def draw_star(size): for _ in range(5): t.forward(size) t.right(144) draw_star(100)
[Add image of the star here]
9. Changing Turtle Speed
Adjust the speed at which the turtle moves.
t.speed(1) # Slowest speed t.speed(10) # Fastest speed
10. Hiding and Showing the Turtle
You can hide the turtle icon to see only the drawing.
t.hideturtle() # Hides the turtle icon t.showturtle() # Shows the turtle icon again
Congratulations! You've covered the fundamentals of programming the Python Turtle module.