WEBVTT

0
00:00.450 --> 00:04.920
Now while our snake game works now with all of the functionality that we set out

1
00:04.920 --> 00:05.753
to create,

2
00:06.000 --> 00:11.000
there's just one thing that bothers me a little bit about this code, and it's how

3
00:11.370 --> 00:12.900
wordy this section is.

4
00:13.350 --> 00:18.350
All that we're trying to do is make sure that we're detecting the distance from

5
00:19.200 --> 00:23.130
all of the segments that we're looping through in snake.segments

6
00:23.340 --> 00:24.810
other than the first one.

7
00:25.350 --> 00:29.400
Surely there's an easier way of doing this rather than writing all of these

8
00:29.400 --> 00:34.080
lines of code. And indeed there is. But in order to use it,

9
00:34.290 --> 00:38.520
we have to learn about a Python concept known as slicing.

10
00:39.540 --> 00:44.540
Let's say that we had a list of piano keys all the way from a to g, and we wanted

11
00:46.050 --> 00:49.680
to get hold of a small section of this list.

12
00:50.130 --> 00:54.270
Let's say we wanted to slice the list so that we only get the c,

13
00:54.270 --> 00:58.620
d, and e keys from this list. How would we do that

14
00:58.620 --> 01:00.390
using Python? Well,

15
01:00.390 --> 01:04.830
one way of doing that is you could write a for loop and you could check, well,

16
01:04.830 --> 01:09.300
is it c, is a d, is it d. If so, then add it to a new list.

17
01:09.510 --> 01:12.690
But that's really convoluted. We have a much,

18
01:12.690 --> 01:16.890
much better way of doing that in Python. And it's known as slicing.

19
01:17.400 --> 01:20.940
This is what the syntax would look like. We would get hold of our lists,

20
01:21.000 --> 01:24.750
piano_keys, and then we would use a set of square brackets,

21
01:25.110 --> 01:28.980
but instead of accessing just one item with a position,

22
01:29.400 --> 01:34.400
we get a set of items by slicing it from position 2 to position 5.

23
01:36.360 --> 01:40.770
This might be a little bit confusing because we know that a is that position

24
01:40.770 --> 01:44.220
0, b is at 1, c is at 2,

25
01:44.640 --> 01:49.640
but the one at position 5 is actually f, so how does this get us c, d, and e?

26
01:52.590 --> 01:54.690
Well, you have to visualize it like this.

27
01:55.170 --> 02:00.170
Imagine that zero position is actually at the beginning of the list before where

28
02:00.870 --> 02:04.440
a starts. And at the end of the list,

29
02:04.470 --> 02:07.350
we're actually at the slicing position of 7.

30
02:08.160 --> 02:09.810
If we think about it this way,

31
02:09.870 --> 02:14.870
then the way that we're slicing this list is going all the way from position 2

32
02:15.990 --> 02:20.790
to position 5 then of course, we would end up with c, d, and e.

33
02:22.020 --> 02:24.240
If you take a look at this code in my Repl.it,

34
02:24.420 --> 02:27.000
I've essentially got the same code that we saw in the slides

35
02:27.360 --> 02:31.890
and we're slicing this list of piano keys from position 2 to 5.

36
02:32.370 --> 02:35.790
When I hit run, you will see that what gets printed is c,

37
02:35.790 --> 02:40.320
d, and e, exactly the slice that we wanted. Now,

38
02:40.350 --> 02:43.890
there's other things that you can do with slicing that's pretty cool. So,

39
02:43.890 --> 02:47.130
for example, if you wanted to slice from position 2,

40
02:47.160 --> 02:48.660
which remember is right here

41
02:48.750 --> 02:52.560
because we're starting at 0, 1, and 2,

42
02:52.890 --> 02:56.430
and you want to slice it all the way to the end of the list, well,

43
02:56.430 --> 03:00.820
you can just emit the second number. You can have two and then a colon.

44
03:01.330 --> 03:04.840
Now, when you slice this you get all of the rest of the list

45
03:05.050 --> 03:07.840
starting from the position that you specified.

46
03:08.500 --> 03:10.900
And this works in the opposite direction as well.

47
03:11.320 --> 03:15.790
Let's say that we want to get hold of everything up to position 5. Well,

48
03:15.790 --> 03:18.850
we could omit the first number, add a colon,

49
03:19.030 --> 03:22.510
and then after the colon specify the end slice position.

50
03:22.900 --> 03:27.700
And now we get all of the items in the list up to that position 5.

51
03:28.900 --> 03:32.830
In addition to just slicing between two numbers,

52
03:33.100 --> 03:37.630
we can actually specify a third number after another colon.

53
03:38.290 --> 03:42.790
And what this number does is it sets the increment. For example,

54
03:42.790 --> 03:46.780
we wanted the slice from a position 2 to position 5,

