1
00:00:00,240 --> 00:00:03,750
So we've now managed to create a snake body, move the snake,

2
00:00:03,810 --> 00:00:08,810
and we've even managed to put our snake related code into a separate class.

3
00:00:10,410 --> 00:00:14,940
Now the next step is to figure out how to control the snake.

4
00:00:15,600 --> 00:00:20,600
Now we're going to control the snake by using the up, down, left, and right arrow

5
00:00:21,810 --> 00:00:22,643
keys.

6
00:00:22,920 --> 00:00:27,150
And we're going to be using our key bindings that we learned in yesterday's

7
00:00:27,150 --> 00:00:32,150
lessons to be able to translate this into a change in direction of the snake. In

8
00:00:33,600 --> 00:00:36,780
our code right after we create our snake,

9
00:00:37,140 --> 00:00:42,140
we're going to call the screen.listen method to start listening for

10
00:00:42,450 --> 00:00:43,283
keystrokes.

11
00:00:43,740 --> 00:00:48,740
And the keystrokes that we're going to listen for are the up, down, left, and right

12
00:00:50,010 --> 00:00:54,510
arrow keys. Now those keys are described by a string.

13
00:00:54,810 --> 00:00:59,370
So it's a uppercase 'U' and then a 'p' so this would be up.

14
00:00:59,760 --> 00:01:03,750
And then it's again, uppercase 'Down', 'Left',

15
00:01:04,319 --> 00:01:06,810
and 'Right'.

16
00:01:07,320 --> 00:01:11,340
These are the keys that it will detect, obviously the up,

17
00:01:11,340 --> 00:01:13,710
down, left, and right arrow keys on the keyboard.

18
00:01:14,400 --> 00:01:19,110
The function that you're going to bind it to is going to be a function in the

19
00:01:19,110 --> 00:01:23,490
snake object and it's going to have the same name. So for example,

20
00:01:23,490 --> 00:01:25,230
it'd be snake.up,

21
00:01:25,320 --> 00:01:29,100
and that will be triggered when the up key is pressed.

22
00:01:29,550 --> 00:01:34,470
And then the same thing happens with snake.down when the down key gets

23
00:01:34,470 --> 00:01:37,530
pressed and then again with left and right.

24
00:01:38,130 --> 00:01:43,130
Your job is to have a think about how you can create these methods up,

25
00:01:44,160 --> 00:01:46,920
down, left, and right, in the snake class

26
00:01:47,370 --> 00:01:51,930
so that when the up key or the down key is detected,

27
00:01:52,260 --> 00:01:56,820
you can move the snake and change its heading accordingly.

28
00:01:57,240 --> 00:02:02,240
So remember that up is going to be towards North, down is going to be South, left

29
00:02:03,510 --> 00:02:07,560
is going to be towards the left of the screen and right is going to be towards

30
00:02:07,560 --> 00:02:08,430
the right of the screen.

31
00:02:08,820 --> 00:02:13,590
So have a think about the headings that we spoke about in previous lessons.

32
00:02:14,070 --> 00:02:18,090
And if you need to, it might be worth just setting up a brand new project,

33
00:02:18,390 --> 00:02:23,070
getting a turtle up and running and then setting it's heading from zero all the

34
00:02:23,070 --> 00:02:27,120
way to 360, just to see which direction it's actually facing.

35
00:02:27,510 --> 00:02:29,250
If you managed to complete the challenge,

36
00:02:29,280 --> 00:02:31,410
you should be able to control the snake

37
00:02:31,680 --> 00:02:34,410
and even though it's going to continue moving forward,

38
00:02:34,650 --> 00:02:39,450
you now determine which direction it moves in by using the arrow keys.

39
00:02:40,080 --> 00:02:43,320
Pause the video now and see if you can complete this challenge.

40
00:02:46,140 --> 00:02:49,860
All right. So I've already created all of these lines of code

41
00:02:49,890 --> 00:02:53,130
which is going to get the screen to listen for these key strokes.

42
00:02:53,580 --> 00:02:55,020
And then when it detects it,

43
00:02:55,050 --> 00:02:58,350
it's going to call these methods in the snake class.

44
00:02:58,710 --> 00:03:02,350
So inside our snake class, let's go ahead and create those methods.

45
00:03:02,470 --> 00:03:03,570
So we've got up,

46
00:03:05,820 --> 00:03:06,690
down,

47
00:03:08,910 --> 00:03:10,410
left, and right.

48
00:03:10,890 --> 00:03:15,890
Let's think about how we can turn the snake up. In order to turn the snake,

49
00:03:17,160 --> 00:03:20,820
the segment that we're most interested in is the first segment.

50
00:03:20,850 --> 00:03:22,740
That's basically the head of the snake.

51
00:03:23,040 --> 00:03:28,040
So we could tap into self.segments and then get hold of the zeroth segment,

52
00:03:29,070 --> 00:03:33,150
which is the first one, and then change its heading by calling setheading.

53
00:03:33,810 --> 00:03:36,900
And then we can tell it to, for example, if it's up,

54
00:03:36,930 --> 00:03:40,950
then it should actually be turning to the 90-degree direction,

