WEBVTT

0
00:00.150 --> 00:00.750
So far,

1
00:00.750 --> 00:05.750
we've managed to get our starting segments for our snake to appear on screen.

2
00:06.630 --> 00:11.630
And then we managed to get our snake to automatically move forwards and to

3
00:12.930 --> 00:17.930
figure out a way for the tail of the snake to follow where the head is going.

4
00:18.750 --> 00:23.750
Now it's time to tidy up our code a little bit so that all the parts that are

5
00:24.390 --> 00:29.390
related to the snake's behavior and the snake's appearance go into a separate

6
00:30.090 --> 00:33.300
class. So that by the end of the whole project,

7
00:33.330 --> 00:37.410
we should end up with three classes; a snake class, a food class,

8
00:37.500 --> 00:41.130
and a scoreboard. And all of these classes will be in separate files,

9
00:41.370 --> 00:43.170
managing only one thing.

10
00:43.650 --> 00:48.150
And the goal of the refactoring is so that you could create a separate file,

11
00:48.150 --> 00:52.860
snake.py, import the snake class that you've created in that file.

12
00:53.430 --> 00:55.500
And then once you initialize this snake,

13
00:55.560 --> 01:00.330
it will do everything that we have so far and create our three segments snake

14
01:00.600 --> 01:04.890
onto the screen. And then finally, while the game loop is running,

15
01:05.130 --> 01:10.110
we're going to get the snake to continuously move forward just by calling snake

16
01:10.140 --> 01:13.140
.move. This is what you're aiming for.

17
01:14.310 --> 01:17.910
Have a think about how you can take the existing code that you've got in the

18
01:17.910 --> 01:22.910
main.py and extract all of the snake related functionality into a separate

19
01:23.610 --> 01:28.230
snake class and make your main.py end up looking like this.

20
01:28.740 --> 01:31.860
The program should do the exact same things as before.

21
01:32.160 --> 01:36.450
The only difference is how the code is organized. Have a think about this.

22
01:36.660 --> 01:39.810
You might need to revise some of the things you learned about how to create

23
01:39.810 --> 01:43.860
classes, how to create objects from the previous lessons.

24
01:44.010 --> 01:45.060
But pause the video,

25
01:45.330 --> 01:49.230
I want you to give this a good try before you come back when we'll go through

26
01:49.230 --> 01:50.250
the solution together.

27
01:53.960 --> 01:54.380
<v 1>Alright.</v>

28
01:54.380 --> 01:57.020
<v 0>So the first thing we need to do is of course,</v>

29
01:57.050 --> 02:02.050
to create our snake class. And remember that in Python, class names are in Pascal

30
02:03.350 --> 02:06.470
case, so the first letter needs to be capitalized.

31
02:07.070 --> 02:10.280
And then inside this class, I'm going to create my init.

32
02:11.000 --> 02:15.590
Now the code in here is going to determine what should happen when we initialize

33
02:15.620 --> 02:19.250
a new snake object. So firstly,

34
02:19.310 --> 02:24.310
I'm going to take the starting positions and take it over to our snake.py.

35
02:25.460 --> 02:29.960
Now, I'm actually going to put this in as a constant. So right at the top,

36
02:30.350 --> 02:32.150
and remember that in Python,

37
02:32.480 --> 02:36.380
the constants are named with all caps like this,

38
02:36.530 --> 02:40.550
and also with snake case with underscores separating each of the words.

39
02:41.000 --> 02:46.000
Now we can use these starting positions inside our snake class to create our new

40
02:47.430 --> 02:51.410
snake. So we're going to need to move our segments over as well.

41
02:51.830 --> 02:55.850
So let's create a new attribute that's associated with our snake class called

42
02:55.850 --> 02:57.650
segments. And remember,

43
02:57.650 --> 03:01.120
we're going to need to use self when we're working within a class.

44
03:02.020 --> 03:05.290
After that, we're going to create our snake.

45
03:05.620 --> 03:10.210
So I'm going to create a method called create_snake down here.

46
03:13.350 --> 03:18.350
And this method is going to do everything that we previously had over here,

47
03:19.680 --> 03:22.320
but we're going to move that into this method.

48
03:22.740 --> 03:25.230
Now there's a couple of things we need to change. Firstly,

49
03:25.230 --> 03:28.260
we change these starting positions into a constant,

50
03:28.560 --> 03:30.270
so let's make that consistent.

51
03:30.750 --> 03:35.700
Second thing is that we need to import turtle into this file from the turtle

52
03:35.700 --> 03:39.720
module in order to use it to create a new segment.

53
03:40.350 --> 03:44.640
And then finally, in order to refer to our attribute segments,

