1
00:00:00,660 --> 00:00:05,070
In the last lesson we created and managed to move a paddle up and down.

2
00:00:05,670 --> 00:00:08,970
Now in this lesson, we're going to figure out how we can create

3
00:00:08,970 --> 00:00:09,930
another paddle,

4
00:00:10,260 --> 00:00:14,100
but we don't want to repeat all the code that we've written so far.

5
00:00:14,340 --> 00:00:18,900
So we're probably going to need the help of a separate paddle class.

6
00:00:19,320 --> 00:00:21,270
So if you haven't already created

7
00:00:21,270 --> 00:00:24,690
your paddle inside a separate file and a separate class,

8
00:00:25,050 --> 00:00:29,100
then this is the time to refactor your code so that you can create 

9
00:00:29,100 --> 00:00:34,100
another paddle object from the puddle class easily and effortlessly.

10
00:00:34,830 --> 00:00:36,240
Here's what we're aiming for.

11
00:00:36,390 --> 00:00:41,390
We're aiming to get rid of all of this paddle related code and move it into a

12
00:00:41,640 --> 00:00:43,140
separate paddle class.

13
00:00:43,650 --> 00:00:48,650
And then subsequently we want to be able to create a right paddle simply by

14
00:00:50,250 --> 00:00:52,560
creating it from the paddle class.

15
00:00:52,950 --> 00:00:57,360
And then we can create a left paddle from the panel of class. Now because the

16
00:00:57,360 --> 00:01:01,380
right paddle has a different coordinate from the left paddle,

17
00:01:01,620 --> 00:01:06,620
we'll need to pass in the position of the right and left paddle as a tuple.

18
00:01:06,870 --> 00:01:11,870
So we might say something like the X will be 350 and the Y will be 0.

19
00:01:12,630 --> 00:01:13,980
And on the left paddle,

20
00:01:13,980 --> 00:01:18,980
the X will be -350 and the Y will be 0.

21
00:01:20,160 --> 00:01:25,160
So have a think about how you can make this code work and refactor this

22
00:01:26,250 --> 00:01:28,980
code into a separate paddle class

23
00:01:29,370 --> 00:01:33,330
and this paddle class should inherit from the turtle class.

24
00:01:33,780 --> 00:01:37,260
Overall, our program should work pretty much the same as before.

25
00:01:37,680 --> 00:01:41,010
The only difference being that we now have a left pedal

26
00:01:41,250 --> 00:01:45,510
which can move up and down with the w and s keys on the keyboard.

27
00:01:46,290 --> 00:01:48,810
Pause the video and try to complete this challenge.

28
00:01:51,450 --> 00:01:54,990
All right. So the first thing we're going to need is a new file,

29
00:01:55,260 --> 00:02:00,260
which is going to be called paddle.py and paddle.py is going to need the

30
00:02:01,410 --> 00:02:05,640
turtle class from the turtle module. So let's go ahead and import it.

31
00:02:06,180 --> 00:02:10,770
And then I'm going to create this Paddle class, and of course,

32
00:02:10,979 --> 00:02:13,590
all classes start off with a capital letter.

33
00:02:14,070 --> 00:02:19,070
Now, this class is going to take all of this paddle creation code

34
00:02:19,680 --> 00:02:23,550
and it's going to carry it out when we initialize a new paddle

35
00:02:23,550 --> 00:02:28,290
object. This paddle object, in order for it to be a turtle

36
00:02:28,290 --> 00:02:32,940
object, we're going to get it to inherit from the turtle class like this.

37
00:02:33,420 --> 00:02:35,880
And in order to get all of the abilities,

38
00:02:35,880 --> 00:02:38,610
the methods, and the attributes of the turtle class,

39
00:02:39,000 --> 00:02:41,940
we need to get this superclass to initialize itself.

40
00:02:42,660 --> 00:02:47,660
The next thing we're going to do is figure out how we can get our paddle to

41
00:02:47,910 --> 00:02:52,320
change its shape, change its color and all of these other things.

42
00:02:52,920 --> 00:02:57,920
So we can get rid of this line entirely because our paddle class is now the same

43
00:02:58,350 --> 00:03:01,870
as a turtle class. And then for the rest of these,

44
00:03:01,870 --> 00:03:04,960
instead of writing paddle.shape or paddle.color,

45
00:03:05,290 --> 00:03:09,460
we're going to hit command R or simply go to edit,

46
00:03:09,730 --> 00:03:12,280
find, and then replace,

47
00:03:12,460 --> 00:03:16,900
and you can take a look at the shortcut on your computer of how to get hold of

48
00:03:16,900 --> 00:03:20,380
this replace screen. So what I want to replace is

49
00:03:20,500 --> 00:03:23,890
everything that is paddled. here in my code,

50
00:03:24,280 --> 00:03:26,680
and I'm going to replace it with self.,

51
00:03:27,190 --> 00:03:29,620
because now we're inside the paddle class

52
00:03:30,010 --> 00:03:34,360
which is effectively the same as a turtle class with some added extras.

53
00:03:34,750 --> 00:03:39,750
We can tap into our methods and our attributes by simply using the self keyword.

54
00:03:41,950 --> 00:03:42,760
Now,

55
00:03:42,760 --> 00:03:47,760
the very last thing we need to do in order to make this work is to go ahead and

