# Python For GCSE

Turtle Graphics - Moving & Drawing

## Introduction

Turtle graphics are based on moving a cursor (the turtle) around a drawing surface. Amongst other attributes, the turtle has a location, a direction of movement and a state for its pen (touching the surface or not).

To use the turtle module, you need to import it.

import turtle

Remember** not to** save your program with the same name as the module.

## Moving

Four basic methods for movement are,

- forward(distance), fd(distance)
- backward(disitance), bk(distance), back(distance)
- right(angle), rt(angle)
- left(angle), lt(angle)

Several turtle methods have short forms. These can save you a little typing when writing your programs.

import turtle turtle.forward(100) turtle.right(90) turtle.forward(50) turtle.left(30) turtle.backward(90)

You can get more precise control over the position and angle of the turtle with,

goto(x,y), setpos(x,y), setposition(x,y) | Move the turtle to the specified coordinates. If the pen is down, it will draw. |

setx(x) | Change the x coordinate of the turtle. |

sety(y) | Change the y coordinate of the turtle. |

setheading(angle), seth(angle) | Change the direction of the turtle to the angle specified. |

home() | Return the turtle to the starting position (0,0). |

import turtle turtle.goto(0,100) turtle.setx(100) turtle.sety(0) turtle.setheading(180) turtle.forward(100)

## Circles

The method for circles has three parameters.

**turtle.circle(radius, extent, steps)**

You only need to provide the first value, the radius of the circle. If you do, a full circle is drawn. The extent is an angle. If you put an angle of 90, you would get a quarter circle.

steps is an interesting value. If you remember that the screen is a grid of pixels, you should appreciate that circles are approximated with lots of small straight lines. If you choose not to specify the number of steps, Python will calculate an optimal number to give a reasonable looking circle. By specifying the number of steps, you get a nifty way of drawing polygons.

import turtle turtle.circle(75) turtle.circle(125,180)

An easy hexagon,

import turtle turtle.circle(100,360,6)

## Dots & Stamps

We can draw dots of varying size and colour at the position of the turtle. We can also stamp the shape of the turtle onto the canvas. If you store the id of the stamp, you have the ability to clear it at a later time. Watch this one several times to spot how the stamp is cleared.

import turtle # draw dot, (size, colour) turtle.dot(10, "red") turtle.fd(50) turtle.dot(5, "blue") turtle.lt(90) turtle.fd(50) # stamp the turtle shape a = turtle.stamp() turtle.rt(180) turtle.fd(75) # clear the stamp turtle.clearstamp(a)

You can also clear turtle stamps by specifying how many of them you want to clear,

**turtle.clearstamps(n)**

If you write a value for n, you will clear the previous n stamps. If you don't write a value, all of the stamps will be cleared.

## Undoing

You can undo actions from the turtle with,

**turtle.undo()**

Use the statement repeatedly to undo multiple actions.

## Speed

You can set the speed of the turtle to a value from 0 to 10. 0 means no animation, and 10 is the slowest speed.

**turtle.speed(speed)**

## Getting Information About The Turtle

All of the following methods return values giving information about the position of the turtle. You need to set a variable to store those values.

position(), pos() | Returns the coordinates of the turtle's current position. |

towards(x,y) | Returns the angle between the current position and the point specified. |

xcor() | Returns the x position of the turtle. |

ycor() | Returns the y position of the turtle. |

heading() | Returns the current angle of the turtle. |

distance(x,y) | Gets the distance between the current position and a specified position. |