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
[edit | edit source]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
[edit | edit source]This is a full list of the functions provided by the Turtle Python module: https://docs.python.org/3/library/turtle.html
Turtle motion
[edit | edit source]Move and draw
[edit | edit source]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
[edit | edit source]position()
| pos()
towards()
xcor()
ycor()
heading()
distance()
Setting and measurement
[edit | edit source]degrees()
radians()
Pen control
[edit | edit source]Drawing state
[edit | edit source]pendown()
| pd()
| down()
penup()
| pu()
| up()
pensize()
| width()
pen()
isdown()
Color control
[edit | edit source]color()
pencolor()
fillcolor()
Filling
[edit | edit source]fill()
begin_fill()
end_fill()
More drawing control
[edit | edit source]reset()
clear()
write()
Turtle state
[edit | edit source]Visibility
[edit | edit source]showturtle()
| st()
hideturtle()
| ht()
isvisible()
v
Appearance
[edit | edit source]shape()
resizemode()
shapesize()
| turtlesize()
settiltangle()
tiltangle()
tilt()
Using events
[edit | edit source]onclick()
onrelease()
ondrag()
mainloop()
| done()
Special Turtle methods
[edit | edit source]begin_poly()
end_poly()
get_poly()
clone()
getturtle()
| getpen()
getscreen()
setundobuffer()
undobufferentries()
tracer()
window_width()
window_height()
Window control
[edit | edit source]bgcolor()
bgpic()
clear()
| clearscreen()
reset()
| resetscreen()
screensize()
setworldcoordinates()
Animation control
[edit | edit source]delay()
tracer()
update()
Using screen events
[edit | edit source]listen()
onkey()
onclick()
| onscreenclick()
ontimer()
Settings and special methods
[edit | edit source]mode()
colormode()
getcanvas()
getshapes()
register_shape()
| addshape()
turtles()
window_height()
window_width()
Methods specific to Screen
[edit | edit source]bye()
exitonclick()
setup()
title()