DEV Community

Developers Today
Developers Today

Posted on

PYTHON TURTLE: OVERVIEW

What is Python Turtle?

from Unsplash by Giorgia Doglioni

Python Turtle is a library used to create simple graphics in Python. It is a great way to introduce programming to kids and adults alike, as it is easy to use and understand. Python Turtle also allows you to learn the basics of programming without having any prior knowledge or experience with coding.

Getting Started with Python Turtle
To get started with Python Turtle, you need to download and install Python. Then you can install the Turtle library by following these steps:

Open a new Python file in your favorite text editor (I use Sublime Text)
Type import turtle at the top of your file.
Create a Turtle object by typing t = Turtle() in your code window, then pressing Enter or Return on your keyboard. This will create an instance of our turtle named โ€œtโ€ that we can use to draw shapes and patterns!

Using Turtle Commands

Turtle Motion Commands:

  1. forward(distance): moves the turtle forward by a specified distance
  2. backward(distance) : moves the turtle backward by a specified distance
  3. right(angle) : turns the turtle clockwise by a specified angle
  4. left(angle) : turns the turtle counterclockwise by a specified angle
  5. goto(x, y) : moves the turtle to the specified coordinates (x, y)
  6. setx(x) : moves the turtle to the specified x-coordinate
  7. sety(y) : moves the turtle to the specified y-coordinate
  8. setheading(angle) : sets the turtle's heading to the specified angle
  9. home() : moves the turtle to the origin (0, 0) and sets the turtle's heading to due east (0 degrees)

Pen Control Commands:

  1. pendown() : puts the pen down, allowing the turtle to draw
  2. pinupup() : lifts the pen up, preventing the turtle from drawing
  3. pensize(width) : sets the width of the pen to the specified value
  4. pencolor(color) : sets the color of the pen to the specified color
  5. fillcolor(color) : sets the color used for filling in shapes
  6. begin_fill() : starts filling in a shape
  7. end_fill() : stops filling in a shape

Window Control Commands:

  1. screensize(width, height) : sets the size of the turtle's screen to the specified values
  2. title(title) : sets the title of the turtle's window
  3. bgcolor(color) : sets the background color of the turtle's window
  4. window_width() : returns the width of the turtle's window
  5. window_height() : returns the height of the turtle's window

Other Commands:

  1. clear() : clears the turtle's screen and resets the turtle to its original state
  2. reset() : clears the turtle's screen and resets the turtle to its original state, but does not clear the drawing window
  3. hideturtle() : hides the turtle from view
  4. showturtle() : makes the turtle visible again
  5. speed(speed) : sets the speed of the turtle (0 = fastest, 10 = slowest)
  6. undo() : undoes the last action performed by the turtle
  7. isdown() : returns True if the pen is down, False otherwise.

Examples:

import turtle
import random
def draw_star(size, color, x, y):
    turtle.penup()
    turtle.goto(x, y)
    turtle.pendown()
    turtle.color(color)
    turtle.begin_fill()
    for _ in range(5):
        turtle.forward(size)
        turtle.right(144)
    turtle.end_fill()
def animate_stars():
    turtle.clear()
    for _ in range(25):
        size = random.randint(10, 30)
        x = random.randint(-300, 300)
        y = random.randint(-300, 300)
        color = random.choice(["red", "blue", "green", "yellow", "white", "cyan", "magenta"])
        draw_star(size, color, x, y)
    turtle.ontimer(animate_stars, 1000)
turtle. Speed(0)
turtle.bgcolor("black")
turtle.hideturtle()
turtle.ontimer(animate_stars, 1000)
turtle.mainloop()

Enter fullscreen mode Exit fullscreen mode

[Youtube]{https://www.youtube.com/watch?v=7dw-MYvF0}

Conclusion
Python Turtle is a great way to introduce programming to kids and adults alike. Itโ€™s easy to use, understand and a great way to learn the basics of programming.

Feel free to follow me on Twitter, GitHub, and YouTube.๐Ÿ™‚

Top comments (1)

Collapse
 
chrisgreening profile image
Chris Greening

Yayyy I love turtle!! I've actually been working on a library built on top of turtle for drawing complex visualizations that you might be interested in! It uses a lot of what you demonstrate here under the hood for drawing neat animations ๐Ÿ˜Ž

A star shape shifts and rotates to the right drawing a complex tracing

GitHub logo chris-greening / spyrograph

Python library for analyzing, exploring, and visualizing epitrochoids and hypotrochoids in just a few lines of code

spyrograph: elegant mathematics and geometries

Animation of three geometric, symmetrical shapes being drawn next to one another left to right. The shape on the left is red, the middle green, and the right is blue.

What is it?

spyrograph is a lightweight Python package that provides an expressive and flexible set of tools for drawing beautiful mathematically driven art. With just a few lines of easy-to-read code you can start analyzing, visualizing, and exploring elegant mathematics

Downloads Issues License Version Documentation Status

"Buy Me A Coffee"

Official website

Official docs

Table of Contents


๐Ÿ”‘ Key features

  • Expressive and consistent syntax
  • Robust underlying mathematics
  • Beginner and expert friendly
  • numpy is the only required third-party installation
  • Clear visualizations and animations
  • Flexible to a wide range of usecases
  • Lightweight, just plug and play

Sample hypotrochoid drawing showing a circle rolling around the interior of another circle drawing a geometric shape


๐Ÿ’ป Installation

pip

Install the latest stable release from PyPI using

$ pip3 install spyrograph
Enter fullscreen mode Exit fullscreen mode

or clone the development version from GitHub with

$ git clone https://github.com/chris-greening/spyrograph.git
Enter fullscreen mode Exit fullscreen mode

๐ŸŒฑ Quickstart

spyrograph is designed to be expressive and easy-to-use - simply import spyrograph and jump right into drawing elegant, complex shapesโ€ฆ