55
00:03:40,980 --> 00:03:42,540
which is actually towards North.

56
00:03:43,230 --> 00:03:48,230
Now I'm going to go ahead and put a pass on all of these other methods,

57
00:03:49,110 --> 00:03:53,190
just so that we can actually test our code and it doesn't give us any errors

58
00:03:53,490 --> 00:03:58,020
because these functions are currently empty. Now, if you don't have the pass,

59
00:03:58,140 --> 00:04:02,280
it won't like it because Python doesn't like the idea of empty functions.

60
00:04:02,760 --> 00:04:05,970
But now we can at least just test this one thing.

61
00:04:06,600 --> 00:04:08,040
So when we run our code,

62
00:04:08,070 --> 00:04:12,570
we should be able to hit the up key and you can see the snake changed direction.

63
00:04:12,960 --> 00:04:13,950
Let me just do that again.

64
00:04:14,520 --> 00:04:18,209
And this is because we managed to get the first segment,

65
00:04:18,240 --> 00:04:20,190
the head of the snake, to change

66
00:04:20,190 --> 00:04:25,020
it's heading to 90 degrees and then the rest of the body will follow because

67
00:04:25,020 --> 00:04:28,110
it's continuously moving on every tick of the clock.

68
00:04:28,710 --> 00:04:32,550
So using this, we can now set the rest of the methods.

69
00:04:32,910 --> 00:04:36,660
But because we're going to need the head of the snake quite a few times in our

70
00:04:36,660 --> 00:04:41,610
code, it might make sense to actually create a separate attribute.

71
00:04:42,060 --> 00:04:46,980
So after we've created the snake, after we've got some segments inside this list,

72
00:04:47,310 --> 00:04:49,500
we can create an attribute called head,

73
00:04:49,590 --> 00:04:51,180
which is going to be the head of the snake.

74
00:04:51,540 --> 00:04:56,160
And this can be equal to self.segments at index zero. Now,

75
00:04:56,160 --> 00:04:59,430
remember if you have this line of code above the create_snake,

76
00:04:59,460 --> 00:05:03,990
it's probably going to error out because at this point the segments is empty

77
00:05:04,350 --> 00:05:08,160
and if you try to get the first item from it, it's not going to allow you to.

78
00:05:08,610 --> 00:05:13,440
So look at the positioning of the code here. Now, once we've got this set,

79
00:05:13,500 --> 00:05:16,500
we can go down here and change everywhere

80
00:05:16,500 --> 00:05:19,590
where we need to use the head of the snake. So for example,

81
00:05:19,830 --> 00:05:24,030
when we're moving the head forwards and when we're moving it up,

82
00:05:24,060 --> 00:05:28,080
left, down, and right. Now we can set

83
00:05:28,110 --> 00:05:33,110
each of these headings. Up is going to be a heading of 90, down is going to be 

84
00:05:34,560 --> 00:05:35,393
270,

85
00:05:36,000 --> 00:05:41,000
left is 180 and right is 0.

86
00:05:41,760 --> 00:05:45,420
These directions are just simply counter-clockwise.

87
00:05:45,450 --> 00:05:49,740
So the turtle starts out pointing towards East and that's a heading of zero,

88
00:05:50,070 --> 00:05:55,070
and then going anticlockwise, up is 90, left is 180, down as 270.

89
00:05:55,920 --> 00:06:00,020
This can be easily verified just by testing out some numbers, right?

90
00:06:00,020 --> 00:06:04,190
You could just put some numbers in there and then just try it out and see which

91
00:06:04,190 --> 00:06:07,130
direction it goes and then you'll be able to figure it out.

92
00:06:07,580 --> 00:06:10,040
But we've also mentioned this in previous lessons

93
00:06:10,040 --> 00:06:13,730
so you could also go back and take a look at when we looked at the heading of

94
00:06:13,760 --> 00:06:18,020
the turtle. So now if I go ahead and run this,

95
00:06:18,320 --> 00:06:23,240
then you should see that I can now freely control the snake by going left or

96
00:06:23,240 --> 00:06:28,100
right or down or up. And if you managed to do this,

97
00:06:28,160 --> 00:06:32,000
then you should consider yourself successful. Now,

98
00:06:32,030 --> 00:06:34,160
while I was testing this code,

99
00:06:34,400 --> 00:06:39,020
I realized that there's something about the snake game that we haven't really

100
00:06:39,380 --> 00:06:40,213
accounted for.

101
00:06:40,730 --> 00:06:44,390
And it's the fact that the snake can't move back on itself.

102
00:06:44,780 --> 00:06:49,310
It can't go forwards and then go the opposite direction because that requires

103
00:06:49,310 --> 00:06:52,040
the head to change directions.

104
00:06:52,280 --> 00:06:54,590
And this is not allowed in the official snake game.

105
00:06:55,070 --> 00:06:59,420
So how can we code this into our game? Well,

106
00:06:59,750 --> 00:07:04,750
we have to figure out when the head is pointing towards down direction,

107
00:07:06,980 --> 00:07:11,360
then we shouldn't allow it to go up. And similarly, when it's pointing up,

