Skip to content

Easily Create Turtle Drawings

CodeMDD.io

The Beginner’s Guide to Python Turtle

by Nikita Silaparasetty

Getting to Know the Python Turtle Library

  • The Python turtle library is a pre-installed library that allows users to create pictures and shapes by providing a virtual canvas.
  • The library is named after the onscreen pen used for drawing, which is called the turtle.
  • turtle is often used to introduce children and beginners to programming concepts in a fun and interactive way.
  • It is versatile and can be used by both children and adults to learn and practice Python programming.

Getting Started With turtle

To get started with the Python turtle library, you need to set it up on your computer. Here are the steps:

  1. Open a Python interpreter or IDE.
  2. Import the turtle module:
    import turtle
  3. Create a turtle object to draw on the screen:
    turtle_object = turtle.Turtle()
  4. To view the turtle window, use the turtle.mainloop() function:
    turtle.mainloop()

Programming With turtle

Moving the Turtle

  • To move the turtle, you can use the turtle.forward() or turtle.backward() functions.
  • The parameter for these functions represents the distance the turtle should move.

Sample code for moving the turtle forward by 100 pixels:

turtle_object.forward(100)

Drawing a Shape

  • To draw a shape, you can use a combination of turtle movements and rotations.
  • For example, to draw a square, you can use the turtle_object.forward() and turtle_object.right() functions in a loop.

Sample code for drawing a square with sides of length 100 pixels:

for _ in range(4):
turtle_object.forward(100)
turtle_object.right(90)

Drawing Preset Figures

  • The turtle library provides convenient functions for drawing preset figures such as circles, triangles, and stars.
  • For example, to draw a circle with a radius of 100 pixels, you can use the turtle_object.circle() function.

Sample code for drawing a circle:

turtle_object.circle(100)

Changing the Screen Color

  • You can change the background color of the turtle window using the turtle.bgcolor() function.
  • The parameter for this function should be a color name or RGB value.

Sample code for changing the screen color to red:

turtle.bgcolor("red")

Changing the Screen Title

  • You can change the title of the turtle window using the turtle.title() function.
  • The parameter for this function should be a string.

Sample code for changing the screen title:

turtle.title("My Turtle Drawing")

Changing the Turtle Size

  • You can change the size of the turtle using the turtle.shapesize() function.
  • The parameters for this function represent the width, outline width, and stretch factor of the turtle.

Sample code for changing the turtle size:

turtle.shapesize(2, 2, 1)

Changing the Pen Size

  • You can change the size of the pen used by the turtle using the turtle.pensize() function.
  • The parameter for this function represents the width of the pen.

Sample code for changing the pen size:

turtle.pensize(3)

Changing the Turtle and Pen Color

  • You can change the color of the turtle and pen using the turtle.color() function.
  • The parameter for this function should be a color name or RGB value.

Sample code for changing the turtle and pen color to green:

turtle.color("green")

Filling in an Image

  • You can fill in a shape drawn by the turtle using the turtle.begin_fill() and turtle.end_fill() functions.
  • The turtle.fillcolor() function can be used to set the color to fill the shape with.

Sample code for filling in a square with blue color:

turtle.begin_fill()
turtle.fillcolor("blue")
for _ in range(4):
turtle.forward(100)
turtle.right(90)
turtle.end_fill()

Changing the Turtle Shape

  • You can change the shape of the turtle using the turtle.shape() function.
  • The parameter for this function should be a valid turtle shape name.

Sample code for changing the turtle shape to a turtle icon:

turtle.shape("turtle")

Changing the Pen Speed

  • You can change the speed of the turtle pen using the turtle.speed() function.
  • The parameter for this function represents the speed of the pen, from 0 (fastest) to 10 (slowest).

Sample code for setting the pen speed to the maximum:

turtle.speed(0)

Customizing in One Line

  • You can customize multiple turtle settings in a single line of code by chaining the function calls.

