Turtle graphics is an homage to classic graphics being drawn on computers. Truthfully it's a great learning resource as it's an accurate representation of how computers draw graphics. Programs such as game engines, photo editing software, drawing software, and even office applications draw graphics in a very similar manor, but have their own built in methods for drawing. After you complete this unit, if you're interested in photo editing I would suggest the pillow library or Python's image library. Or check out RayLib for python if you want to extend your learning into 3D. If you really want to get into graphics and the sciences behind it you will likely be doing lots of math and learning C++ and OpenGL.
We're going to draw rudimentary programs.
Line 1: imports the turtle library, sometimes we need to import libraries that aren't already included in vanilla python
Line 3: Made a new turtle and named it "speedy"
Line 5: make speedy walk 100 pixels forward
Line 7: end of turtle program, you need this to prevent the window from closing.
This will let us walk forward and back (using negative numbers), so that's not entirely useful. What if we want to turn? well we have to tell speedy left or right and by how many degrees:
Line 6: speedy turns 90 degrees left
Line 7: speedy walks forward 100 pixels
This should be enough to get you started.
Is the turtle moving too fast? Well you can adjust the speed by using the name of your turtle then the speed method. For our turtle speedy we would write:
speedy.speed(1)
1 is the slowest, 10 is the second fastest... but 0 is almost instant. It's weird but you can get by using just 1-10.
So we know how to make functions, but we can make functions that take in values to know what to do with them. This is useful in this unit because you can make your own custom shape and put in numbers to have them drawn. here's an example:
Line 5: declared a function but put in a variable inside the brackets. This will take on the value of whatever is in the brackets once called (50 on line 10 in this case so side = 50)
Line 6: made a for loop to represent the 5 sides of the pentagon
Line 7: speedy moves forward as many pixels as what is passed to the function (will be 50)
Line 8: speedy turns left 72 degrees. (may have to use math to figure out turn degrees)
Line 10: calls the drawPentagon function and passes the 50 into the side length variable.
We can reuse this function over and over, even in a loop if we'd like:
Here we see we drew three pentagons with different side lengths. You could also move speedy in between to try and center the pentagons. Functions are very useful in making reusable code.
Black and white graphics are pretty boring so luckily there's a way to use different pen and fill colours.
Line 5: changes speedy's pen colour to red. Note that it uses American spelling of color.
You can use some common strings in the pencolor() function, but you can also use hexadecimal values. To find the perfect colour you can use this website:
Hexadecimal colours
You can then paste it in the pencolor function and use it like this:
Line 5: I made the pen colour #FDCD16 which is a custom colour.
We can also set a fill colour so that whenever the turtle completes a shape it will then fill in the colour. If it doesn't complete the shape it will fill in from its start point to end point along the line(s) it has already drawn.
Line 8: set the fill colour to blue
Line 10: begin fill so once I'm done drawing a shape I can fill it.
Lines 12-15: code to draw a square... Not given because that's a previous assignment.
Line 17: ends the fill.
We can also set the background of the drawing.
Line 6: sets up a screen variable
Line 7: sets the background to black
Being able to move forward and back is useful but so far you can't disconnect things in your drawings. To do this we need to use the pen up and pen down commands.
Line 6: lifts up the pen so it won't draw while the turtle is moving now.
...
Line 8: puts the pen back down to start drawing again.
We can move around now with the pen up but it's hard to tell where our turtle is, here's some tools that will help you move them around:
goto(x,y): sends the turtle to an x, y coordinate (the turtle starts at 0,0 so you may have negative numbers)
setx(x): moves the turtle horizontally (left or right) to the x value in the brackets
sety(y): moves the turtle vertically (up or down) to the y value in the brackets
Line 7: uses goto to send speedy to (-20, 20)
Changed the pen colour to red for the second line to show the second line
If you lose your turtle from moving around so much you can tell it to say it's position. Here's an example where franklin tells us where he is:
Line 14: prints the position into the console.
You can change your turtle's shape to 3 different things, by default it's "classic", but there's many other shapes that you can find here:
You can then stamp that shape using the stamp() function. Here's how to change and stamp:
Line 4: speedy stamps while in his classic shape.
Line 10: changes shape to turtle
Line 11: stamps turtle
Line 15: changed shape to square and program ends.
You can also set the heading 0 is east (right), 90 is north (up), 180 is west (left), and 270 is south (down). This will help you rotate your turtle back around if needed. and it would be written like this:
We can draw straight lines but what about circles and curves? We can use the circle() function
Line 4: draws a circle with a radius of 50, meaning it's actually 100 pixels across.
What if we want to draw half a circle? Well we can add an arguement to the circle function.
Line 4: draws half a circle of radius 50. Notice we use degrees to say how far it goes, so 360 would be a full circle, 180 is half, 90 is a quarter, and so on.