Wikijunior:Raspberry Pi/Raspberry Pi Turtle Stars
| The Turtle module is built-in to the default installation of Python. So you don't need a Raspberry Pi for this tutorial. |
Turtle is a programming language that is used to draw artwork using a turtle and a series of commands. We are using the Python module which allows you to draw pictures using Turtle commands.
Draw a star
First we're going to write a program to draw a star:
# Import all functions and variables from the turtle library
from turtle import *
# Set the pen color to WhiteSmoke
color("WhiteSmoke")
# Set the background color to MidnightBlue
bgcolor("MidnightBlue")
# Put the pen down so the turtle can start drawing
pendown()
# Start filling the shape with the current pen color
begin_fill()
# Draw the star shape
for side in range(5):
# Turn left 144 degrees
left(144)
# Move the turtle forward 50 units
forward(50)
# Finish filling the shape
end_fill()
# Pick the pen up so the turtle can move without drawing
penup()
# Move the turtle forward 100 units
forward(100)
# Enter the turtle graphics event loop and wait for the user to close the window
done()
Now change the program so that it uses a function:
from turtle import *
# A function for drawing a star #'def' means 'define'
def drawStar():
pendown()
begin_fill()
for side in range(5):
left(144)
forward(50)
end_fill()
penup()
# Now use the function to draw a light grey star on a dark blue
background
color("WhiteSmoke")
bgcolor("MidnightBlue")
drawStar()
forward(100)
drawStar()
left(120)
forward(150)
drawStar()
hideturtle()
done()
Now change the function to draw different size stars:
def drawStar(starSize)
…
forward(starSize)
…
drawStar(100)
…
drawStar(50)
Now, change the function so that we can draw stars of different sizes and colours:
def drawStar(starSize, starColour)
color(starColour)
…
forward(starSize)
…
drawStar(100, "Red")
…
drawStar(50, "White")
Have fun creating many stars of various sizes and colours!
Turtle API
This is a full list of the functions provided by the Turtle Python module: https://docs.python.org/3/library/turtle.html
Turtle motion
Move and draw
forward() | fd()
backward() | bk() | back()
right() | rt()
left() | lt()
goto() | setpos() | setposition()
setx()
sety()
setheading() | seth()
home()
circle()
dot()
stamp()
clearstamp()
clearstamps()
undo()
speed()
Tell Turtle's state
position() | pos()
towards()
xcor()
ycor()
heading()
distance()
Setting and measurement
degrees()
radians()
Pen control
Drawing state
pendown() | pd() | down()
penup() | pu() | up()
pensize() | width()
pen()
isdown()
Color control
color()
pencolor()
fillcolor()
Filling
fill()
begin_fill()
end_fill()
More drawing control
reset()
clear()
write()
Turtle state
Visibility
showturtle() | st()
hideturtle() | ht()
isvisible()v
Appearance
shape()
resizemode()
shapesize() | turtlesize()
settiltangle()
tiltangle()
tilt()
Using events
onclick()
onrelease()
ondrag()
mainloop() | done()
Special Turtle methods
begin_poly()
end_poly()
get_poly()
clone()
getturtle() | getpen()
getscreen()
setundobuffer()
undobufferentries()
tracer()
window_width()
window_height()
Window control
bgcolor()
bgpic()
clear() | clearscreen()
reset() | resetscreen()
screensize()
setworldcoordinates()
Animation control
delay()
tracer()
update()
Using screen events
listen()
onkey()
onclick() | onscreenclick()
ontimer()
Settings and special methods
mode()
colormode()
getcanvas()
getshapes()
register_shape() | addshape()
turtles()
window_height()
window_width()
Methods specific to Screen
bye()
exitonclick()
setup()
title()