1
00:00:00,450 --> 00:00:04,920
Now while our snake game works now with all of the functionality that we set out

2
00:00:04,920 --> 00:00:05,753
to create,

3
00:00:06,000 --> 00:00:11,000
there's just one thing that bothers me a little bit about this code, and it's how

4
00:00:11,370 --> 00:00:12,900
wordy this section is.

5
00:00:13,350 --> 00:00:18,350
All that we're trying to do is make sure that we're detecting the distance from

6
00:00:19,200 --> 00:00:23,130
all of the segments that we're looping through in snake.segments

7
00:00:23,340 --> 00:00:24,810
other than the first one.

8
00:00:25,350 --> 00:00:29,400
Surely there's an easier way of doing this rather than writing all of these

9
00:00:29,400 --> 00:00:34,080
lines of code. And indeed there is. But in order to use it,

10
00:00:34,290 --> 00:00:38,520
we have to learn about a Python concept known as slicing.

11
00:00:39,540 --> 00:00:44,540
Let's say that we had a list of piano keys all the way from a to g, and we wanted

12
00:00:46,050 --> 00:00:49,680
to get hold of a small section of this list.

13
00:00:50,130 --> 00:00:54,270
Let's say we wanted to slice the list so that we only get the c,

14
00:00:54,270 --> 00:00:58,620
d, and e keys from this list. How would we do that

15
00:00:58,620 --> 00:01:00,390
using Python? Well,

16
00:01:00,390 --> 00:01:04,830
one way of doing that is you could write a for loop and you could check, well,

17
00:01:04,830 --> 00:01:09,300
is it c, is a d, is it d. If so, then add it to a new list.

18
00:01:09,510 --> 00:01:12,690
But that's really convoluted. We have a much,

19
00:01:12,690 --> 00:01:16,890
much better way of doing that in Python. And it's known as slicing.

20
00:01:17,400 --> 00:01:20,940
This is what the syntax would look like. We would get hold of our lists,

21
00:01:21,000 --> 00:01:24,750
piano_keys, and then we would use a set of square brackets,

22
00:01:25,110 --> 00:01:28,980
but instead of accessing just one item with a position,

23
00:01:29,400 --> 00:01:34,400
we get a set of items by slicing it from position 2 to position 5.

24
00:01:36,360 --> 00:01:40,770
This might be a little bit confusing because we know that a is that position

25
00:01:40,770 --> 00:01:44,220
0, b is at 1, c is at 2,

26
00:01:44,640 --> 00:01:49,640
but the one at position 5 is actually f, so how does this get us c, d, and e?

27
00:01:52,590 --> 00:01:54,690
Well, you have to visualize it like this.

28
00:01:55,170 --> 00:02:00,170
Imagine that zero position is actually at the beginning of the list before where

29
00:02:00,870 --> 00:02:04,440
a starts. And at the end of the list,

30
00:02:04,470 --> 00:02:07,350
we're actually at the slicing position of 7.

31
00:02:08,160 --> 00:02:09,810
If we think about it this way,

32
00:02:09,870 --> 00:02:14,870
then the way that we're slicing this list is going all the way from position 2

33
00:02:15,990 --> 00:02:20,790
to position 5 then of course, we would end up with c, d, and e.

34
00:02:22,020 --> 00:02:24,240
If you take a look at this code in my Repl.it,

35
00:02:24,420 --> 00:02:27,000
I've essentially got the same code that we saw in the slides

36
00:02:27,360 --> 00:02:31,890
and we're slicing this list of piano keys from position 2 to 5.

37
00:02:32,370 --> 00:02:35,790
When I hit run, you will see that what gets printed is c,

38
00:02:35,790 --> 00:02:40,320
d, and e, exactly the slice that we wanted. Now,

39
00:02:40,350 --> 00:02:43,890
there's other things that you can do with slicing that's pretty cool. So,

40
00:02:43,890 --> 00:02:47,130
for example, if you wanted to slice from position 2,

41
00:02:47,160 --> 00:02:48,660
which remember is right here

42
00:02:48,750 --> 00:02:52,560
because we're starting at 0, 1, and 2,

43
00:02:52,890 --> 00:02:56,430
and you want to slice it all the way to the end of the list, well,

44
00:02:56,430 --> 00:03:00,820
you can just emit the second number. You can have two and then a colon.

45
00:03:01,330 --> 00:03:04,840
Now, when you slice this you get all of the rest of the list

46
00:03:05,050 --> 00:03:07,840
starting from the position that you specified.

47
00:03:08,500 --> 00:03:10,900
And this works in the opposite direction as well.

48
00:03:11,320 --> 00:03:15,790
Let's say that we want to get hold of everything up to position 5. Well,

49
00:03:15,790 --> 00:03:18,850
we could omit the first number, add a colon,

50
00:03:19,030 --> 00:03:22,510
and then after the colon specify the end slice position.

51
00:03:22,900 --> 00:03:27,700
And now we get all of the items in the list up to that position 5.

52
00:03:28,900 --> 00:03:32,830
In addition to just slicing between two numbers,

53
00:03:33,100 --> 00:03:37,630
we can actually specify a third number after another colon.

54
00:03:38,290 --> 00:03:42,790
And what this number does is it sets the increment. For example,

55
00:03:42,790 --> 00:03:46,780
we wanted the slice from a position 2 to position 5,

56
00:03:47,080 --> 00:03:50,800
but we want to only get every other item.

