Make a Turtle Move in Circles

Cristiano Medeiros Dalbem on Flickr

A simple, fun Python tutorial that’ll get you drawing shapes all over the screen.

Today we’re going to play around with a little code, a little geometry, and a little art!

The goal: drawing whimsical spirals across our screen. The language: Python, which is beginner-friendly and perfect for simple code. We’re also going to use Turtle, Python’s popular graphics extension.

Start by navigating to https://repl.it/languages/python_turtle. In the left-hand editor, set up the basics of our program with the following code:

from turtle import Turtle, Screen
screen = Screen()
rosa = Turtle()
rosa.shape('turtle')

First, we import some functions from the turtle library. Libraries are collections of code files written by experts. Often, you can find functions to perform tricky tasks that would be annoying to write yourself. No point in reinventing the wheel!

Next, we create our turtle, Rosa, and we make a screen for her to draw on. We also use the “shape” function to change Rosa’s icon into a turtle. If you run the code at this point, you should see a blank screen with Rosa chilling in the centre. You can run your code by hitting the green button near the top of the screen.

START WITH A CIRCLE

Before diving into more complex shapes, let’s warm up Rosa by making her draw a circle:

rosa.circle(100)

The value between the brackets of the “circle” function determines the size of the circle Rosa is drawing. In a circle, all the points on the edge are the same distance from the centre. The length between the edge and the centre is known as the radius. A bigger radius means a bigger circle.

Degrees are a unit we use to measure angles. Imagine cutting a gigantic pie into 360 equal slices. Each slice would have an angle of 1 degree. If you put 180 pieces together, you get a half circle, and 180 degrees. What about quarter circle? That would require 90 pieces of pie, or 90 degrees.

When you’re using the “circle” function, you can add a second argument to specify the number of degrees. What happens if you use a negative number? Or a number bigger than 360?

rosa.circle(100, -180)

SEND IT INTO A SPIRAL

To create a spiral, we need to stop Rosa halfway through her circle. This means that she’ll only turn 180 degrees instead of 360. Afterwards, we’ll start a new, bigger circle. Again, Rosa only completes half of it before stopping.

If we repeat this process multiple times, increasing the radius by 20 units at a time, the result is a perfect spiral!

Any time you see the word “repeat” in your programming instructions, it’s time to use a loop. Writing the same code over and over is boring. Instead, let’s throw in some variables, and keep our code short with the help of a while loop:

from turtle import Turtle, Screen
screen = Screen()
rosa = Turtle()
rosa.shape('turtle')
radius = 10

while radius < 200:
 rosa.circle(radius, 180)
 radius += 20

The “+=“ symbol is a programming shorthand for “increase by the following amount”.

The “while loop” runs until its condition is no longer true. In our case, that means the loop will repeat over and over until we’ve increased the radius of our circle so that it’s bigger than 200. Try it out!

Ta-da! If you want to create even more cool spirals, try multiplying the radius by a fixed value instead of adding to it. A low value, like 1.3 or 1.5, should keep things interesting.

 radius *= 1.3

Learn More

Turtle built-in commands

http://www.eg.bucknell.edu/~hyde/Python3/TurtleDirections.html

First turtle program

https://interactivepython.org/runestone/static/thinkcspy/PythonTurtle/OurFirstTurtleProgram.html

Python turtle module

https://www.simplifiedpython.net/python-turtle-module/

Python Turtle Colors

https://ecsdtech.com/8-pages/121-python-turtle-colors

Color filled shapes in python
https://www.tutorialsandyou.com/python/how-to-draw-color-filled-shapes-in-python-turtle-17.html

Turtle Command flash cards

https://quizlet.com/156092424/python-turtle-commands-v-36-flash-cards/

Python turtle cheat sheet

https://compuzzle.wordpress.com/2015/05/04/python-turtle-cheat-sheet-and-geometric-shapes/

Python Turtle Graphics

https://gameswithcode.com/python-turtle-graphics/

python-turtle-programming-for-kids

https://www.gordonchoi.com/python-turtle-programming-for-kids/

Python turtle drawing

https://www.youtube.com/watch?v=KrR0i0n3exMttps://www.youtube.com/watch?v=KrR0i0n3exM

Author

  • Patricia Foster

    Patricia Foster is a computer science student at Carleton University. In addition to working professionally as a software developer, she spends her time reading and writing.

Also In The June 2019 Issue

Meet your new favorite robot pal and learn how to code in JavaScript.

A Python coding challenge to create exciting labyrinths at the click of a button!

With this new online program, you don’t have to be a coding whiz to create fun mobile apps!

Spend those lazy summer days curled up with great stories about science & tech.

Learn the secret to creating huge programs without getting lost in thousands of lines of code.

A wacky story about connectivity, Danish kings, and the need for good dentists.

A simple, fun Python tutorial that’ll get you drawing shapes all over the screen.

How Microsoft’s funny little assistant led to the AI we know and love today.

In the future, will computers be able to do everything? Even create an entire universe?

Follow this detailed step-by-step tutorial to create dazzling, colourful flowers out of geometric patterns.

Download some paper templates and bring your Minecraft creations into the real world.

Battery history is a critical part of the history of technology. Without stored electricity, there would be no electronics.

Mix some math and some code to create dynamic games with realistic physics.

Links from the bottom of all the June 2019 articles, collected in one place for you to print, share, or bookmark.

Interesting stories about science and technology for June 2019.

Interested but not ready to subscribe? Sign-up for our free monthly email newsletter with curated site content and a new issue email announcement that we send every two months.

No, thanks!