Drawing
Python is good for things other than writing text. Python can draw as well. However, just like with everything else, we're going to need to learn some precursors first.
Python is good for things other than writing text. Python can draw as well. However, just like with everything else, we're going to need to learn some precursors first.
The first thing that you'll need to know is how to import modules. After that, we can get started right away. There are four ways to import modules. Here they are:
import modulename
: This lets you access things inside the module with modulename.item
.import modulename as newname
: This lets you access things inside the module with newname.item
.from modulename import *
: This lets you access everything inside the module without typing modulename.
before it.from modulename import items
: This lets you access items inside the module without typing modulename.
before them.For example, to import everything from the turtle
module, we use from turtle import *
.
Every turtle program that we make will have 3 lines of code at the beginning:
from turtle import *
t = Pen()
s = Screen()
The first line imports the turtle module. The second line creates a turtle to move around, and the third line creates the background for our turtle.
While making each program in this section, if that starter code is not shown and you are not instructed otherwise, add these lines of code to the beginning.
If you're using something other than IDLE, you should make sure that that website uses turtle.Pen()
and turtle.Screen()
. For example, Trinket uses turtle.Turtle()
instead of turtle.Pen()
.
To use a turtle command, such as forward, we do not do turtle.forward(size)
, but we call forward on our current turtle, namely t
. For example, to set the color to red, we do t.color("red")
(but that's getting ahead of ourselves).
Remember that if your turtle is named something besides t
, use that turtle's name instead of t
.
If you're only going to read a few turtle methods, read this section. This section is crucial to coding with turtle.
To move the turtle forward, use .forward(size)
. We can also move the turtle backward with .backward(size)
.
To turn the turtle left, use .left(angle)
. To turn right, use .right(angle)
. Note that the angles should be in degrees. You can also set the direction with .seth(angle)
. When using .seth()
, remember that 0 degrees points to the right.
To set the turtle's color, we use .color(color)
. Color can either be a color name or a hex value. Not all color names are supported, and they should be tested beforehand.
To draw a circle, use .circle(size)
. We can also draw partial circles with .circle(size,extent = degreeValue)
. For example, to draw a semicircle, use .circle(size,extent = 180)
.
To pull the turtle "off" the screen to allow moving without drawing, use .up()
. To place the turtle back "on" the screen, use .down()
.
To set the background color, use s.bgColor(color)
. Make sure to call this method on s
, our screen and NOT THE TURTLE!
from turtle import *
t = Pen()
s = Screen()
t.color("red")
t.forward(10)
t.circle(50)
t.left(90)
t.circle(50)
t.left(90)
t.circle(50)
t.left(90)
t.circle(50)
t.left(90)
t.up()
t.backward(10)
t.seth(270)
t.forward(50)
t.down()
t.forward(50)
The turtle that you use has many more pen functions that allow us to change the pen size, change the shape, hide the turtle! Let's learn some.
To change the pen size, use .pensize(size)
.
To change the pen shape, use .shape(shape)
. Available shapes are arrow
, turtle
, circle
, triangle
, square
, and classic
.
To change the pen speed, use .speed(0-10)
. Note that at speed 0, no animation takes place, making the turtle jump instead of draw.
To hide the turtle, use .hideturtle()
. To show it, use .showturtle()
.
Filling areas is one very fun thing that we can do with turtle. There are only a few commands to remember how to use for filling areas. Here they are:
To begin filling, use .begin_fill()
.
To end filling, use .end_fill()
. Until this is called, you will not see
As you should remember from earlier, .color(color)
sets the color. It actually sets both the pen and fill color. To set just the pen color, use .pencolor(color)
. To set just the fill color, use .fillcolor(color)
.