WEBVTT

0
00:00.660 --> 00:05.070
In the last lesson we created and managed to move a paddle up and down.

1
00:05.670 --> 00:08.970
Now in this lesson, we're going to figure out how we can create

2
00:08.970 --> 00:09.930
another paddle,

3
00:10.260 --> 00:14.100
but we don't want to repeat all the code that we've written so far.

4
00:14.340 --> 00:18.900
So we're probably going to need the help of a separate paddle class.

5
00:19.320 --> 00:21.270
So if you haven't already created

6
00:21.270 --> 00:24.690
your paddle inside a separate file and a separate class,

7
00:25.050 --> 00:29.100
then this is the time to refactor your code so that you can create 

8
00:29.100 --> 00:34.100
another paddle object from the puddle class easily and effortlessly.

9
00:34.830 --> 00:36.240
Here's what we're aiming for.

10
00:36.390 --> 00:41.390
We're aiming to get rid of all of this paddle related code and move it into a

11
00:41.640 --> 00:43.140
separate paddle class.

12
00:43.650 --> 00:48.650
And then subsequently we want to be able to create a right paddle simply by

13
00:50.250 --> 00:52.560
creating it from the paddle class.

14
00:52.950 --> 00:57.360
And then we can create a left paddle from the panel of class. Now because the

15
00:57.360 --> 01:01.380
right paddle has a different coordinate from the left paddle,

16
01:01.620 --> 01:06.620
we'll need to pass in the position of the right and left paddle as a tuple.

17
01:06.870 --> 01:11.870
So we might say something like the X will be 350 and the Y will be 0.

18
01:12.630 --> 01:13.980
And on the left paddle,

19
01:13.980 --> 01:18.980
the X will be -350 and the Y will be 0.

20
01:20.160 --> 01:25.160
So have a think about how you can make this code work and refactor this

21
01:26.250 --> 01:28.980
code into a separate paddle class

22
01:29.370 --> 01:33.330
and this paddle class should inherit from the turtle class.

23
01:33.780 --> 01:37.260
Overall, our program should work pretty much the same as before.

24
01:37.680 --> 01:41.010
The only difference being that we now have a left pedal

25
01:41.250 --> 01:45.510
which can move up and down with the w and s keys on the keyboard.

26
01:46.290 --> 01:48.810
Pause the video and try to complete this challenge.

27
01:51.450 --> 01:54.990
All right. So the first thing we're going to need is a new file,

28
01:55.260 --> 02:00.260
which is going to be called paddle.py and paddle.py is going to need the

29
02:01.410 --> 02:05.640
turtle class from the turtle module. So let's go ahead and import it.

30
02:06.180 --> 02:10.770
And then I'm going to create this Paddle class, and of course,

31
02:10.980 --> 02:13.590
all classes start off with a capital letter.

32
02:14.070 --> 02:19.070
Now, this class is going to take all of this paddle creation code

33
02:19.680 --> 02:23.550
and it's going to carry it out when we initialize a new paddle

34
02:23.550 --> 02:28.290
object. This paddle object, in order for it to be a turtle

35
02:28.290 --> 02:32.940
object, we're going to get it to inherit from the turtle class like this.

36
02:33.420 --> 02:35.880
And in order to get all of the abilities,

37
02:35.880 --> 02:38.610
the methods, and the attributes of the turtle class,

38
02:39.000 --> 02:41.940
we need to get this superclass to initialize itself.

39
02:42.660 --> 02:47.660
The next thing we're going to do is figure out how we can get our paddle to

40
02:47.910 --> 02:52.320
change its shape, change its color and all of these other things.

41
02:52.920 --> 02:57.920
So we can get rid of this line entirely because our paddle class is now the same

42
02:58.350 --> 03:01.870
as a turtle class. And then for the rest of these,

43
03:01.870 --> 03:04.960
instead of writing paddle.shape or paddle.color,

44
03:05.290 --> 03:09.460
we're going to hit command R or simply go to edit,

45
03:09.730 --> 03:12.280
find, and then replace,

46
03:12.460 --> 03:16.900
and you can take a look at the shortcut on your computer of how to get hold of

47
03:16.900 --> 03:20.380
this replace screen. So what I want to replace is

48
03:20.500 --> 03:23.890
everything that is paddled. here in my code,

49
03:24.280 --> 03:26.680
and I'm going to replace it with self.,

50
03:27.190 --> 03:29.620
because now we're inside the paddle class

51
03:30.010 --> 03:34.360
which is effectively the same as a turtle class with some added extras.

52
03:34.750 --> 03:39.750
We can tap into our methods and our attributes by simply using the self keyword.

53
03:41.950 --> 03:42.760
Now,

54
03:42.760 --> 03:47.760
the very last thing we need to do in order to make this work is to go ahead and

