Raspberry Pi project:Python Turtle
In this project we are going to program with Python Turtle. Turtle images are a popular way to introduce children to programming. It was part of the original Logo programming language, developed by Wally Feurzeig, Seymour Papert and Cynthia Solomon in 1967. This Python library is already installed by default on your Raspberry Pi.
This manual covers:
- Level - Beginner 30% 30%
- Duration 30 min 35% 35%
- Cost - € 70,30 50% 50%
Step 2: Turtle Functions:
Like any Python library, the Turtle library has its own functions. In this project we are not using all functions of the Turtle library. The functions we do use are listed below:
Step 3: Programming:
We program with Python in the Python IDE. You can follow the following steps to draw different things with the turtle library.
At the top of the code we first import the turtle library.
Next we set the settings for the turtle and the background.
In the def snowflake () we write the program lines about how we want to draw the snowflake. When snowflake () is called, it will be drawn.
In the def ball () we write the code about how we draw the ball. We do this by drawing a circle and then coloring it. by calling ball (), the ball will be drawn.
The code itself starts moving the turtle to a different location. No signing is done here. When the turtle is in the correct location, we call snowflake (), after which a snowflake will be drawn. We then repeat this again. On the third time, we no longer call the snowflake (), but instead call ball ().
Finally, we move the turtle to the home location, after which we change the color of the turtle to green.
Below is the full python code:
# first the turtle library is imported
# the turtle (the pen) is linked to variable t
t = turtle.Turtle ()
# the background color of tet turtle screen is set to color 'DeepSkyBlue'
#when invoked it draws a snowflake
# - the turtle writes the color white (pencolor)
# - the turtle is set to speed 10, scale from 1 to 10 (speed)
t.pen (pencolor ="White", speed =10)
#this bit of code is repeated 8 times, because the snowflake has eight branches
for i in range (8):
#with each branch the turte is turned 45 degrees
#the side branches of the snowflake are drawn
for i in range(3):
for i in range(3):
# when called, a ball is drawn
#the shape of the turlte is changed to the shape 'turtle'
# - the turtle writes the color black (pencolor)
# - the turtle's fill gets the color red (fillcolor)
# - the line is resized to 5 pixels (pensize)
# - the turtle is set to speed 5, scale from 1 to 10 (speed)
t.pen (pencolor ="Black", fillcolor ="Red", pensize =5, speed =5)
#def snowflake is drawn at location x = 200 y = 200
#def snowflake is drawn at location x = 100 y = 300
#def ball is drawn at location x = 10 y = -350
#the turtle returns to the home position (x = 0 y = 0)
#the turtle gets a green fill
t.pen (fillcolor ="Green")
Step 4: Result:
When you have entered the code correctly, you can click on “Run”. Then the Python Turtle Graphics is opened, after which the figures are drawn. When everything is drawn it will look like the one below:
When you have completed this project, you now know how to get started with the Python Turtle library. With this you can also draw a lot of nice figures.
Here the turtle functions are further elaborated that will be processed in the project.
bgcolor ( ):
This allows you to change the background color of the turtle screen. You put this color between the brackets.
This changes the properties of the pen. between the brackets you put down what you want to change about the pen. This is how you change with t.pen (pencolor = ” ”) The writing color of the pen. With t.pen (fillcolor = ” ”) Change the color of the turtle. With this you can also give a figure a fill color. With t.pen (pensize = ) you change the width of the line. With t.pen (speed = ) you can set the speed that the turtle goes. The speed is on a scale from 1 (very slow) to 10 (very fast).
forward ( ):
With forward ( ) you can tell how far the turtle should go straight. You can also do this with the shortened version: fd ( ).
backward ( ):
With backward ( ) you can tell how far back the turtle needs to be. You can also do this with the shortened version: bk ( ).
right ( ):
With right ( ) you specify the angle at which the turtle should turn clockwise. You can also do this with the short version: rt ( ).
left ( ):
With left ( ) you specify the angle at which the turtle should turn counterclockwise. You can also do this with the short version: lt ( ).
circle ( ):
With circle ( ) you can easily draw a nice circle. You put the radius of the circle between the brackets.
Normally the turtle draws a line when it changes position. With penup () you can turn this off, so you can change the position of the turtle without drawing a line.
With pendown () you do the opposite of penup (). Here you set again that when you change the position of the turtle, a line is drawn again.
With bigin_fill () you set a start when you want to fill a figure.
With end_fill () you set the end of when you want to fill a figure. after calling this function, the figure will be filled with the color you set.
goto (x, y):
With goto (x, y) you quickly direct the turtle to a coordinate. You set this coordinate between the brackets. First you state the x-axis, and then the y-axis.
With home () you quickly direct the turtle to your home position. This is the coordinate (0,0).