56
00:03:48,700 --> 00:03:51,910
import our paddle from the paddle.py.

57
00:03:54,700 --> 00:03:58,540
And once we've imported it, we need to address this warning.

58
00:03:58,810 --> 00:04:00,670
Notice how it's highlighting this area

59
00:04:00,700 --> 00:04:05,700
and it's telling us that this is an unexpected argument because the paddle

60
00:04:05,830 --> 00:04:09,700
class doesn't actually take any inputs when it's initialized.

61
00:04:09,910 --> 00:04:12,760
It's just got the self here. But in fact,

62
00:04:12,790 --> 00:04:16,510
we do need a position being passed over.

63
00:04:17,019 --> 00:04:21,910
So this position is going to determine where the paddle is going to go to,

64
00:04:22,420 --> 00:04:26,950
because remember the left paddle is going to have a different coordinate from

65
00:04:26,950 --> 00:04:27,880
the right paddle.

66
00:04:28,540 --> 00:04:33,190
So now that we fixed that, this is now completely valid code.

67
00:04:35,590 --> 00:04:38,200
And if we run our code as it is,

68
00:04:38,260 --> 00:04:43,180
you'll see our paddles being created and looking exactly the same way as before

69
00:04:43,450 --> 00:04:48,450
but now we've got one extra paddle. Going down here with our go up and go down

70
00:04:50,140 --> 00:04:50,973
methods,

71
00:04:51,040 --> 00:04:55,360
ideally, we would want them to live within our paddle class.

72
00:04:55,690 --> 00:04:58,390
So we want them to be methods inside this class.

73
00:04:58,930 --> 00:05:03,930
And remember that methods always have a first attribute as the self.

74
00:05:05,050 --> 00:05:07,030
Now, in addition, we have to, again,

75
00:05:07,060 --> 00:05:12,060
replace this paddle. with self. so that it's actually referring to the object

76
00:05:14,410 --> 00:05:19,360
that's created from this class to get its Y coordinate or get its X coordinate.

77
00:05:20,080 --> 00:05:22,480
Now, coming back to our main.py,

78
00:05:22,510 --> 00:05:26,800
we'll have to modify this code a little bit because this go_up and go_down

79
00:05:26,800 --> 00:05:28,090
function is now gone,

80
00:05:28,450 --> 00:05:33,450
and instead, we now have the r_paddle.go_up and r_paddle.go_down.

81
00:05:36,970 --> 00:05:40,840
So the right paddle is going to be controlled by the up and down keys,

82
00:05:41,260 --> 00:05:46,210
but the left paddle, we can get it to be controlled by some other keys. Now,

83
00:05:46,210 --> 00:05:46,810
in our case,

84
00:05:46,810 --> 00:05:51,810
I'm going to choose the 'w' to go up and the 's' key to go down.

85
00:05:52,780 --> 00:05:57,780
So we're going to need another set of these screen.onkey method calls.

86
00:05:58,340 --> 00:05:59,180
But in this case,

87
00:05:59,180 --> 00:06:04,180
this is going to get the left paddle to go up and the left paddle to go down. And

88
00:06:05,000 --> 00:06:07,460
to go up is controlled by the 'w' key

89
00:06:07,730 --> 00:06:12,560
and to go down is controlled by the 's' key. So now,

90
00:06:12,590 --> 00:06:17,210
if we run our code, you can see that when I moved w it goes up, when I move

91
00:06:17,210 --> 00:06:21,950
s it goes down, and up and down moves the right side of the paddle.

92
00:06:22,640 --> 00:06:23,690
So notice how

93
00:06:23,720 --> 00:06:28,720
now that we've refactored our paddle related code into a completely separate

94
00:06:29,090 --> 00:06:30,560
class, firstly,

95
00:06:30,650 --> 00:06:34,160
our main.py is now a lot simpler.

96
00:06:34,190 --> 00:06:37,730
It's clear of all of this paddle related code.

97
00:06:38,270 --> 00:06:41,720
And on top of that, if I wanted to create a third paddle

98
00:06:41,720 --> 00:06:45,260
let's say I wanted you to create a top paddle for some reason,

99
00:06:45,530 --> 00:06:50,530
then all I have to do is just call the paddle class and then pass in a tuple

100
00:06:51,200 --> 00:06:53,900
for the location of this paddle.

101
00:06:54,950 --> 00:06:59,810
So let's put it at, I dunno, 100 by 100.

102
00:07:00,380 --> 00:07:01,213
If I hit run,

103
00:07:01,220 --> 00:07:05,180
you can see there's a third random paddle and I didn't have to write

104
00:07:05,300 --> 00:07:10,300
any of this extra code or any of the related methods. We can create as many of

105
00:07:10,790 --> 00:07:15,410
these paddles as we want because we've now got this paddle class.

106
00:07:16,460 --> 00:07:17,960
So with these examples,

107
00:07:18,050 --> 00:07:22,370
I hope you're getting the sense of how important classes are and just how

108
00:07:22,370 --> 00:07:26,990
important they are when it comes to the development of a more complex project

109
00:07:27,230 --> 00:07:30,230
like this pong game. Now in the next lesson,

110
00:07:30,230 --> 00:07:34,190
we're going to continue building out our program and we're going to create our

111
00:07:34,200 --> 00:07:36,770
ball class and get the ball moving.

