Wikijunior:Raspberry Pi/Raspberry Pi Turtle Stars

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

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

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

Move and draw edit

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

position() | pos()
towards()
xcor()
ycor()
heading()
distance()

Setting and measurement edit

degrees()
radians()

Pen control edit

Drawing state edit

pendown() | pd() | down()
penup() | pu() | up()
pensize() | width()
pen()
isdown()

Color control edit

color()
pencolor()
fillcolor()

Filling edit

fill()
begin_fill()
end_fill()

More drawing control edit

reset()
clear()
write()

Turtle state edit

Visibility edit

showturtle() | st()
hideturtle() | ht()
isvisible()v

Appearance edit

shape()
resizemode()
shapesize() | turtlesize()
settiltangle()
tiltangle()
tilt()

Using events edit

onclick()
onrelease()
ondrag()
mainloop() | done()

Special Turtle methods edit

begin_poly()
end_poly()
get_poly()
clone()
getturtle() | getpen()
getscreen()
setundobuffer()
undobufferentries()
tracer()
window_width()
window_height()

Window control edit

bgcolor()
bgpic()
clear() | clearscreen()
reset() | resetscreen()
screensize()
setworldcoordinates()

Animation control edit

delay()
tracer()
update()

Using screen events edit

listen()
onkey()
onclick() | onscreenclick()
ontimer()

Settings and special methods edit

mode()
colormode()
getcanvas()
getshapes()
register_shape() | addshape()
turtles()
window_height()
window_width()

Methods specific to Screen edit

bye()
exitonclick()
setup()
title()