WEBVTT

0
00:00.270 --> 00:03.870
Now in order to be able to create a lot of the games that we've spoken about,

1
00:04.290 --> 00:09.290
we need a way of being able to listen to things the user does,

2
00:10.020 --> 00:14.580
like when the user taps a specific key on the keyboard. And the code that allows

3
00:14.580 --> 00:17.220
us to do this are called event listeners.

4
00:17.610 --> 00:20.370
So if we look at the turtle documentation,

5
00:20.520 --> 00:24.150
you can see that there's a whole section on screen events,

6
00:24.540 --> 00:29.540
including listening to key presses or listening to a click or other things that

7
00:31.050 --> 00:35.700
you can listen to. So the important thing is this listen method.

8
00:36.150 --> 00:41.150
This allows the turtle screen to start listening and waiting for events that the

9
00:42.180 --> 00:44.910
user might trigger, like tapping on a key.

10
00:45.540 --> 00:50.460
I've gone ahead and created a new PyCharm project which I've called day-19

11
00:50.850 --> 00:54.810
and I've created my main.py file inside the project. Now,

12
00:54.810 --> 00:59.810
the first thing I'm going to do is I'm going to import the turtle class and the

13
01:00.090 --> 01:02.400
screen class from the turtle module.

14
01:02.850 --> 01:06.600
And then I'm going to use them to create my Tim the turtle,

15
01:08.310 --> 01:12.000
and also create a screen object.

16
01:12.390 --> 01:17.130
This is basically going to control the window when we run our code.

17
01:17.160 --> 01:19.560
So you saw it just flash just now.

18
01:20.160 --> 01:22.770
Now in order to start listening for events,

19
01:22.800 --> 01:27.570
we have to get hold of the screen object and then tell it to start listening.

20
01:28.140 --> 01:29.970
And once it starts listening,

21
01:30.000 --> 01:34.920
we have to bind a function that will be triggered when a particular key is

22
01:34.920 --> 01:37.470
pressed on the keyboard. Now,

23
01:37.530 --> 01:41.730
in order to bind a key stroke to an event in our code,

24
01:42.090 --> 01:44.460
we have to use an event listener.

25
01:45.090 --> 01:48.990
So the one that we're going to be using is this onkey method.

26
01:49.350 --> 01:54.350
And you can see it expects a function with no arguments and also a key,

27
01:56.250 --> 01:57.060
like the

28
01:57.060 --> 02:02.060
'a' key on your keyboard or a key symbol, so the space or up or down.

29
02:02.700 --> 02:05.610
So I'm going to say screen.onkey

30
02:06.180 --> 02:10.260
and then I have to bind some sort of function to this method.

31
02:10.500 --> 02:14.400
So let's create a function and we'll call it move_

32
02:14.990 --> 02:15.860
forwards.

33
02:17.510 --> 02:22.340
<v 0>And all that this function is going to do is it's going to get Tim to go forward</v>

34
02:22.400 --> 02:26.780
by 10 paces. And let's not forget our colon here.

35
02:27.320 --> 02:30.200
Now, coming back to my onkey method,

36
02:30.440 --> 02:35.330
I'm going to say that when the space key, so the space bar, is pressed,

37
02:35.750 --> 02:38.450
then trigger the function move_forwards.

38
02:38.990 --> 02:43.990
Now here's a really important point; when we use a function as a argument,

39
02:44.270 --> 02:48.230
so something that is going to be passed into another function,

40
02:48.650 --> 02:51.440
we don't actually add the parentheses at the end.

41
02:51.920 --> 02:55.430
The parentheses triggers the function to happen there and then.

42
02:55.880 --> 03:00.880
But what we want is we want this method, onkey, to listen for when the space bar

43
03:02.710 --> 03:07.710
is pressed and only when that happens to trigger this move_forwards function.

44
03:08.770 --> 03:13.750
Now, the final thing I need to do is to add the exitonclick so that my screen

45
03:13.750 --> 03:15.460
doesn't disappear when I run it.

46
03:16.180 --> 03:20.470
And now what I'm going to try is when I hit the spacebar,

47
03:20.860 --> 03:25.780
you should see my turtle move forwards by 10 paces each time.

48
03:26.440 --> 03:30.790
So we're now controlling our turtle using a keystroke.

49
03:31.360 --> 03:36.360
What we've essentially got here is a function that's being used as an input.

50
03:37.270 --> 03:39.970
Let's say we have a function called function_a

