WEBVTT

0
00:00.330 --> 00:04.260
To start, we're going to need to create a new scoreboard file.

1
00:05.640 --> 00:07.170
And then inside this file

2
00:07.170 --> 00:12.170
we're going to add our usual imports and we're going to create the scoreboard

3
00:12.390 --> 00:13.223
class.

4
00:13.650 --> 00:17.400
Now the scoreboard class is going to inherit from the turtle class,

5
00:17.670 --> 00:22.670
so we can go ahead and add all of our inits and our super.init.

6
00:24.330 --> 00:26.700
Now let's define our scoreboard.

7
00:27.000 --> 00:31.590
It's going to have a white color so it shows up on the black background,

8
00:32.040 --> 00:35.670
it's going to have pen up because we don't want it to draw,

9
00:36.180 --> 00:41.180
and it's also going to have its turtle hidden because we're only interested in

10
00:41.310 --> 00:43.860
it being able to write something.

11
00:44.430 --> 00:47.910
And the thing that we want it to write is the current score.

12
00:48.270 --> 00:52.140
Let's go ahead and create some attributes for the scoreboard class.

13
00:52.500 --> 00:56.850
One is going to be the l_score which starts off at zero,

14
00:57.330 --> 01:01.200
and the other is going to be the r_score which starts off at zero.

15
01:01.650 --> 01:06.240
So our scoreboard is going to keep track of how the left player and the right

16
01:06.240 --> 01:07.470
player are doing.

17
01:08.640 --> 01:12.330
And then it's going to write this onto the screen.

18
01:12.810 --> 01:16.350
So we're going to write the self.l_score first,

19
01:16.920 --> 01:21.920
and we're going to change the alignment to center and also change the font to

20
01:23.070 --> 01:24.600
something quite large,

21
01:24.840 --> 01:29.840
like a courier font with 80 points and have a normal weight.

22
01:32.520 --> 01:35.970
Where do we want this to be written though? Because by default,

23
01:35.970 --> 01:39.990
this is going to be written at the center of the screen, (0, 0).

24
01:40.590 --> 01:41.430
But instead,

25
01:41.490 --> 01:46.490
what we want it to do is we probably want it to go to somewhere on the top-left

26
01:46.920 --> 01:49.350
of the screen. This is the left player score.

27
01:49.890 --> 01:52.320
So you can have a play around with the coordinates,

28
01:52.440 --> 01:56.610
but I found that -100 in the X-axis,

29
01:56.640 --> 02:01.080
so a little bit to the left of the center and then 200 on the Y-axis,

30
02:01.080 --> 02:06.080
so near the top of the screen seems to do well for this left-sided score.

31
02:06.720 --> 02:11.720
Now let's make this show up by going back to our main.py and importing our

32
02:12.210 --> 02:16.200
scoreboard class and creating a new scoreboard object.

33
02:17.850 --> 02:21.570
So when we hit run, you can see that big zero show up bright here.

34
02:22.590 --> 02:23.010
Now,

35
02:23.010 --> 02:27.870
the next thing we need to do is to show the right-sided player's score.

36
02:28.410 --> 02:33.410
We're going to make our scoreboard go to the opposite side of the screen,

37
02:34.800 --> 02:37.980
so that's going to be +100 and again

38
02:37.980 --> 02:40.230
with a Y value of 200.

39
02:40.980 --> 02:45.360
And then we're going to get it to write the right-sided score,

40
02:45.990 --> 02:50.730
so our score, and we're going to have the same alignment and the same font.

41
02:51.720 --> 02:56.250
Now you can see 0 0 when the game starts out. Now,

42
02:56.250 --> 03:00.100
what we actually want our scoreboard to do is the keep track of the score.

43
03:00.490 --> 03:05.110
So when the right paddle misses, it's a score to the left-sided player.

44
03:05.860 --> 03:08.530
We can define that in our main.py.

45
03:09.070 --> 03:11.350
When the right paddle misses,

