Intro to Turtle Graphics (Part 2)

Intro to Turtle
Graphics (Part 2)
Let’s continue to draw on Python …
Review: Peanut Butter Jelly Time!
 Remember this guy …
Review: Peanut Butter Jelly Time!
 Recall that in the beginning of the year we learned that
software is basically a set of instructions for your
computer
 Remember, computers may be powerful but they’re
really not all that smart!
 We’ve learned that we need to be very precise in our
instructions as computers are very obedient, but they
won’t think for themselves.
Review: Peanut Butter Jelly Time!
Well, let’s look at some of the “software” we created
before practicing programming …
Example 1:
 Walk to the kitchen.
 Walk towards the cabinets.
 Take a plate out of the cabinet.
 Walk towards the counter that has the bread.
 Put a slice of bread on your plate.
 Walk towards the toaster.
 Put the bread in the toaster.
Review: Peanut Butter Jelly Time!
 Toast the bread.
 Walk to the refrigerator.
 Take out the peanut butter and Nutella jars.
 Walk towards the utensil drawer.
 Take out two butter knives.
 Open the jars.
 Use one knife for peanut butter and spread peanut
butter on one side of the toast.
Review: Peanut Butter Jelly Time!
Example 2:
 Get the peanut butter and jelly.
 Get two slices of bread.
 Open the peanut butter.
 Take some of the peanut butter and spread it.
 Then, take some jelly and spread it.
 Put bread together and eat.
Review: Peanut Butter Jelly Time!
 Now that we’ve had some practice with programming
and we understand just how specific we need to be,
let’s try this again!
 Write out a set of instructions for your robot at home
that will allow it to successfully make a peanut butter
and jelly sandwich.
 This does not necessarily mean your program needs to
be longggg and include every minute detail, we just
need to be sure to include all steps (concise, is nice!)
Review: Peanut Butter Jelly Time!
Example 3:
Hello. First walk into the kitchen. Walk towards the pantry. Grasp the handle. Pull outwards
until pantry is open. Reach into the pantry and grasp the peanut butter jar. Retract your arms
backward. Turn 90 degrees. Walk towards counter and place jar on counter by removing
your grasp of the jar. Walk towards the pantry and grasp the bag of bread with one hand.
With the other grab the jar of jelly. Retract both hands inward, turn 90 degrees, and walk
back towards the counter. Place the bread and the jelly on the counter. Walk towards
pantry again just as before and remove a plate and a knife and spoon. Carry the spoon and
knife in one hand and the plate with the other. Close the pantry. Take the plate, spoon, and
knife back to counter and place on counter. Take hold of the top of the peanut butter jar.
Turn your hands until top removes itself. Place top on counter. Take hold of the top of the jelly
jar. Turn your hands until top removes itself. Grab bag of bread and undo twisty-tie. Remove
twisty tie. Open bag of bread. Grab two slices of bread. Remove two slices of bread and
place on plate. Close bag of bread. Take hold of the knife and position your hand above the
jar of peanut butter. Lower the knife into the jar of peanut butter. Turn the knife as it’s in the
peanut butter to get some on the knife. Remove the knife gently from the jar and bring knife
over to either slice of bread on the plate. Lower the knife with peanut butter onto the bread
and slide back and forth until it is covered with peanut butter. Place knife on the plate. Grab
hold of the spoon and position it over the jar of jelly. Lower the spoon into the jar and scoop
up some jelly. Remove spoon from jar and bring spoon over to the other slice of bread. Dump
the jelly from the spoon onto the bread and spread back and forth. Now, take hold of the
slice of bread with peanut butter and pick it up on the sides so as to not touch the peanut
butter. Turn it over (180 degrees) and place it on top of the piece of bread with jelly so as the
shapes match. Eat.
Review: Peanut Butter Jelly Time!
 In addition, create at least TWO functions, for activity
that we might see ourselves repeating throughout the
process.
 Example:
Review: Peanut Butter Jelly Time!
 In addition, create at least TWO functions, for activity that we
might see ourselves repeating throughout the process.
 Example:
def spread(type):
- pick up knife
- dip in type of jar
- scoop some spread out of jar
- place knife with spread facing bread at top right
corner on blank side of bread
- evenly spread throughout one side of bread
Review: Basic Turtle Functions
 Okay, now let’s get back to Python
 Remember, we learned a few instructions that turtle
understands in Python:
turtle.forward(spaces)
turtle.right(degrees)
turtle.left(degrees)
 Note that we have both a function for turning left AND
turning right, but maybe this wasn’t always the case …
Meet Karel
This is Karel …
 Karel was a programming language created to help
beginners understand the precision of instructions
necessary when programming a computer.
 Unfortunately, Karel only knows two directional
commands:
>> move – moves forward one coordinate
>> turnLeft – turns left 90 degrees
Meet Karel
 So, how the heck does Karel turn right?
Meet Karel
 The Karel language also allows for users to create
functions, which is where the turnRight function comes
from
def turnRight():
turnLeft()
turnLeft()
turnLeft()
Review: Basic Turtle Functions
 We learned a few more:
turtle.goto( x_coor, y_coor )
turtle.penup()
turtle.pendown()
 Remember that whenever you pick the pen up, you
need to put it back down if you want to begin drawing
again.
 Also, by default, turtle will begin drawing when first
starting a program. In other words, you do not need to
begin by calling turtle.pendown()
Review: Basic Turtle Functions
 We also briefly took a look at this function:
turtle.speed(level)
 This function takes one float argument, denoting the
speed at which the turtle draws.
 You can pass arguments as large as you want but at a
certain point, turtle will be moving as quick as it can.
Give it a break …
 It seems you can pass an argument as long as it is
greater than 0.5. Any argument less than or equal to 0.5
will just cause turtle to fly through your instructions.
Pen Color
 By default, turtle will draw solid black lines
 However, you can change the color of your lines by
calling the function:
turtle.pencolor(“color_code”)
 This function accepts one argument, which is the color
code.
 Note: the color code must be inside of delimiters!
RGB Color
 The color value can be a standard Red Green Blue
(RGB) color code.
# this is to set the color as red
turtle.pencolor(“#FF 00 00”)
# this is to set the color green
turtle.pencolor(“#00 FF 00”)
# this is set the color blue
turtle.pencolor(“#00 00 FF”)
RGB Color
 RGB color codes define how much red, green and blue
should be mixed into a given color
 They are divided into three channels:
- the first two characters denote the amount of red
- the second two characters denote the amount of green
- the third two characters denote the amount of blue
Color code: “ # 00 00 00 ”
 this would be black
 Note that they do not need to be spaced out
 Note also that most color codes begin with a # symbol
RGB Color
 RGB uses a number system called “hexadecimal”
 Hexadecimal (or “hex”) is a base 16 number system. This
means it uses 16 digits to represent numbers
 Counting in hex:
- 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F
 You’ll notice that here the 11th to 16th digits are
represented by letters
RGB Color
#000000  this denotes black
#FFFFFF  this denotes
#FF0000  this denotes all red
#00FF00  this denotes all green
#0000FF  this denotes all blue
RGB Color Resources
Web based color tools/websites:
http://cloford.com/resources/colours/500col.htm
http://paletton.com/#uid=14r0u0kllllaFw0g0qFqFg0w0aF
Programming Challenge: RGB Squares
Draw three squares in different colors.
One in red, one in green and one in blue.
Fill Shape Command
We can also fill in our shapes with colors:
turtle.fillcolor(“color_code”)
turtle.begin_fill()
turtle.end_fill()