51
03:39.970 --> 03:42.490
that takes some sort of input.

52
03:43.090 --> 03:48.090
And then we have a function called function_b, we can actually pass function_b

53
03:48.280 --> 03:50.980
into function_a like this.

54
03:51.400 --> 03:54.370
So notice that when we pass a function as an input,

55
03:54.670 --> 03:58.480
we only pass the name without the parentheses at the end.

56
03:59.050 --> 04:03.220
But how exactly does this function onkey work?

57
04:03.250 --> 04:08.250
Because it's kind of crazy to use a function as the input to another function,

58
04:09.670 --> 04:10.390
right?

59
04:10.390 --> 04:15.390
So want to show you a quick example of passing a function into another function,

60
04:16.540 --> 04:21.190
and I'm going to use the calculator example that we did many,

61
04:21.190 --> 04:26.080
many moons ago. Um, when we created a calculator that could take two numbers,

62
04:26.170 --> 04:29.410
add, subtract, multiply, or divide those numbers.

63
04:29.980 --> 04:32.260
So we've got our four functions here. Now,

64
04:32.290 --> 04:35.260
you don't actually have to write any of this out, cause it's quite tedious.

65
04:35.590 --> 04:37.480
I just want you to watch and listen.

66
04:38.170 --> 04:42.880
So let's say I create a final function, which I'll call calculator.

67
04:43.630 --> 04:48.610
And this function is going to take a n1 and n2, two numbers,

68
04:49.000 --> 04:53.050
but it's also going to take a function as an input.

69
04:53.770 --> 04:56.110
Now, inside this calculator function,

70
04:56.260 --> 05:00.010
I'm going to take the function name that was passed in

71
05:00.460 --> 05:05.460
and then call that function by adding the parentheses and then passing in n1

72
05:06.100 --> 05:10.300
and n2. So now I have my calculator function

73
05:10.690 --> 05:13.840
which can take any function as an input.

74
05:14.140 --> 05:16.630
And once that function has been passed in here,

75
05:17.050 --> 05:22.050
then that function will get triggered passing in n1 and n2. Let's return

76
05:23.590 --> 05:25.510
this result as the output,

77
05:25.900 --> 05:28.690
and then let's call our calculator function,

78
05:28.990 --> 05:33.340
let's pass in some numbers and let's pass in the name of a function.

79
05:34.060 --> 05:39.060
So now let's go ahead and save this into a variable and then print that variable

80
05:40.450 --> 05:43.000
result. So now when I run the code,

81
05:43.030 --> 05:48.030
you can see I get five printed and that's because I've got add as the third

82
05:48.070 --> 05:52.630
input here. But what happens if I change this to multiply? Well,

83
05:52.630 --> 05:54.700
I get six instead of five.

84
05:55.150 --> 05:59.360
And what happens if I put divide? Well, I get zero because,

85
05:59.380 --> 06:01.210
because it's rounding up to a whole number.

86
06:01.690 --> 06:05.470
But essentially what I'm trying to demonstrate to you is this concept

87
06:05.620 --> 06:09.640
which is known in Python as Higher Order Functions.

88
06:10.150 --> 06:15.150
And the idea of a higher order function is a function that can work with other

89
06:15.670 --> 06:17.650
functions. So in this case,

90
06:17.710 --> 06:22.710
our calculator is a higher order function because it's actually taking another

91
06:23.230 --> 06:28.180
function as an input, and then working with it inside the body of the function.

92
06:28.870 --> 06:33.190
Now there are certain programming languages where you can't do this at all.

93
06:33.460 --> 06:36.280
But in Python, this is pretty commonly used.

94
06:36.550 --> 06:41.550
And it's really useful when we need to listen for events and then trigger a

95
06:42.160 --> 06:46.270
particular function. Now, while we're on the topic of functions,

96
06:46.450 --> 06:51.340
I recommend that when you're using methods that you haven't created yourself

97
06:51.370 --> 06:55.330
like for example onkey, to use keyword arguments

98
06:55.360 --> 06:59.050
instead of positional arguments especially if like in this case,

99
06:59.260 --> 07:01.390
the position doesn't actually have any meaning.

100
07:02.230 --> 07:04.630
So now that you've seen how event listeners work,

101
07:04.870 --> 07:08.980
we're going to be using event listeners to build out our Etch-A-Sketch program

102
07:09.040 --> 07:12.790
in the next lesson. So for all of that and more, I'll see you there.