54
03:44.730 --> 03:49.730
we have to say self.segments and then append this new segment to our snake

55
03:51.030 --> 03:53.610
segments. That's this part done.

56
03:53.910 --> 03:58.910
The next thing we need to do is to get rid of this code and move it inside the

57
04:00.480 --> 04:02.010
snake. So again,

58
04:02.040 --> 04:06.780
I'm going to create another method that's associated with the snake class called

59
04:06.810 --> 04:09.600
move. So when the snake moves,

60
04:09.660 --> 04:14.660
it's going to look to this code to figure out how it should do that. Again,

61
04:15.090 --> 04:18.510
because we're inside our class, it's no longer just segments.

62
04:18.780 --> 04:21.030
It's now self.segments.

63
04:21.510 --> 04:25.500
So let's go ahead and replace it in all the places where we have our red

64
04:25.530 --> 04:29.220
underlines and now that's pretty much it for our snake class.

65
04:29.520 --> 04:33.780
We can create a new snake object and each time we do that,

66
04:33.930 --> 04:38.930
it creates a three-segment snake using the starting positions

67
04:39.600 --> 04:44.490
which are declared at the top. So if we want the snake to show up on screen,

68
04:44.760 --> 04:49.260
then we're going to have to first import the snake class from the snake file,

69
04:49.800 --> 04:54.800
and then we're going to create a new snake object from that class.

70
04:55.830 --> 05:00.060
And once this line gets triggered, then we're going to be calling

71
05:00.060 --> 05:04.800
create_snake and our three-segment snake will show up on screen like this.

72
05:05.610 --> 05:08.490
Now the next step is while the game is on,

73
05:08.670 --> 05:12.780
the screen is going to update every 0.1 second.

74
05:13.080 --> 05:15.000
So that's essentially what this is doing.

75
05:15.030 --> 05:19.620
It's saying that delay for 0.1 second and then refresh the screen.

76
05:20.250 --> 05:22.530
And every time the screen refreshes,

77
05:22.770 --> 05:26.640
we're going to get the snake to move forwards by one step.

78
05:27.120 --> 05:31.410
Now at the moment, each step is defined here as 20.

79
05:31.830 --> 05:36.270
So again, I want to extract that into a constant up here,

80
05:36.630 --> 05:39.450
so we're going to call it the MOVE_DISTANCE

81
05:40.950 --> 05:45.950
and we can set that to 20 and then change this to use the constant here.

82
05:47.430 --> 05:51.600
And the reason why we have all these constants is so that if we wanted to tweak

83
05:51.600 --> 05:52.240
our game,

84
05:52.240 --> 05:56.940
say if we wanted the snake to start at a different position or for it to move

85
05:56.990 --> 06:01.250
further each time, then we don't have to dig through the body of our code.

86
06:01.430 --> 06:03.290
All we have to do is look at the top,

87
06:03.320 --> 06:06.680
look at all the things that we can change and then change it accordingly.

88
06:07.160 --> 06:10.100
So now coming back to our code here,

89
06:10.490 --> 06:15.490
all we now really have here is just these few lines of code and everything that

90
06:15.830 --> 06:17.360
is snake related,

91
06:17.390 --> 06:22.390
creating the snake or moving the snake is now abstracted into it's own class.

92
06:24.350 --> 06:26.690
This way when something goes wrong with our snake,

93
06:26.780 --> 06:30.470
we know who's responsible and which file to dig through

94
06:30.470 --> 06:33.380
to figure out the reason. Now, at this point,

95
06:33.380 --> 06:35.900
if we go ahead and rerun our code,

96
06:36.260 --> 06:39.710
you can see that our program works exactly the same as before.

97
06:39.740 --> 06:43.820
Nothing should have changed and nothing should have broken ow because our game

98
06:43.820 --> 06:46.550
isn't yet able to go into the game over sequence,

99
06:46.760 --> 06:50.150
there's no way of stopping the snake other than hitting the stop button.

100
06:50.450 --> 06:53.420
So don't worry if at this stage inside your console

101
06:53.450 --> 06:57.020
you see a lot of red and it says things like keyboard interrupt,

102
06:57.020 --> 07:00.050
or trace back errors. All of these are normal.

103
07:00.380 --> 07:04.520
The thing that you're more concerned about is whether if, when you run the game,

104
07:04.700 --> 07:07.430
whether if it actually works like it used to,

105
07:07.460 --> 07:10.220
so namely your snake moving across the screen.

106
07:10.760 --> 07:15.760
If it does exactly what it used to and your main.py is now much simpler and

107
07:17.180 --> 07:22.040
all of your snake functionality and behavior and appearance is now inside its

108
07:22.040 --> 07:26.060
own class, then you have successfully achieved the goal of this lesson.