Sample code for changing the turtle size, pen size, and pen color in one line:

turtle.shapesize(2, 2, 1).pensize(3).color("green")

Picking the Pen Up and Down

  • The turtle pen can be lifted up or placed down using the turtle.penup() and turtle.pendown() functions.

Sample code for picking the pen up and down:

turtle.penup()
turtle.forward(100)
turtle.pendown()

Undoing Changes

  • The turtle can undo its last action using the turtle.undo() function.

Sample code for undoing the last action:

turtle.forward(100)
turtle.undo()

Clearing the Screen

  • You can clear the turtle screen using the turtle.clear() function.

Sample code for clearing the screen:

turtle.clear()

Resetting the Environment

  • You can reset the turtle environment to its initial state using the turtle.reset() function.

Sample code for resetting the environment:

turtle.reset()

Leaving a Stamp

  • The turtle can leave a stamp at its current position using the turtle.stamp() function.

Sample code for leaving a stamp:

turtle.stamp()

Cloning Your Turtle

  • You can create a clone of the turtle with its own position and attributes using the turtle.clone() function.

Sample code for cloning the turtle:

clone_turtle = turtle.clone()

Using Loops and Conditional Statements

for Loops

  • The for loop can be used to repeat a block of code a specific number of times.
  • It is useful for drawing repeated patterns or shapes.

Sample code for using a for loop to draw a square pattern:

for _ in range(4):
turtle_object.forward(100)
turtle_object.right(90)

while Loops

  • The while loop can be used to repeat a block of code as long as a certain condition is true.
  • It is useful for drawing shapes with a dynamic size or structure.

Sample code for using a while loop to draw a spiral:

length = 10
angle = 90
while length < 200:
turtle_object.forward(length)
turtle_object.right(angle)
length += 10

Conditional Statements

  • Conditional statements, such as if, elif, and else, allow you to make decisions based on certain conditions.
  • They can be used to create different behaviors for the turtle based on user input or other factors.

Sample code for using a conditional statement to change the pen color based on user input:

user_color = input("Enter a color: ")
if user_color == "red":
turtle.color("red")
elif user_color == "blue":
turtle.color("blue")
else:
turtle.color("black")

Final Project: The Python Turtle Race

Setting Up the Game Environment

  • To set up the game environment, you need to import the random module and create a screen for the race.
  • You also need to create multiple turtles and position them at the starting line.

Sample code for setting up the game environment:

import turtle
import random
screen = turtle.Screen()
screen.title("Python Turtle Race")
turtles = []
colors = ["red", "blue", "green", "yellow", "orange"]
for i, color in enumerate(colors):
turtle_object = turtle.Turtle(shape="turtle")
turtle_object.color(color)
turtle_object.penup()
turtle_object.goto(-200, -100 + i * 50)
turtle_object.pendown()
turtles.append(turtle_object)
screen.mainloop()

Creating the Die

  • To create the die for the game, you can use the random module to generate a random number between 1 and 6.
  • The number rolled on the die determines how far the turtle moves.

Sample code for creating the die:

def roll_die():
return random.randint(1, 6)

Developing the Game

  • To develop the game, you need to implement a loop that rolls the die and moves the turtles accordingly.
  • The game ends when a turtle reaches the finish line.

Sample code for developing the game:

while True:
for turtle_object in turtles:
distance = roll_die() * 10
turtle_object.forward(distance)
if turtle_object.xcor() >= 200:
winner_color = turtle_object.color()[0]
print(f"The {winner_color} turtle wins!")
break

Conclusion

In this tutorial, you learned how to use the Python turtle library to create drawings and develop a simple game. You explored various turtle commands and Python programming concepts such as loops and conditional statements. By following the step-by-step instructions and executing the sample codes, you gained practical experience in using the turtle library. Now you can continue exploring the possibilities of turtle and take your Python programming skills to the next level!