108
00:07:11,390 --> 00:07:14,000
we shouldn't let it go down, when it's pointing right

109
00:07:14,030 --> 00:07:17,150
we shouldn't let it go left. So at the top here,

110
00:07:17,180 --> 00:07:18,740
I'm going to create some constants.

111
00:07:18,770 --> 00:07:23,770
I'm going to create a UP direction and it's going to be 90, DOWN direction is going to

112
00:07:24,920 --> 00:07:29,920
be 270, LEFT is going to be equal to 180 and RIGHT is going to be equal to

113
00:07:33,230 --> 00:07:37,730
0. So now that I've got all of these as constants,

114
00:07:37,880 --> 00:07:42,880
I can go in here and set the heading to instead say UP when it needs to go up,

115
00:07:47,620 --> 00:07:51,640
DOWN, LEFT and RIGHT. In addition,

116
00:07:51,670 --> 00:07:53,530
we're going to do an if check.

117
00:07:53,560 --> 00:07:58,560
So we're going to say if the current self.head.heading

118
00:08:01,810 --> 00:08:06,190
is not equal to down, well, in that case,

119
00:08:06,250 --> 00:08:09,190
it's allowed to move up. So this way,

120
00:08:09,190 --> 00:08:12,700
it just means that if the current heading is pointed down,

121
00:08:13,150 --> 00:08:16,510
it can't move up. But for all other directions,

122
00:08:16,540 --> 00:08:18,970
if it's already moving up or left or right,

123
00:08:19,240 --> 00:08:21,280
then it can change the heading to up.

124
00:08:23,350 --> 00:08:27,430
Now be really careful here because it's not just self.head.heading,

125
00:08:27,700 --> 00:08:32,700
it's actually heading as a method because remember that the head of the snake is

126
00:08:34,150 --> 00:08:37,150
the first segment of our list of segments

127
00:08:37,630 --> 00:08:40,809
and each segment is a individual turtle.

128
00:08:41,409 --> 00:08:44,230
The turtle has a heading method

129
00:08:44,410 --> 00:08:48,820
which will give you a direction in terms of these 360-degree numbers,

130
00:08:49,270 --> 00:08:53,950
and then we can use that to check to see if it's equal to down.

131
00:08:54,370 --> 00:08:57,780
And if it is, then it's not allowed to go up.

132
00:08:58,590 --> 00:09:00,780
So using this logic,

133
00:09:00,810 --> 00:09:05,810
see if you can complete the rest of the three methods so that when you run your

134
00:09:06,990 --> 00:09:10,020
code, you can turn in all directions.

135
00:09:10,440 --> 00:09:12,660
But when you're facing one direction,

136
00:09:12,690 --> 00:09:15,750
you can't go backwards, like this.

137
00:09:16,520 --> 00:09:17,353
Okay?

138
00:09:20,480 --> 00:09:22,880
Alright. So it's going to be pretty straightforward.

139
00:09:23,240 --> 00:09:27,950
If it's already going up, then it's not allowed to go down.

140
00:09:28,640 --> 00:09:31,520
If it's already going right,

141
00:09:31,820 --> 00:09:35,990
then it's not allowed to go left. And finally,

142
00:09:36,050 --> 00:09:40,310
if it's already going left, then it's not allowed to go right.

143
00:09:41,810 --> 00:09:45,950
So this way, even though our snake can move freely through the space,

144
00:09:46,280 --> 00:09:49,880
it can't go back on itself. And there we have it.

145
00:09:49,910 --> 00:09:53,990
We've now managed to create our snake, get it to move and control it

146
00:09:54,080 --> 00:09:56,150
using our key presses.

147
00:09:56,690 --> 00:10:00,590
Even though it seems like our game is still not quite there,

148
00:10:00,860 --> 00:10:02,660
we've already come so far.

149
00:10:02,990 --> 00:10:05,930
So this comes up to our hour mark

150
00:10:06,290 --> 00:10:10,010
and it's now time to take a break, refresh your minds,

151
00:10:10,370 --> 00:10:13,850
look at some of the concepts that you might be confused about that we covered

152
00:10:13,850 --> 00:10:14,683
today,

153
00:10:14,720 --> 00:10:19,040
especially this concept of using the update to refresh the screen,

154
00:10:19,460 --> 00:10:21,680
as well as using the timer

155
00:10:21,710 --> 00:10:26,570
to delay the refresh so that we can control how often it happens.

156
00:10:26,960 --> 00:10:31,610
And then using our key bindings as well as separating all classes.

157
00:10:31,880 --> 00:10:34,910
We looked at a lot of things today. Now, tomorrow,

158
00:10:34,970 --> 00:10:37,370
once you're rested and fresh and ready,

159
00:10:37,640 --> 00:10:39,860
we're going to be finishing up the snake game.

160
00:10:40,310 --> 00:10:44,330
And we're going to be learning about inheritance as well as slicing

161
00:10:44,750 --> 00:10:48,350
and we're going to be building out the full functionality of this game.

162
00:10:48,680 --> 00:10:53,600
So I hope you're looking forward to it. I certainly am. I'll see you tomorrow.

