LogoLogo
Terminal
  • Attic Lab
  • Getting Started
  • Crest Gold
  • Crest Silver
  • Videos on Computation
  • PI PICO (CIRCUITPYTHON)
    • Getting Started
    • Pin Out Diagram
    • Breadboards
    • 1. Led Blink
    • 2. RGB
    • 3. OLED
    • 4. Sensors
    • 5. Wifi
    • 6. Servos
  • Arduino
    • Getting Started
    • Pin Out Diagrams
      • Mega2560 R3
    • Programming
      • Arduino C - Cheat Sheet
    • Buttons
      • Momentary Switches
    • Display
      • LEDs
      • 7 Segment Displays
      • LCD Displays (GPIO)
      • LCD Displays (SPI)
      • OLEDs
    • Communication
      • Antenna Theory
      • Lora
      • Wifi
        • Boards
    • Project Ideas
    • Motion
      • DC Motors
      • Servo Motors
      • Stepper Motors
  • Microsoft Office
    • Word
    • Powerpoint
    • Excel
  • The Terminal
    • Basics
    • Cheat Sheet
    • Games
      • Level 1 - Bashcrawl
      • Level 2 - Bandit
  • TinkerCad
    • Gallery
    • Getting Started
    • Basic Operations
    • Basic Skills
    • Projects
      • Locking Container
  • Python
    • Hello World
    • Turtle Graphics
      • Strings in Turtle Graphics
      • Cheat Sheet
    • Variables
    • Loops
    • If Statements
    • Functions
    • Games
      • Pong
  • Raspberry Pi
    • Setup
      • Changing The Hostname
      • Headless Setup
      • Kiosk Mode
    • Remote Connections
    • Displays
      • Memory
        • External HD
      • HyperPixel 4.0
  • Ultimaker 3D Printing
    • The Thingiverse
    • Preparing the File
    • Printing
    • Calibration Prints
    • Print Set
  • Fusion 360
    • Getting Started
    • Design Tutorials
      • Tweezers
      • Mars Rover Wheel
    • Surface Modeling
  • Electronics
    • References
    • Antenna Theory
    • LoRa
  • PCB Milling
    • FlatCam
    • Candle
    • PCB Milling
  • Projects
  • Projects
    • Star Map Necklace
    • Ideas Respository
  • Latex
    • What is LaTeX?
    • Getting Started
    • Structure
    • Page Size & Margins
    • Styling
    • Images
    • Lists
    • Tables
    • Mathematics
      • Superscript and Subscripts
      • List of Symbols
      • Fractions and Binomials
      • Integrals, Sums & Limits
    • Colors
  • Web Development
    • The Internet
    • Intro to HTML
    • Basic Elements
    • Basic Styling
Powered by GitBook
On this page
  • The Spiral (spiral.py)
  • Circle Spiral
  • Color
  • Changing Background Colours
  • Adding Complexity
  • Extension

Was this helpful?

  1. Python

Turtle Graphics

PreviousHello WorldNextStrings in Turtle Graphics

Last updated 5 years ago

Was this helpful?

We can create graphics in Python by using a turtle. This is an imaginary pointer that we can instruct to move around the screen.

The Spiral (spiral.py)

Our first turtle program is going to create a spiral by drawing lines of every greater length before turning through 90 degrees.

Open up a new python file and type in the following code:

Play around with the numbers in the code to see their effect on the output. Try turning by 91 degrees instead of 90. What happens?

Circle Spiral

We can also draw basic shapes with turtle. Let's try the circle command.

Color

We can add colour to our turtle by using the pencolor command. Modify your code to set the pen to red

One colour is a bit boring though. We can store several colours in a list and get python to cycle through them.

  • In almost all programming languages, list entries are numbered starting from 0. So colors[0] = red, colors[1] = yellow etc.

  • x % 4 (said as x mod 4) calculates the remainder when x is divided by 4. So in our loop this will cycle through 0,1,2,3

Changing Background Colours

Of course, your images will look much better if the background was darker. We can set the background colour using the bgcolor command:

turtle.bgcolor("black")

Adding Complexity

We're now going to add some complexity to our programmes to make our images look even better. Take a look at the following code:

Lets look at the changes one at a time:

  • sides = 6 : In the examples above we had squares, i.e 4 sides. Now we want to store the number of sides in a variable so that we can change it easily.

  • for x in range (360) : Not much of a change here but we're going to create a bigger picture by going to 360

  • colors = ["red", "yellow", "blue", "orange", "green", "purple"] : More colours. Well why not!

  • t.forward(x * 3 / sides + x) : This adds some complexity to our image, moving the turtle forward more with each loop. Can you work out what it's doing?

  • t.left(360/sides + 1) : Before we were turning left 90 + 1 degrees because we had a square. 360/4 = 90. Now we want to create a shape with however many sides we have stored in our variable.

  • t.width(x*sides/100) : Again, this is adding more complexity. The width command sets how wide our pen mark will be, gradually increasing the width as the spiral moves out.

Make these changes to your code and run the program

Make sure that you copy the changes exactly!!

Play around with your code and see what kind of interesting images you can make. Screenshot your best effort and submit your work.

IDEA: Change the number of side, add more colors. Try adding an extra turn to the end of your loop, like t.left(90)

On a Mac, you can take a screenshot by pressing Command-shift-4 and selecting the area that you want to screenshot. A file of the screenshot will be saved on your desktop.

Extension

We can even ask the user how many sides they would like. Replace the line side=6 with the following:

sides = eval( input("Enter a number of sides between 2 and 6: ") )

This takes an input from the user and uses the eval() function to convert it in to a number that the computer can use.

Task: Who can make the most appealing graphic. Try to add more than 4 colours to your drawing. Go to see a list of other colours.

here