WEBVTT

0
00:00.240 --> 00:03.750
So we've now managed to create a snake body, move the snake,

1
00:03.810 --> 00:08.810
and we've even managed to put our snake related code into a separate class.

2
00:10.410 --> 00:14.940
Now the next step is to figure out how to control the snake.

3
00:15.600 --> 00:20.600
Now we're going to control the snake by using the up, down, left, and right arrow

4
00:21.810 --> 00:22.643
keys.

5
00:22.920 --> 00:27.150
And we're going to be using our key bindings that we learned in yesterday's

6
00:27.150 --> 00:32.150
lessons to be able to translate this into a change in direction of the snake. In

7
00:33.600 --> 00:36.780
our code right after we create our snake,

8
00:37.140 --> 00:42.140
we're going to call the screen.listen method to start listening for

9
00:42.450 --> 00:43.283
keystrokes.

10
00:43.740 --> 00:48.740
And the keystrokes that we're going to listen for are the up, down, left, and right

11
00:50.010 --> 00:54.510
arrow keys. Now those keys are described by a string.

12
00:54.810 --> 00:59.370
So it's a uppercase 'U' and then a 'p' so this would be up.

13
00:59.760 --> 01:03.750
And then it's again, uppercase 'Down', 'Left',

14
01:04.320 --> 01:06.810
and 'Right'.

15
01:07.320 --> 01:11.340
These are the keys that it will detect, obviously the up,

16
01:11.340 --> 01:13.710
down, left, and right arrow keys on the keyboard.

17
01:14.400 --> 01:19.110
The function that you're going to bind it to is going to be a function in the

18
01:19.110 --> 01:23.490
snake object and it's going to have the same name. So for example,

19
01:23.490 --> 01:25.230
it'd be snake.up,

20
01:25.320 --> 01:29.100
and that will be triggered when the up key is pressed.

21
01:29.550 --> 01:34.470
And then the same thing happens with snake.down when the down key gets

22
01:34.470 --> 01:37.530
pressed and then again with left and right.

23
01:38.130 --> 01:43.130
Your job is to have a think about how you can create these methods up,

24
01:44.160 --> 01:46.920
down, left, and right, in the snake class

25
01:47.370 --> 01:51.930
so that when the up key or the down key is detected,

26
01:52.260 --> 01:56.820
you can move the snake and change its heading accordingly.

27
01:57.240 --> 02:02.240
So remember that up is going to be towards North, down is going to be South, left

28
02:03.510 --> 02:07.560
is going to be towards the left of the screen and right is going to be towards

29
02:07.560 --> 02:08.430
the right of the screen.

30
02:08.820 --> 02:13.590
So have a think about the headings that we spoke about in previous lessons.

31
02:14.070 --> 02:18.090
And if you need to, it might be worth just setting up a brand new project,

32
02:18.390 --> 02:23.070
getting a turtle up and running and then setting it's heading from zero all the

33
02:23.070 --> 02:27.120
way to 360, just to see which direction it's actually facing.

34
02:27.510 --> 02:29.250
If you managed to complete the challenge,

35
02:29.280 --> 02:31.410
you should be able to control the snake

36
02:31.680 --> 02:34.410
and even though it's going to continue moving forward,

37
02:34.650 --> 02:39.450
you now determine which direction it moves in by using the arrow keys.

38
02:40.080 --> 02:43.320
Pause the video now and see if you can complete this challenge.

39
02:46.140 --> 02:49.860
All right. So I've already created all of these lines of code

40
02:49.890 --> 02:53.130
which is going to get the screen to listen for these key strokes.

41
02:53.580 --> 02:55.020
And then when it detects it,

42
02:55.050 --> 02:58.350
it's going to call these methods in the snake class.

43
02:58.710 --> 03:02.350
So inside our snake class, let's go ahead and create those methods.

44
03:02.470 --> 03:03.570
So we've got up,

45
03:05.820 --> 03:06.690
down,

46
03:08.910 --> 03:10.410
left, and right.

47
03:10.890 --> 03:15.890
Let's think about how we can turn the snake up. In order to turn the snake,

48
03:17.160 --> 03:20.820
the segment that we're most interested in is the first segment.

49
03:20.850 --> 03:22.740
That's basically the head of the snake.

50
03:23.040 --> 03:28.040
So we could tap into self.segments and then get hold of the zeroth segment,

51
03:29.070 --> 03:33.150
which is the first one, and then change its heading by calling setheading.

52
03:33.810 --> 03:36.900
And then we can tell it to, for example, if it's up,

53
03:36.930 --> 03:40.950
then it should actually be turning to the 90-degree direction,

