Python For GCSE
Turtle Graphics - Controlling The Pen
Up & Down
The two key methods for controlling the pen are,
- penup(), pu(), up()
- pendown(), pd(), down()
Remember that when the pen is up, moving the turtle will not draw to the canvas. You can determine if the pen is down in a program with,
turtle.isdown()
Size
You can set the thickness of the line drawn by typing,
turtle.pensize(n)
Pen Colour
There are a variety of ways to control the colour of the turtle pen, all using the same method,
turtle.pencolor()
import turtle # get the current pen colour c = turtle.pencolor() # use a colour name turtle.pencolor("red") turtle.fd(100) turtle.lt(45) # use an rgb value, 0-1.0 for each value turtle.pencolor(0,1.0,0) turtle.fd(100) turtle.lt(45) # stored colours reddish = (0.5,0,0) turtle.pencolor(reddish) turtle.fd(100) turtle.lt(45) # setting it back to the colour saved at the start turtle.pencolor(c) turtle.fd(100)
Filling
You set the fill colour the same way as you would for the pen. The difference comes when you want to fill in some shapes.
import turtle turtle.fillcolor("red") turtle.begin_fill() turtle.fd(100) turtle.lt(90) turtle.fd(100) turtle.lt(90) turtle.fd(100) turtle.lt(90) turtle.fd(100) turtle.end_fill()
Clearing Up
There are two basic methods for clearing the drawings a turtle has made,
turtle.reset()
turtle.clear()
There is a difference. Clearing does not change the position and other attributes of the turtle. Reset, as the name suggests, sets everything back to the way it was the instant that the turtle was created.
Hide & Seek
You can hide and show the turtle with the following,
turtle.hideturtle(), turtle.ht()
turtle.showturtle(). turtle.st()
If your pogram is complex enough for it not to be obvious if the turtle is currently visible, you can check the visibility with,
turtle.isvisible()
Writing
You can write text on the screen at the position of the cursor. Here is an example,
import turtle turtle.write("Hello",font=("Arial", 16, "normal"))