Now in order to be able to create a lot of the games that we've spoken about,
we need a way of being able to listen to things the user does,
like when the user taps a specific key on the keyboard. And the code that allows
us to do this are called event listeners.
So if we look at the turtle documentation,
you can see that there's a whole section on screen events,
including listening to key presses or listening to a click or other things that
you can listen to. So the important thing is this listen method.
This allows the turtle screen to start listening and waiting for events that the
user might trigger, like tapping on a key.
I've gone ahead and created a new PyCharm project which I've called day-19
and I've created my main.py file inside the project. Now,
the first thing I'm going to do is I'm going to import the turtle class and the
screen class from the turtle module.
And then I'm going to use them to create my Tim the turtle,
and also create a screen object.
This is basically going to control the window when we run our code.
So you saw it just flash just now.
Now in order to start listening for events,
we have to get hold of the screen object and then tell it to start listening.
And once it starts listening,
we have to bind a function that will be triggered when a particular key is
pressed on the keyboard. Now,
in order to bind a key stroke to an event in our code,
we have to use an event listener.
So the one that we're going to be using is this onkey method.
And you can see it expects a function with no arguments and also a key,
like the
'a' key on your keyboard or a key symbol, so the space or up or down.
So I'm going to say screen.onkey
and then I have to bind some sort of function to this method.
So let's create a function and we'll call it move_
forwards.
0: And all that this function is going to do is it's going to get Tim to go forward
by 10 paces. And let's not forget our colon here.
Now, coming back to my onkey method,
I'm going to say that when the space key, so the space bar, is pressed,
then trigger the function move_forwards.
Now here's a really important point; when we use a function as a argument,
so something that is going to be passed into another function,
we don't actually add the parentheses at the end.
The parentheses triggers the function to happen there and then.
But what we want is we want this method, onkey, to listen for when the space bar
is pressed and only when that happens to trigger this move_forwards function.
Now, the final thing I need to do is to add the exitonclick so that my screen
doesn't disappear when I run it.
And now what I'm going to try is when I hit the spacebar,
you should see my turtle move forwards by 10 paces each time.
So we're now controlling our turtle using a keystroke.
What we've essentially got here is a function that's being used as an input.
Let's say we have a function called function_a
that takes some sort of input.
And then we have a function called function_b, we can actually pass function_b
into function_a like this.
So notice that when we pass a function as an input,
we only pass the name without the parentheses at the end.
But how exactly does this function onkey work?
Because it's kind of crazy to use a function as the input to another function,
right?
So want to show you a quick example of passing a function into another function,
and I'm going to use the calculator example that we did many,
many moons ago. Um, when we created a calculator that could take two numbers,
add, subtract, multiply, or divide those numbers.
So we've got our four functions here. Now,
you don't actually have to write any of this out, cause it's quite tedious.
I just want you to watch and listen.
So let's say I create a final function, which I'll call calculator.
And this function is going to take a n1 and n2, two numbers,
but it's also going to take a function as an input.
Now, inside this calculator function,
I'm going to take the function name that was passed in
and then call that function by adding the parentheses and then passing in n1
and n2. So now I have my calculator function
which can take any function as an input.
And once that function has been passed in here,
then that function will get triggered passing in n1 and n2. Let's return
this result as the output,
and then let's call our calculator function,
let's pass in some numbers and let's pass in the name of a function.
So now let's go ahead and save this into a variable and then print that variable
result. So now when I run the code,
you can see I get five printed and that's because I've got add as the third
input here. But what happens if I change this to multiply? Well,
I get six instead of five.
And what happens if I put divide? Well, I get zero because,
because it's rounding up to a whole number.
But essentially what I'm trying to demonstrate to you is this concept
which is known in Python as Higher Order Functions.
And the idea of a higher order function is a function that can work with other
functions. So in this case,
our calculator is a higher order function because it's actually taking another
function as an input, and then working with it inside the body of the function.
Now there are certain programming languages where you can't do this at all.
But in Python, this is pretty commonly used.
And it's really useful when we need to listen for events and then trigger a
particular function. Now, while we're on the topic of functions,
I recommend that when you're using methods that you haven't created yourself
like for example onkey, to use keyword arguments
instead of positional arguments especially if like in this case,
the position doesn't actually have any meaning.
So now that you've seen how event listeners work,
we're going to be using event listeners to build out our Etch-A-Sketch program
in the next lesson. So for all of that and more, I'll see you there.