54
03:40.980 --> 03:42.540
which is actually towards North.

55
03:43.230 --> 03:48.230
Now I'm going to go ahead and put a pass on all of these other methods,

56
03:49.110 --> 03:53.190
just so that we can actually test our code and it doesn't give us any errors

57
03:53.490 --> 03:58.020
because these functions are currently empty. Now, if you don't have the pass,

58
03:58.140 --> 04:02.280
it won't like it because Python doesn't like the idea of empty functions.

59
04:02.760 --> 04:05.970
But now we can at least just test this one thing.

60
04:06.600 --> 04:08.040
So when we run our code,

61
04:08.070 --> 04:12.570
we should be able to hit the up key and you can see the snake changed direction.

62
04:12.960 --> 04:13.950
Let me just do that again.

63
04:14.520 --> 04:18.210
And this is because we managed to get the first segment,

64
04:18.240 --> 04:20.190
the head of the snake, to change

65
04:20.190 --> 04:25.020
it's heading to 90 degrees and then the rest of the body will follow because

66
04:25.020 --> 04:28.110
it's continuously moving on every tick of the clock.

67
04:28.710 --> 04:32.550
So using this, we can now set the rest of the methods.

68
04:32.910 --> 04:36.660
But because we're going to need the head of the snake quite a few times in our

69
04:36.660 --> 04:41.610
code, it might make sense to actually create a separate attribute.

70
04:42.060 --> 04:46.980
So after we've created the snake, after we've got some segments inside this list,

71
04:47.310 --> 04:49.500
we can create an attribute called head,

72
04:49.590 --> 04:51.180
which is going to be the head of the snake.

73
04:51.540 --> 04:56.160
And this can be equal to self.segments at index zero. Now,

74
04:56.160 --> 04:59.430
remember if you have this line of code above the create_snake,

75
04:59.460 --> 05:03.990
it's probably going to error out because at this point the segments is empty

76
05:04.350 --> 05:08.160
and if you try to get the first item from it, it's not going to allow you to.

77
05:08.610 --> 05:13.440
So look at the positioning of the code here. Now, once we've got this set,

78
05:13.500 --> 05:16.500
we can go down here and change everywhere

79
05:16.500 --> 05:19.590
where we need to use the head of the snake. So for example,

80
05:19.830 --> 05:24.030
when we're moving the head forwards and when we're moving it up,

81
05:24.060 --> 05:28.080
left, down, and right. Now we can set

82
05:28.110 --> 05:33.110
each of these headings. Up is going to be a heading of 90, down is going to be 

83
05:34.560 --> 05:35.393
270,

84
05:36.000 --> 05:41.000
left is 180 and right is 0.

85
05:41.760 --> 05:45.420
These directions are just simply counter-clockwise.

86
05:45.450 --> 05:49.740
So the turtle starts out pointing towards East and that's a heading of zero,

87
05:50.070 --> 05:55.070
and then going anticlockwise, up is 90, left is 180, down as 270.

88
05:55.920 --> 06:00.020
This can be easily verified just by testing out some numbers, right?

89
06:00.020 --> 06:04.190
You could just put some numbers in there and then just try it out and see which

90
06:04.190 --> 06:07.130
direction it goes and then you'll be able to figure it out.

91
06:07.580 --> 06:10.040
But we've also mentioned this in previous lessons

92
06:10.040 --> 06:13.730
so you could also go back and take a look at when we looked at the heading of

93
06:13.760 --> 06:18.020
the turtle. So now if I go ahead and run this,

94
06:18.320 --> 06:23.240
then you should see that I can now freely control the snake by going left or

95
06:23.240 --> 06:28.100
right or down or up. And if you managed to do this,

96
06:28.160 --> 06:32.000
then you should consider yourself successful. Now,

97
06:32.030 --> 06:34.160
while I was testing this code,

98
06:34.400 --> 06:39.020
I realized that there's something about the snake game that we haven't really

99
06:39.380 --> 06:40.213
accounted for.

100
06:40.730 --> 06:44.390
And it's the fact that the snake can't move back on itself.

101
06:44.780 --> 06:49.310
It can't go forwards and then go the opposite direction because that requires

102
06:49.310 --> 06:52.040
the head to change directions.

103
06:52.280 --> 06:54.590
And this is not allowed in the official snake game.

104
06:55.070 --> 06:59.420
So how can we code this into our game? Well,

105
06:59.750 --> 07:04.750
we have to figure out when the head is pointing towards down direction,

106
07:06.980 --> 07:11.360
then we shouldn't allow it to go up. And similarly, when it's pointing up,