55
03:47.080 --> 03:50.800
but we want to only get every other item.

56
03:51.190 --> 03:54.580
So then it would give c, it would skip over the second one,

57
03:54.670 --> 03:59.050
and then it would give us e. And if I hit run, this is exactly what you see.

58
04:00.460 --> 04:04.000
So let's say I wanted to get hold of everything in this list

59
04:04.450 --> 04:09.400
but I wanted every second item. Well, then this is what my code would look like,

60
04:09.430 --> 04:13.900
go from the beginning to the end and then skip every second one.

61
04:14.590 --> 04:17.140
And I end up with a, c, e, and g. 

62
04:18.430 --> 04:21.250
Now to extend this a little bit further,

63
04:21.640 --> 04:26.640
we can actually use this neat trick to specify a -1 as the increment

64
04:27.430 --> 04:32.430
and what this does is it will actually reverse this list for us going from right

65
04:33.040 --> 04:37.300
to the end to the beginning. So essentially the increments are by one,

66
04:37.540 --> 04:41.710
but it's by -1, so that way we get the list starting from the end

67
04:41.950 --> 04:43.210
all the way back to the beginning.

68
04:43.630 --> 04:47.020
And this is a really neat trick that you'll see a lot of people use when it

69
04:47.020 --> 04:51.310
comes to manipulating lists and tuples. So speaking of tuples,

70
04:51.490 --> 04:55.810
this method of slicing also works for tuples.

71
04:56.320 --> 04:58.540
So let's say we had a piano tuple,

72
04:58.570 --> 05:03.570
which remember is defined by a set of parentheses with items inside separated by

73
05:04.120 --> 05:06.220
commas. Well,

74
05:06.220 --> 05:10.780
we can use the same method of slicing to work with our tuple.

75
05:11.140 --> 05:12.880
So we use our square brackets,

76
05:13.180 --> 05:17.230
let's get hold of the items from section 2 to 5.

77
05:17.530 --> 05:20.920
Then when we hit run, you'll see it gives us mi, fa, so.

78
05:20.920 --> 05:25.330
So coming back to our code, I have a challenge for you.

79
05:25.330 --> 05:30.330
Can you figure out how to use what we've just learned about slicing to

80
05:30.610 --> 05:33.850
change this code so that we get rid of this

81
05:33.850 --> 05:38.850
if statement where we're passing over the snake head so that we are in fact only

82
05:39.430 --> 05:44.430
checking that the snake head has a distance to every segment other than the first

83
05:45.040 --> 05:49.870
segment, less than 10? If you managed successfully to do this,

84
05:49.900 --> 05:51.340
you should be able to get rid of this

85
05:51.340 --> 05:56.340
if statement, change this to a single if statement and somehow figure out a way of

86
05:57.590 --> 06:02.590
making this code work using slicing. Pause the video and give this a go.

87
06:05.680 --> 06:06.130
Alright,

88
06:06.130 --> 06:11.130
so we know that we want to loop through every segment in snake.segments

89
06:11.530 --> 06:14.560
other than the first one. Well,

90
06:15.010 --> 06:20.010
all we need to do is simply slice this list of segments. So we can get hold of

91
06:21.640 --> 06:26.290
everything that starts off from position 1 to the end,

92
06:26.620 --> 06:29.350
by writing 1: nothing.

93
06:29.860 --> 06:34.860
This will give us everything inside the list other than the first item, like

94
06:35.740 --> 06:36.573
so.

95
06:37.150 --> 06:42.150
And then we can use that segment to loop through it and then check every segment

96
06:42.490 --> 06:46.840
in the tail against the distance to the head. And now when you run the code,

97
06:47.080 --> 06:50.440
you can see that it works just as perfectly as before,

98
06:51.400 --> 06:56.400
but we've now vastly simplified our code using the power of Python

99
06:56.860 --> 06:57.693
slicing.

100
06:58.600 --> 07:03.160
Have fun playing with your snake game and be sure to let me know your highest

101
07:03.160 --> 07:05.500
score because I can't seem to get past 10.

102
07:05.890 --> 07:09.280
And you've pretty much seen in this tutorial how bad I am at this game,

103
07:09.580 --> 07:13.450
but I'm sure you can do much better. And on top of that,

104
07:13.480 --> 07:17.200
think about ways that you might want to customize your snake game.

105
07:17.530 --> 07:21.370
Maybe you want to change the food to have a different color or a different

106
07:21.370 --> 07:25.210
shape, maybe your snake is actually going to eat turtles, I don't know.

107
07:25.420 --> 07:27.220
The world is your oyster.

108
07:27.430 --> 07:30.760
Now that you've learned about so many more concepts in Programming

109
07:31.000 --> 07:35.620
including inheritance in OOP as well as slicing lists and tuples.

110
07:36.490 --> 07:40.180
So I hope you've had fun building this game and I'll see you tomorrow.