55
03:48.700 --> 03:51.910
import our paddle from the paddle.py.

56
03:54.700 --> 03:58.540
And once we've imported it, we need to address this warning.

57
03:58.810 --> 04:00.670
Notice how it's highlighting this area

58
04:00.700 --> 04:05.700
and it's telling us that this is an unexpected argument because the paddle

59
04:05.830 --> 04:09.700
class doesn't actually take any inputs when it's initialized.

60
04:09.910 --> 04:12.760
It's just got the self here. But in fact,

61
04:12.790 --> 04:16.510
we do need a position being passed over.

62
04:17.020 --> 04:21.910
So this position is going to determine where the paddle is going to go to,

63
04:22.420 --> 04:26.950
because remember the left paddle is going to have a different coordinate from

64
04:26.950 --> 04:27.880
the right paddle.

65
04:28.540 --> 04:33.190
So now that we fixed that, this is now completely valid code.

66
04:35.590 --> 04:38.200
And if we run our code as it is,

67
04:38.260 --> 04:43.180
you'll see our paddles being created and looking exactly the same way as before

68
04:43.450 --> 04:48.450
but now we've got one extra paddle. Going down here with our go up and go down

69
04:50.140 --> 04:50.973
methods,

70
04:51.040 --> 04:55.360
ideally, we would want them to live within our paddle class.

71
04:55.690 --> 04:58.390
So we want them to be methods inside this class.

72
04:58.930 --> 05:03.930
And remember that methods always have a first attribute as the self.

73
05:05.050 --> 05:07.030
Now, in addition, we have to, again,

74
05:07.060 --> 05:12.060
replace this paddle. with self. so that it's actually referring to the object

75
05:14.410 --> 05:19.360
that's created from this class to get its Y coordinate or get its X coordinate.

76
05:20.080 --> 05:22.480
Now, coming back to our main.py,

77
05:22.510 --> 05:26.800
we'll have to modify this code a little bit because this go_up and go_down

78
05:26.800 --> 05:28.090
function is now gone,

79
05:28.450 --> 05:33.450
and instead, we now have the r_paddle.go_up and r_paddle.go_down.

80
05:36.970 --> 05:40.840
So the right paddle is going to be controlled by the up and down keys,

81
05:41.260 --> 05:46.210
but the left paddle, we can get it to be controlled by some other keys. Now,

82
05:46.210 --> 05:46.810
in our case,

83
05:46.810 --> 05:51.810
I'm going to choose the 'w' to go up and the 's' key to go down.

84
05:52.780 --> 05:57.780
So we're going to need another set of these screen.onkey method calls.

85
05:58.340 --> 05:59.180
But in this case,

86
05:59.180 --> 06:04.180
this is going to get the left paddle to go up and the left paddle to go down. And

87
06:05.000 --> 06:07.460
to go up is controlled by the 'w' key

88
06:07.730 --> 06:12.560
and to go down is controlled by the 's' key. So now,

89
06:12.590 --> 06:17.210
if we run our code, you can see that when I moved w it goes up, when I move

90
06:17.210 --> 06:21.950
s it goes down, and up and down moves the right side of the paddle.

91
06:22.640 --> 06:23.690
So notice how

92
06:23.720 --> 06:28.720
now that we've refactored our paddle related code into a completely separate

93
06:29.090 --> 06:30.560
class, firstly,

94
06:30.650 --> 06:34.160
our main.py is now a lot simpler.

95
06:34.190 --> 06:37.730
It's clear of all of this paddle related code.

96
06:38.270 --> 06:41.720
And on top of that, if I wanted to create a third paddle

97
06:41.720 --> 06:45.260
let's say I wanted you to create a top paddle for some reason,

98
06:45.530 --> 06:50.530
then all I have to do is just call the paddle class and then pass in a tuple

99
06:51.200 --> 06:53.900
for the location of this paddle.

100
06:54.950 --> 06:59.810
So let's put it at, I dunno, 100 by 100.

101
07:00.380 --> 07:01.213
If I hit run,

102
07:01.220 --> 07:05.180
you can see there's a third random paddle and I didn't have to write

103
07:05.300 --> 07:10.300
any of this extra code or any of the related methods. We can create as many of

104
07:10.790 --> 07:15.410
these paddles as we want because we've now got this paddle class.

105
07:16.460 --> 07:17.960
So with these examples,

106
07:18.050 --> 07:22.370
I hope you're getting the sense of how important classes are and just how

107
07:22.370 --> 07:26.990
important they are when it comes to the development of a more complex project

108
07:27.230 --> 07:30.230
like this pong game. Now in the next lesson,

109
07:30.230 --> 07:34.190
we're going to continue building out our program and we're going to create our

110
07:34.200 --> 07:36.770
ball class and get the ball moving.