107
07:11.390 --> 07:14.000
we shouldn't let it go down, when it's pointing right

108
07:14.030 --> 07:17.150
we shouldn't let it go left. So at the top here,

109
07:17.180 --> 07:18.740
I'm going to create some constants.

110
07:18.770 --> 07:23.770
I'm going to create a UP direction and it's going to be 90, DOWN direction is going to

111
07:24.920 --> 07:29.920
be 270, LEFT is going to be equal to 180 and RIGHT is going to be equal to

112
07:33.230 --> 07:37.730
0. So now that I've got all of these as constants,

113
07:37.880 --> 07:42.880
I can go in here and set the heading to instead say UP when it needs to go up,

114
07:47.620 --> 07:51.640
DOWN, LEFT and RIGHT. In addition,

115
07:51.670 --> 07:53.530
we're going to do an if check.

116
07:53.560 --> 07:58.560
So we're going to say if the current self.head.heading

117
08:01.810 --> 08:06.190
is not equal to down, well, in that case,

118
08:06.250 --> 08:09.190
it's allowed to move up. So this way,

119
08:09.190 --> 08:12.700
it just means that if the current heading is pointed down,

120
08:13.150 --> 08:16.510
it can't move up. But for all other directions,

121
08:16.540 --> 08:18.970
if it's already moving up or left or right,

122
08:19.240 --> 08:21.280
then it can change the heading to up.

123
08:23.350 --> 08:27.430
Now be really careful here because it's not just self.head.heading,

124
08:27.700 --> 08:32.700
it's actually heading as a method because remember that the head of the snake is

125
08:34.150 --> 08:37.150
the first segment of our list of segments

126
08:37.630 --> 08:40.810
and each segment is a individual turtle.

127
08:41.410 --> 08:44.230
The turtle has a heading method

128
08:44.410 --> 08:48.820
which will give you a direction in terms of these 360-degree numbers,

129
08:49.270 --> 08:53.950
and then we can use that to check to see if it's equal to down.

130
08:54.370 --> 08:57.780
And if it is, then it's not allowed to go up.

131
08:58.590 --> 09:00.780
So using this logic,

132
09:00.810 --> 09:05.810
see if you can complete the rest of the three methods so that when you run your

133
09:06.990 --> 09:10.020
code, you can turn in all directions.

134
09:10.440 --> 09:12.660
But when you're facing one direction,

135
09:12.690 --> 09:15.750
you can't go backwards, like this.

136
09:16.520 --> 09:17.353
<v 2>Okay?</v>

137
09:20.480 --> 09:22.880
<v 0>Alright. So it's going to be pretty straightforward.</v>

138
09:23.240 --> 09:27.950
If it's already going up, then it's not allowed to go down.

139
09:28.640 --> 09:31.520
If it's already going right,

140
09:31.820 --> 09:35.990
then it's not allowed to go left. And finally,

141
09:36.050 --> 09:40.310
if it's already going left, then it's not allowed to go right.

142
09:41.810 --> 09:45.950
So this way, even though our snake can move freely through the space,

143
09:46.280 --> 09:49.880
it can't go back on itself. And there we have it.

144
09:49.910 --> 09:53.990
We've now managed to create our snake, get it to move and control it

145
09:54.080 --> 09:56.150
using our key presses.

146
09:56.690 --> 10:00.590
Even though it seems like our game is still not quite there,

147
10:00.860 --> 10:02.660
we've already come so far.

148
10:02.990 --> 10:05.930
So this comes up to our hour mark

149
10:06.290 --> 10:10.010
and it's now time to take a break, refresh your minds,

150
10:10.370 --> 10:13.850
look at some of the concepts that you might be confused about that we covered

151
10:13.850 --> 10:14.683
today,

152
10:14.720 --> 10:19.040
especially this concept of using the update to refresh the screen,

153
10:19.460 --> 10:21.680
as well as using the timer

154
10:21.710 --> 10:26.570
to delay the refresh so that we can control how often it happens.

155
10:26.960 --> 10:31.610
And then using our key bindings as well as separating all classes.

156
10:31.880 --> 10:34.910
We looked at a lot of things today. Now, tomorrow,

157
10:34.970 --> 10:37.370
once you're rested and fresh and ready,

158
10:37.640 --> 10:39.860
we're going to be finishing up the snake game.

159
10:40.310 --> 10:44.330
And we're going to be learning about inheritance as well as slicing

160
10:44.750 --> 10:48.350
and we're going to be building out the full functionality of this game.

161
10:48.680 --> 10:53.600
So I hope you're looking forward to it. I certainly am. I'll see you tomorrow.