As I mentioned, in this section
we're going to be working heavily with the Python turtle module.
And as we've seen before,
this turtle module is essentially a way for us to be able to draw graphics onto
the screen.
And you can imagine it as just this little wandering turtle with a pen on its
back and everywhere it goes, it leaves a trail of line.
So let's take a closer look at the turtle graphics module. Now here,
I've already created a brand new PyCharm project called day-18-start,
and I've created a blank main.py. Inside this project,
we're going to import our turtle. So as you saw from previous lessons,
we can import the turtle module and get hold of the turtle class
by simply saying from the turtle module,
import the Turtle class with the capital T. And now we can work with that class
and we're going to use it to create a new turtle object.
Let's call it timmy_the_turtle,
and this is going to be created from the turtle class.
So this is going to be a new turtle object saved inside this variable.
Now, when we actually run our project,
you'll see a brief flash of a window
and then it will disappear. In order for that window to stay where it is
and only exit when we actually click on the window,
we have to create another object, which we'll call our screen.
And this comes from the total module as well.
And it's a class called Screen.
We also have to import that if we want to use it.
So now that we've got hold of the screen,
we can use one of the functions called exitonclick.
So this way, when we run our code,
we can see our turtle show up in the middle as this little arrow and our window
won't disappear until we click on it. That's what exitonclick does.
So we need to keep this bit of the code at the very bottom.
It has to happen after we've done all this stuff with turtle.
So I'm going to move it to the bottom of the file. Now here's a question.
How do I know how to do that? Is it because I am super experienced
and I remember everything off the top of my head? No.
Is it because I'm super clever and I can just work things out? No,
that's definitely not the answer.
The way that programmers go about figuring out how to use these modules or
packages is through the use of documentation.
So let's take a look at the turtle graphics documentation.
And this is what the documentation looks like. It's a very,
very long document that goes through all of the things that you can do with this
module. Now, if we scroll down,
you've got a contents page here and you can see the various things you can do
with the turtle. Control it's motion or control the pen or change its visibility
or appearance. Now let's take a look at this shape function,
and you can see the documentation explains to us that this particular function
will set the turtles shape to one with a given name.
And we can choose from the various shapes, including arrow, turtle, circle,
square, triangle, and classic. So
let's use this knowledge and change timmy_the_turtle to have a different shape.
So inside here, we can put any of those strings.
So I'm going to change it to a turtle shape. This way when I run my code,
you can see an actual turtle appear on the screen.
It just makes it a little bit easier to visualize what's going on with this
module. Now, of course you can change it to any of the other ones,
test it out and see if it works. In an ideal world
you would read through the entire documentation of a module before you start
using it. But of course, in the real world,
we all know that that's not quite possible.
Very often you'll need the help of Stack Overflow. For example,
if we wanted to look at turtle graphics and we wanted to change the shape
of the turtle.
0: Then you can see that when you actually search for this inside the Stack
Overflow search bar, your results are not very relevant.
But if I put the same query into google.com and at the end I tag on the keyword
Stack Overflow, we get results that are a lot more relevant.
So if we take a look at this post,
you can see that it's starting to talk about this shape method.
And down here, this answer even links to the relevant part of the documentation
which we've already seen previously, this shape method.
The idea is that you might need to Google around to see how to achieve a certain
thing you want to do.
And then once you've seen some of the methods that they mentioned in the
answers,
then go to the documentation and read up on it in order to actually understand
what it does and how to use it. Coming back to our turtle,
what if we wanted to change the color of the turtle? Well,
this should be quite easy. We can see that in the color control section,
we can use this color method to set the pen color and fill color.
So for example, we could say timmy_the_turtle.color,
and we can put in a string here like red.
So now when I run my code,
you can see that my turtle timmy is now red instead of the previous default
color of black. Now,
how do I know which colors I can use other than the ones shown in the example,
red or green? Well,
it says that when we want to change the color using a color string or an RGB
color, then it accepts inputs as in pen color.
So it basically says it's using this method to set the color. And this method
gets the color from a Tk color specifications string.
So let's Google and see what that is.
If we click on the first link,
you can see it takes us to the strings and we've got a whole bunch of names and
their corresponding RGB values.
So what is this Tk exactly?
Tk is short for the module tkinter,
which is the Tk interface.
And this is one of the ways that you can use Python to create a graphical user
interface, which is also known as a GUI.
If you think back to the beginning of computer history,
if you think about the Apple Lisa one,
that was the first computer that had a graphical user interface,
where it had a mouse and you can point and click.
And this was huge back in the day.
But before then we had text interfaces like MS-
DOS or like our console when we're using Python.
The text interfaces accept text commands and the graphical user interfaces can
show images and allows you to click and drag and do all of those things by
looking instead of just typing commands. And tkinter is what the turtle module
actually relies on under the hood to create these graphics,
like our turtle showing up here.
So now that we've seen where these names come from,
a much easier way of using these colors is through a page where they have all
been rendered on screen. So in the course resources,
we link to this page where you can see all the colors next to their names,
and you can pick and choose whichever one you want.
Have a go at changing the color of the turtle by using one of these colors that
you see on screen.
Now that we've seen how we can create a new turtle and change its appearance,
it's time to look at how we can make it do certain things. In the documentation,
you can see there's a whole bunch of things you can do in terms of moving and
drawing. A very simple one would be to move it forward or to move it backwards.
So we can tell it to move forwards by a hundred paces.
And when we rerun the code, this is what we see.
So we can get it to move forwards, move backwards, and also move left
and right. So here, the input that it takes is an angle,
a number between zero and 360 to tell it what angle
to turn right or to turn left. For example,
currently our turtle is facing East. So if we wanted to face
2: South,
then we have to get it to turn right by 90 degrees. So we can say,
timmy_the_turtle.right and then we put 90 in here.
And that's what it looks like. In the upcoming lessons
we've got a whole bunch of turtle challenges for you
that's going to test not only your knowledge of programming that you've learned so
far, but also how well you can read and understand documentation.
This is a key skill to acquire as a seasoned developer because after all,
we can't always rely on other people telling us what to do.
We have to be able to get it from the source.
So make sure that you've got the documentation pulled up and have a glance
through it.
Then you can head over to the next lesson and start tackling some of these
challenges.
Now remember that some of these challenges can be quite a bit of a struggle,
but keep in mind that the struggle is good. The more that you struggle,
the stronger you get. I'll see you on the next lesson.