46
03:11.530 --> 03:14.500
then we're going to get these scoreboard to

47
03:14.530 --> 03:17.500
give the left side a point.

48
03:17.920 --> 03:22.920
So maybe we could have a method called l_point in our scoreboard that increases

49
03:24.820 --> 03:27.460
the point in the left-sided player.

50
03:27.700 --> 03:32.530
So we can say self.l_score +=1.

51
03:33.400 --> 03:36.490
In addition, we're going to need to update our scoreboard,

52
03:36.880 --> 03:39.790
but all of that code is stuck in the init.

53
03:40.090 --> 03:44.740
So let's go ahead and move it out into a separate function

54
03:44.770 --> 03:47.110
which we'll call update_scoreboard. 

55
03:49.470 --> 03:52.290
And we can call self.update_scoreboard

56
03:52.530 --> 03:57.180
when we first initialized the scoreboard but also when we score a point.

57
03:59.280 --> 04:00.960
Now, when we run our code,

58
04:01.020 --> 04:05.400
you can see that when the right-sided misses, our left

59
04:05.400 --> 04:10.230
side gets a point. But at the moment, it's overwriting the previous score.

60
04:10.800 --> 04:12.030
So to prevent that,

61
04:12.060 --> 04:16.890
we have to add a self.clear before we update the scoreboard.

62
04:18.270 --> 04:23.010
And you can see now it erases everything that used to be there and it updates

63
04:23.010 --> 04:27.840
the score. So we've addressed when you are right paddle misses,

64
04:27.900 --> 04:31.290
but what about when our left paddle misses? Well,

65
04:31.290 --> 04:34.200
we should have a method called r_point as well

66
04:34.440 --> 04:36.720
that gives the right side a point.

67
04:38.070 --> 04:39.780
So let's add that over here.

68
04:42.690 --> 04:47.550
Increase the r_score by one and we'll again call

69
04:47.550 --> 04:48.600
update_scoreboard.

70
04:52.020 --> 04:52.380
<v 1>Right.</v>

71
04:52.380 --> 04:57.300
<v 0>Now when our left side misses, we get a point to the left,</v>

72
04:57.720 --> 05:00.900
and when our left side misses, we get a point to the right.

73
05:01.800 --> 05:05.220
That's pretty much all of pong. Now,

74
05:05.250 --> 05:08.910
if you want an extra challenge both in terms of playing pong

75
05:09.300 --> 05:10.530
as well as the code,

76
05:10.950 --> 05:15.950
see if you can figure out a way of getting the ball to increase its speed

77
05:16.320 --> 05:18.330
every time it hits a paddle,

78
05:18.780 --> 05:23.780
because that will make the game a lot more exciting instead of having it always

79
05:24.150 --> 05:27.660
move so slowly. If you can't figure it out,

80
05:27.900 --> 05:30.630
then continue watching and I'll show you the solution.

81
05:33.990 --> 05:36.720
So in order to speed up the ball,

82
05:37.170 --> 05:42.170
the key is in how much time we make our game loop sleep.

83
05:43.140 --> 05:46.440
Because the shorter that this sleep is,

84
05:46.470 --> 05:48.690
let's say instead of 0.1 second

85
05:48.720 --> 05:52.050
let's do 0.01 second.

86
05:53.010 --> 05:57.000
And if we run now, you can see our ball moves a lot faster.

87
05:57.560 --> 06:00.590
Let's make it even more extreme and add another zero.

88
06:02.750 --> 06:07.130
So how can we reduce this number by a little bit each time,

89
06:07.400 --> 06:09.650
but never make it go into the negative

90
06:09.710 --> 06:12.140
because if this becomes a negative number,

91
06:12.410 --> 06:17.300
then we actually get an error over here telling us that the sleep length must be

92
06:17.300 --> 06:20.240
non-negative. In order to do this,

93
06:20.300 --> 06:24.740
we have to figure out a way of making this number a little bit smaller each

94
06:24.740 --> 06:25.573
time.