57
00:03:51,190 --> 00:03:54,580
So then it would give c, it would skip over the second one,

58
00:03:54,670 --> 00:03:59,050
and then it would give us e. And if I hit run, this is exactly what you see.

59
00:04:00,460 --> 00:04:04,000
So let's say I wanted to get hold of everything in this list

60
00:04:04,450 --> 00:04:09,400
but I wanted every second item. Well, then this is what my code would look like,

61
00:04:09,430 --> 00:04:13,900
go from the beginning to the end and then skip every second one.

62
00:04:14,590 --> 00:04:17,140
And I end up with a, c, e, and g. 

63
00:04:18,430 --> 00:04:21,250
Now to extend this a little bit further,

64
00:04:21,640 --> 00:04:26,640
we can actually use this neat trick to specify a -1 as the increment

65
00:04:27,430 --> 00:04:32,430
and what this does is it will actually reverse this list for us going from right

66
00:04:33,040 --> 00:04:37,300
to the end to the beginning. So essentially the increments are by one,

67
00:04:37,540 --> 00:04:41,710
but it's by -1, so that way we get the list starting from the end

68
00:04:41,950 --> 00:04:43,210
all the way back to the beginning.

69
00:04:43,630 --> 00:04:47,020
And this is a really neat trick that you'll see a lot of people use when it

70
00:04:47,020 --> 00:04:51,310
comes to manipulating lists and tuples. So speaking of tuples,

71
00:04:51,490 --> 00:04:55,810
this method of slicing also works for tuples.

72
00:04:56,320 --> 00:04:58,540
So let's say we had a piano tuple,

73
00:04:58,570 --> 00:05:03,570
which remember is defined by a set of parentheses with items inside separated by

74
00:05:04,120 --> 00:05:06,220
commas. Well,

75
00:05:06,220 --> 00:05:10,780
we can use the same method of slicing to work with our tuple.

76
00:05:11,140 --> 00:05:12,880
So we use our square brackets,

77
00:05:13,180 --> 00:05:17,230
let's get hold of the items from section 2 to 5.

78
00:05:17,530 --> 00:05:20,920
Then when we hit run, you'll see it gives us mi, fa, so.

79
00:05:20,920 --> 00:05:25,330
So coming back to our code, I have a challenge for you.

80
00:05:25,330 --> 00:05:30,330
Can you figure out how to use what we've just learned about slicing to

81
00:05:30,610 --> 00:05:33,850
change this code so that we get rid of this

82
00:05:33,850 --> 00:05:38,850
if statement where we're passing over the snake head so that we are in fact only

83
00:05:39,430 --> 00:05:44,430
checking that the snake head has a distance to every segment other than the first

84
00:05:45,040 --> 00:05:49,870
segment, less than 10? If you managed successfully to do this,

85
00:05:49,900 --> 00:05:51,340
you should be able to get rid of this

86
00:05:51,340 --> 00:05:56,340
if statement, change this to a single if statement and somehow figure out a way of

87
00:05:57,590 --> 00:06:02,590
making this code work using slicing. Pause the video and give this a go.

88
00:06:05,680 --> 00:06:06,130
Alright,

89
00:06:06,130 --> 00:06:11,130
so we know that we want to loop through every segment in snake.segments

90
00:06:11,530 --> 00:06:14,560
other than the first one. Well,

91
00:06:15,010 --> 00:06:20,010
all we need to do is simply slice this list of segments. So we can get hold of

92
00:06:21,640 --> 00:06:26,290
everything that starts off from position 1 to the end,

93
00:06:26,620 --> 00:06:29,350
by writing 1: nothing.

94
00:06:29,860 --> 00:06:34,860
This will give us everything inside the list other than the first item, like

95
00:06:35,740 --> 00:06:36,573
so.

96
00:06:37,150 --> 00:06:42,150
And then we can use that segment to loop through it and then check every segment

97
00:06:42,490 --> 00:06:46,840
in the tail against the distance to the head. And now when you run the code,

98
00:06:47,080 --> 00:06:50,440
you can see that it works just as perfectly as before,

99
00:06:51,400 --> 00:06:56,400
but we've now vastly simplified our code using the power of Python

100
00:06:56,860 --> 00:06:57,693
slicing.

101
00:06:58,600 --> 00:07:03,160
Have fun playing with your snake game and be sure to let me know your highest

102
00:07:03,160 --> 00:07:05,500
score because I can't seem to get past 10.

103
00:07:05,890 --> 00:07:09,280
And you've pretty much seen in this tutorial how bad I am at this game,

104
00:07:09,580 --> 00:07:13,450
but I'm sure you can do much better. And on top of that,

105
00:07:13,480 --> 00:07:17,200
think about ways that you might want to customize your snake game.

106
00:07:17,530 --> 00:07:21,370
Maybe you want to change the food to have a different color or a different

107
00:07:21,370 --> 00:07:25,210
shape, maybe your snake is actually going to eat turtles, I don't know.

108
00:07:25,420 --> 00:07:27,220
The world is your oyster.

109
00:07:27,430 --> 00:07:30,760
Now that you've learned about so many more concepts in Programming

110
00:07:31,000 --> 00:07:35,620
including inheritance in OOP as well as slicing lists and tuples.

111
00:07:36,490 --> 00:07:40,180
So I hope you've had fun building this game and I'll see you tomorrow.

