Raspberry Pi Club Lesson 3 - Python Turtle
Today we will be programming a Turtle using python.
We will start replicating the shapes the same as last week and then continue onto using variables and maths to make it automatically create X sided shapes by changing the value.
First open up idle and create a new workpad by clicking File->
New Window. After type in the following while replacing your name here with your name
#Python Turtle #Square #By <Your Name Here> import turtle
now save the file as square.py into your documents.
Now we want to add the Turtle, we're going to call him Ben, do this by adding the following lines to your code.
turtle.colormode(255) screen = turtle.Screen() ben = turtle.Turtle() times = 0 sides = 0 i = 0 #Write your program after this #Write everything before this turtle.mainloop()
This now gives us two main variables, the screen or playground for ben to run around on and ben himself to control. The two lines with the # in front are comments which tell us to put our turtle program in the middle. The final line waits until we press the close button on the window before it shuts down.
We have also added in times, I and sides for later use.
We are now ready to program the turtle.
Commands For Ben:
Now Ben is ready for programming we can start making him move, instead of blocks we control Ben using the following syntax.
We then replace function with a function of the python turtle. The four functions we are going to cover first are the following:
ben.fd(50) #This makes ben move forwards 50 steps ben.bk(50) #This makes ben move backwards 50 steps ben.lt(90) #This makes ben turn left by 90 degrees ben.rt(90) #This makes ben move right by 90 degrees
Try putting some of these functions in the area where you program Ben and then press F5 to run your program. Ben should start moving around on the screen and replicate the commands you told him. Here you do not need to copy out the comments.
I wrote the following program and the output came out as the following image.
ben.fd(50) ben.rt(90) ben.bk(50) ben.lt(90) ben.fd(50)
We're now going to start making shapes, as we learned last week its more efficient to use loops to automate repeated steps. In python this is using the while function.
A simple while loop is done using the following code
while (i<times): #Your Turtle Code Here i=i+1
(To get the indents use the tab button on the keyboard and not spaces. Doing this will cause the program not to work).
My code now looks like this:
#Python Turtle #Square #By Ryan Walmsley import turtle turtle.colormode(255) screen = turtle.Screen() ben = turtle.Turtle() times = 0 sides = 0 i = 0 #Write your program after this while (i<times): #Your Turtle Code Here i = i+1 #Write everything before this turtle.mainloop()
Now program ben to create the square by placing the two commands below the
#Your Turtle Code Here
Now press F5 to run the program, Ben shouldn't move at all.
This is because we have forgotten to set the amount of times we want Ben to repeat the code. Close the window and change the variable
times = 0 to
times = 4 making Ben repeat it 4 times.
Ben should now be drawing a square!
You're well on the way to becoming a pythonical programmer.
Like last week we are now going to modify the same program to create a circle, triangle, hexagon and five pointed star.
After these we will be making it so you set a variable with the amount of sides and it will automatically create that shape.
Based off last week's worksheets can you remember how to make it turn into a circle?
We will also show you later on how to make it change colours.
Save this as
A triangle only requires two modifications to be made. The amount of times needs to be changed and the angle once again to 60.
Now save your program as
Once again we only need to slightly modify the code. A hexagon has 6 sides and an angle of 120
Your output should look something like this
Save this has
Five Pointed Star
Finally the last program we are going to convert is the five pointed star from last week
To make a 5 pointed star we need to do the following 5 times:
Move Forward 100 Steps Turn Right 72 degrees Move Forward 100 Steps Turn Left 144 Degrees
We then get the following output!
As you have likely noticed by now we are only working in black and white. When we add colours to our programs they look so much better!
Unlike scratch colours in python turtle require some more programming.
Our monitors have only 3 colours that they can display, this is Red, Green and Blue or more commonly abbreviated as RGB.
We then instead set how bright each colour is. We will be using decimal values which can range between 0-255.
Here's some example colours and there RGB values.
We then set the colour of Ben's pen using the following command:
place this before the while loop.
Replace RRR, GGG & BBB with the RGB value you would like to set it to be
Feel free to change the numbers around to create your own colours.
It is also possible to randomise the colour each time you load the program by importing the random module.
To do this add the following line below import turtle
Next above your while loop we need to create the three colours. To do this we will assign them to variables.
To do this do the following:
red = random.randint(0,255) green = random.randint(0,255) blue = random.randint(0,255) ben.pencolor(red,green,blue)
Your program should look a bit like this where the code inside the while loop may be different for another shape
red = random.randint(0,255) green = random.randint(0,255) blue = random.randint(0,255) ben.pencolor(red,green,blue) #Write your program after this while (i<times): ben.fd(50) ben.rt(91) i = i+1
This will then set the pen colour to a random one every time you run your program.
You can instead move this to happen for every edge of the shape by putting it in the while loop. Here's the ouput after doing the modification on circle.py
Making Ben work faster!
Normally we would him more coffee but instead his speed can be changed by just making his speed variable higher and this is easy to do! Just add the following command before the while loop
We then replace speed with one of the following speeds:
- Fastest : 0
- Fast : 10
- Normal: 6
- Slow: 3
- Slowest: 1
X sided shapes!
We're onto the last section of today's club where we will be programming python to automatically calculate the angles for the shape we set.
For this we need to do the following:
1) change the while loop to use the variable
sides instead of times
2) To help avoid the shapes not fitting on the screen we will make the length automatically change to the amount of sides. Do this by changing the
3) we need to calculate the angle required. To do this we need to calculate the angle. The angle can be calculated by deviding 360 by the amount of sides we have using the divide operator (
/) and then dividing this all by 1. The final maths equation is
Set the angle used in
ben.rt() to this equation.
4) Finally set the amount of angles you wish to draw in the sides variable at the top. Don't worry about chaingthe times as this isn't used any more.