95
06:25.910 --> 06:30.860
So let's make this number a attribute in our ball class.

96
06:31.460 --> 06:36.260
Now I don't want to call it speed because that might be a bit confusing because

97
06:36.350 --> 06:40.010
the turtle class already has a method called speed

98
06:40.340 --> 06:45.290
as you can see here. So instead I'm going to call it move_speed,

99
06:45.860 --> 06:47.810
and I'm going to set it to 0.1.

100
06:49.100 --> 06:53.510
So now inside our main.py, instead of having a hard-coded

101
06:53.510 --> 06:57.470
number I'm going to get hold of my ball.move_speed.

102
06:58.370 --> 07:03.170
Now, every single time our ball bounces in the X-axis,

103
07:03.500 --> 07:06.350
it means that has been touched by a paddle.

104
07:06.980 --> 07:11.690
So inside this method, in addition to getting it to reverse its direction,

105
07:12.110 --> 07:17.110
we can get the move_speed to reduce by multiplying it by a decimal number.

106
07:18.200 --> 07:22.940
So I've stuck with 0.9 because that seems to work for me.

107
07:23.240 --> 07:24.320
So for example,

108
07:24.350 --> 07:29.350
if we start out with a value of 5 and we multiply that by 0.9,

109
07:29.870 --> 07:31.190
it becomes 4.5.

110
07:31.550 --> 07:35.300
And if we multiply that by 0.9 becomes 4.05.

111
07:35.720 --> 07:39.140
So basically each time the ball bounces on a paddle,

112
07:39.410 --> 07:43.790
it's going to increase in speed. But this can't go on indefinitely.

113
07:44.060 --> 07:47.630
When we reset the game once one player has lost,

114
07:47.900 --> 07:52.580
we have to set this move_speed back to the original 0.1,

115
07:53.030 --> 07:57.020
just so that it doesn't keep on increasing in speed indefinitely.

116
07:57.740 --> 08:01.190
So now you can see whenever the ball hits a paddle,

117
08:02.510 --> 08:07.340
it will increase its speed ever so slightly making it just a little bit more

118
08:07.340 --> 08:09.080
challenging for the players.

119
08:10.130 --> 08:14.120
Now you can of course tweak around with that number so instead of 0.9,

120
08:14.120 --> 08:18.110
you could have 0.5, which basically haves the speed each time,

121
08:18.560 --> 08:22.790
but have a play around with that and see what you prefer. Now

122
08:22.790 --> 08:27.530
one last thing to mention before we finish off today is that when I'm testing

123
08:27.530 --> 08:29.690
this program on my computer,

124
08:30.080 --> 08:33.650
it seems like when I hold down the up hey or the down key,

125
08:33.680 --> 08:36.290
my paddle will continuously move.

126
08:36.740 --> 08:41.660
But there seems to be a bug with turtle where if I hold down the 'w' key,

127
08:41.990 --> 08:45.230
it can only move one bit at a time,

128
08:45.710 --> 08:48.380
but it doesn't seem to affect the 's' key.

129
08:49.070 --> 08:51.740
So I've been doing a bit of digging around online

130
08:51.800 --> 08:56.610
and I can't figure out exactly why this is. If you manage to figure it out,

131
08:56.640 --> 08:58.980
then feel free to add it to the Q/A so that

132
08:58.980 --> 09:03.870
other students can learn from it. But so far I've been coming up empty.

133
09:04.320 --> 09:05.580
But this is just a warning

134
09:05.580 --> 09:10.580
in case you find this behavior as well on your computer and you get confused and

135
09:10.620 --> 09:12.990
thinking it's something wrong with your code. It's not.

136
09:13.290 --> 09:17.520
It's something about the turtle module that causes it to behave like this.

137
09:17.850 --> 09:19.650
So don't worry if that's happening to you.

138
09:20.130 --> 09:22.080
And if you're playing pong with a friend,

139
09:22.290 --> 09:24.690
you can always trick them to use the left sided player.