DEV Community

Cover image for Turtle python module
Sibusiso Dlamini
Sibusiso Dlamini

Posted on

Turtle python module

What is Turtle?

Turtle is a standard python library(module) that can be used to draw and create simple GUIs. In other words, it is the paint equilavent for python beginners. If creating art with code is somewhat interesting or if you simply stumbled across this module for the first time I encourage that you give a shot.

Installation

Since Turtle it is a standard python library there are no installations required however if you want to make the most of this tutorial I advise that make use of the terminal and not an editor. If you decide to use the terminal make sure that python is installed onto your path. You can simply check by entering "python" in your terminal and if it is installed correctly you should see something similiar to this.

python repl

Using a text editor/IDE will not change anything about the program but I just simply believe using the terminal will help improve your understanding of python and coding in general.

What's is a repl

Repl stand for Read-Evaluate-Print Loop. What a repl does is evaluate valid python expressions and prints them automatically. It is a great tool for debugging and testing any functions that you would like to run without executing all the code inside a program. E.g.

Python repl example

If you dont have python installed on your machine yet and just want to play around with before deciding to learn it as a language, you can give it a test run at repl.it Moving on...

Hello world

First we have to import Turtle module. It is common convention to use python syntax and give the turtle module an alias. This is so Turtle.method() can be shortened to t.method().

import Turtle as t

wn = t.Screen()
JJ = t.Turtle()
JJ.write('Hello world')

Enter fullscreen mode Exit fullscreen mode

Here is what the output of the above code should look like.

hello world

Now to try and understand what each line does. With any turtle program we need a window object to draw on, hence the t.Screen(). and we store that in the variable wn.We the create a new turtle object jj by calling the method t.Turtle. Why would did I choose JJ as a variable name. Well, lets just say JJ is a friend of mine and I thought I might get creative and refer jj the turtle so that you can understand the capabilities of the turtle module. You can name your turtle object just about anything you, since there aren't many useful alternative names for a turtle object other than pen.

JJ can do all sorts of tricks. He can move backward, forward, Turn left and right. All you have to do is attach the relevant a method to JJ and your wish is his command!


jj.pendown()

jj.forward(100)
jj.left(90)
JJ.backward(90)
jj.right(100)

jj.penup()

Enter fullscreen mode Exit fullscreen mode

If you are using the repl in the terminal you can see execution of each line as you enter each line of code, and this is the reason I prefer that you follow this tutorial in the terminal. JJ also has other attribute that you can change like

  • speed (1 slowest and 10 fastest)
  • color
  • pensize
  • width
  • shape

You can make him jj.goto(x,y) a specific co_ordinate on the window. He can draw circles by entering a radius as a parameter jj.circle(r)

I find the idea of using 1s and 0s into as an art medium quite fascinating. Here are examples of some art works people have created from code.

Tree

The idea of drawing natural objects like a tree helped understand the concept of recursion a little better. This is really advanced and probably took a lot of trial and error, nonelethess in my opinion the end product is was worth every bit 😉.

illusion

Implementing an illusion of sorts can really engage your brain if you try and reverse engineer it. This is one that was given to us as an assignment, I would've uploaded mine but didn't know if I got the illusion right.

aesthete

Lastly for the pure aestheticist and the sake of beautiful things. An impressive piece.

And if drawing art does not jiggle your joystick then why not use Turtle to create GUIs for simple games. You can find Youtube tutorials on how to build Tic-Tac-Toe or if your up for the challenge why not recreate the vintage Pong game. This tutorial only covered the basics, if you want to know more about the turtle module you can read the documentation here.

That's it !!! 🎉

That is everything you need to know about getting started with the turtle module.

One of the many reasons that python is adored by a large portion of the community is that it you can start messing around and tinkering with it straight out of the box is. It gives us more than just a terminal to begin with. It gives us a very interactive feedback which is what a lot of beginners are looking for in a language. I can rant and rave about python forever the point being simple modules like this one, can really define a language.

Thanks for the read and Happy Coding! 🔥

Top comments (1)

Collapse
 
waylonwalker profile image
Waylon Walker

This looks like such a fun teaching module. I have seen it many times, but never touched it. It reminds me of a board Game that I have played with my kids called robot turtles.