1
00:00:00,330 --> 00:00:04,260
To start, we're going to need to create a new scoreboard file.

2
00:00:05,640 --> 00:00:07,170
And then inside this file

3
00:00:07,170 --> 00:00:12,170
we're going to add our usual imports and we're going to create the scoreboard

4
00:00:12,390 --> 00:00:13,223
class.

5
00:00:13,650 --> 00:00:17,400
Now the scoreboard class is going to inherit from the turtle class,

6
00:00:17,670 --> 00:00:22,670
so we can go ahead and add all of our inits and our super.init.

7
00:00:24,330 --> 00:00:26,700
Now let's define our scoreboard.

8
00:00:27,000 --> 00:00:31,590
It's going to have a white color so it shows up on the black background,

9
00:00:32,040 --> 00:00:35,670
it's going to have pen up because we don't want it to draw,

10
00:00:36,180 --> 00:00:41,180
and it's also going to have its turtle hidden because we're only interested in

11
00:00:41,310 --> 00:00:43,860
it being able to write something.

12
00:00:44,430 --> 00:00:47,910
And the thing that we want it to write is the current score.

13
00:00:48,270 --> 00:00:52,140
Let's go ahead and create some attributes for the scoreboard class.

14
00:00:52,500 --> 00:00:56,850
One is going to be the l_score which starts off at zero,

15
00:00:57,330 --> 00:01:01,200
and the other is going to be the r_score which starts off at zero.

16
00:01:01,650 --> 00:01:06,240
So our scoreboard is going to keep track of how the left player and the right

17
00:01:06,240 --> 00:01:07,470
player are doing.

18
00:01:08,640 --> 00:01:12,330
And then it's going to write this onto the screen.

19
00:01:12,810 --> 00:01:16,350
So we're going to write the self.l_score first,

20
00:01:16,920 --> 00:01:21,920
and we're going to change the alignment to center and also change the font to

21
00:01:23,070 --> 00:01:24,600
something quite large,

22
00:01:24,840 --> 00:01:29,840
like a courier font with 80 points and have a normal weight.

23
00:01:32,520 --> 00:01:35,970
Where do we want this to be written though? Because by default,

24
00:01:35,970 --> 00:01:39,990
this is going to be written at the center of the screen, (0, 0).

25
00:01:40,590 --> 00:01:41,430
But instead,

26
00:01:41,490 --> 00:01:46,490
what we want it to do is we probably want it to go to somewhere on the top-left

27
00:01:46,920 --> 00:01:49,350
of the screen. This is the left player score.

28
00:01:49,890 --> 00:01:52,320
So you can have a play around with the coordinates,

29
00:01:52,440 --> 00:01:56,610
but I found that -100 in the X-axis,

30
00:01:56,640 --> 00:02:01,080
so a little bit to the left of the center and then 200 on the Y-axis,

31
00:02:01,080 --> 00:02:06,080
so near the top of the screen seems to do well for this left-sided score.

32
00:02:06,720 --> 00:02:11,720
Now let's make this show up by going back to our main.py and importing our

33
00:02:12,210 --> 00:02:16,200
scoreboard class and creating a new scoreboard object.

34
00:02:17,850 --> 00:02:21,570
So when we hit run, you can see that big zero show up bright here.

35
00:02:22,590 --> 00:02:23,010
Now,

36
00:02:23,010 --> 00:02:27,870
the next thing we need to do is to show the right-sided player's score.

37
00:02:28,410 --> 00:02:33,410
We're going to make our scoreboard go to the opposite side of the screen,

38
00:02:34,800 --> 00:02:37,980
so that's going to be +100 and again

39
00:02:37,980 --> 00:02:40,230
with a Y value of 200.

40
00:02:40,980 --> 00:02:45,360
And then we're going to get it to write the right-sided score,

41
00:02:45,990 --> 00:02:50,730
so our score, and we're going to have the same alignment and the same font.

42
00:02:51,720 --> 00:02:56,250
Now you can see 0 0 when the game starts out. Now,

43
00:02:56,250 --> 00:03:00,100
what we actually want our scoreboard to do is the keep track of the score.

44
00:03:00,490 --> 00:03:05,110
So when the right paddle misses, it's a score to the left-sided player.

45
00:03:05,860 --> 00:03:08,530
We can define that in our main.py.

46
00:03:09,070 --> 00:03:11,350
When the right paddle misses,

47
00:03:11,530 --> 00:03:14,500
then we're going to get these scoreboard to

48
00:03:14,530 --> 00:03:17,500
give the left side a point.

49
00:03:17,920 --> 00:03:22,920
So maybe we could have a method called l_point in our scoreboard that increases

50
00:03:24,820 --> 00:03:27,460
the point in the left-sided player.

51
00:03:27,700 --> 00:03:32,530
So we can say self.l_score +=1.

52
00:03:33,400 --> 00:03:36,490
In addition, we're going to need to update our scoreboard,

53
00:03:36,880 --> 00:03:39,790
but all of that code is stuck in the init.

54
00:03:40,090 --> 00:03:44,740
So let's go ahead and move it out into a separate function

55
00:03:44,770 --> 00:03:47,110
which we'll call update_scoreboard. 

56
00:03:49,470 --> 00:03:52,290
And we can call self.update_scoreboard

57
00:03:52,530 --> 00:03:57,180
when we first initialized the scoreboard but also when we score a point.

58
00:03:59,280 --> 00:04:00,960
Now, when we run our code,

59
00:04:01,020 --> 00:04:05,400
you can see that when the right-sided misses, our left

60
00:04:05,400 --> 00:04:10,230
side gets a point. But at the moment, it's overwriting the previous score.

61
00:04:10,800 --> 00:04:12,030
So to prevent that,

62
00:04:12,060 --> 00:04:16,890
we have to add a self.clear before we update the scoreboard.

63
00:04:18,269 --> 00:04:23,010
And you can see now it erases everything that used to be there and it updates

64
00:04:23,010 --> 00:04:27,840
the score. So we've addressed when you are right paddle misses,

65
00:04:27,900 --> 00:04:31,290
but what about when our left paddle misses? Well,

66
00:04:31,290 --> 00:04:34,200
we should have a method called r_point as well

67
00:04:34,440 --> 00:04:36,720
that gives the right side a point.

68
00:04:38,070 --> 00:04:39,780
So let's add that over here.

69
00:04:42,690 --> 00:04:47,550
Increase the r_score by one and we'll again call

70
00:04:47,550 --> 00:04:48,600
update_scoreboard.

71
00:04:52,020 --> 00:04:52,380
Right.

72
00:04:52,380 --> 00:04:57,300
Now when our left side misses, we get a point to the left,

73
00:04:57,720 --> 00:05:00,900
and when our left side misses, we get a point to the right.

74
00:05:01,800 --> 00:05:05,220
That's pretty much all of pong. Now,

75
00:05:05,250 --> 00:05:08,910
if you want an extra challenge both in terms of playing pong

76
00:05:09,300 --> 00:05:10,530
as well as the code,

77
00:05:10,950 --> 00:05:15,950
see if you can figure out a way of getting the ball to increase its speed

78
00:05:16,320 --> 00:05:18,330
every time it hits a paddle,

79
00:05:18,780 --> 00:05:23,780
because that will make the game a lot more exciting instead of having it always

80
00:05:24,150 --> 00:05:27,660
move so slowly. If you can't figure it out,

81
00:05:27,900 --> 00:05:30,630
then continue watching and I'll show you the solution.

82
00:05:33,990 --> 00:05:36,720
So in order to speed up the ball,

83
00:05:37,170 --> 00:05:42,170
the key is in how much time we make our game loop sleep.

84
00:05:43,140 --> 00:05:46,440
Because the shorter that this sleep is,

85
00:05:46,470 --> 00:05:48,690
let's say instead of 0.1 second

86
00:05:48,720 --> 00:05:52,050
let's do 0.01 second.

87
00:05:53,010 --> 00:05:57,000
And if we run now, you can see our ball moves a lot faster.

88
00:05:57,560 --> 00:06:00,590
Let's make it even more extreme and add another zero.

89
00:06:02,750 --> 00:06:07,130
So how can we reduce this number by a little bit each time,

90
00:06:07,400 --> 00:06:09,650
but never make it go into the negative

91
00:06:09,710 --> 00:06:12,140
because if this becomes a negative number,

92
00:06:12,410 --> 00:06:17,300
then we actually get an error over here telling us that the sleep length must be

93
00:06:17,300 --> 00:06:20,240
non-negative. In order to do this,

94
00:06:20,300 --> 00:06:24,740
we have to figure out a way of making this number a little bit smaller each

95
00:06:24,740 --> 00:06:25,573
time.

96
00:06:25,910 --> 00:06:30,860
So let's make this number a attribute in our ball class.

97
00:06:31,460 --> 00:06:36,260
Now I don't want to call it speed because that might be a bit confusing because

98
00:06:36,350 --> 00:06:40,010
the turtle class already has a method called speed

99
00:06:40,340 --> 00:06:45,290
as you can see here. So instead I'm going to call it move_speed,

100
00:06:45,860 --> 00:06:47,810
and I'm going to set it to 0.1.

101
00:06:49,100 --> 00:06:53,510
So now inside our main.py, instead of having a hard-coded

102
00:06:53,510 --> 00:06:57,470
number I'm going to get hold of my ball.move_speed.

103
00:06:58,370 --> 00:07:03,170
Now, every single time our ball bounces in the X-axis,

104
00:07:03,500 --> 00:07:06,350
it means that has been touched by a paddle.

105
00:07:06,980 --> 00:07:11,690
So inside this method, in addition to getting it to reverse its direction,

106
00:07:12,110 --> 00:07:17,110
we can get the move_speed to reduce by multiplying it by a decimal number.

107
00:07:18,200 --> 00:07:22,940
So I've stuck with 0.9 because that seems to work for me.

108
00:07:23,240 --> 00:07:24,320
So for example,

109
00:07:24,350 --> 00:07:29,350
if we start out with a value of 5 and we multiply that by 0.9,

110
00:07:29,870 --> 00:07:31,190
it becomes 4.5.

111
00:07:31,550 --> 00:07:35,300
And if we multiply that by 0.9 becomes 4.05.

112
00:07:35,720 --> 00:07:39,140
So basically each time the ball bounces on a paddle,

113
00:07:39,410 --> 00:07:43,790
it's going to increase in speed. But this can't go on indefinitely.

114
00:07:44,060 --> 00:07:47,630
When we reset the game once one player has lost,

115
00:07:47,900 --> 00:07:52,580
we have to set this move_speed back to the original 0.1,

116
00:07:53,030 --> 00:07:57,020
just so that it doesn't keep on increasing in speed indefinitely.

117
00:07:57,740 --> 00:08:01,190
So now you can see whenever the ball hits a paddle,

118
00:08:02,510 --> 00:08:07,340
it will increase its speed ever so slightly making it just a little bit more

119
00:08:07,340 --> 00:08:09,080
challenging for the players.

120
00:08:10,130 --> 00:08:14,120
Now you can of course tweak around with that number so instead of 0.9,

121
00:08:14,120 --> 00:08:18,110
you could have 0.5, which basically haves the speed each time,

122
00:08:18,560 --> 00:08:22,790
but have a play around with that and see what you prefer. Now

123
00:08:22,790 --> 00:08:27,530
one last thing to mention before we finish off today is that when I'm testing

124
00:08:27,530 --> 00:08:29,690
this program on my computer,

125
00:08:30,080 --> 00:08:33,650
it seems like when I hold down the up hey or the down key,

126
00:08:33,679 --> 00:08:36,289
my paddle will continuously move.

127
00:08:36,740 --> 00:08:41,659
But there seems to be a bug with turtle where if I hold down the 'w' key,

128
00:08:41,990 --> 00:08:45,230
it can only move one bit at a time,

129
00:08:45,710 --> 00:08:48,380
but it doesn't seem to affect the 's' key.

130
00:08:49,070 --> 00:08:51,740
So I've been doing a bit of digging around online

131
00:08:51,800 --> 00:08:56,610
and I can't figure out exactly why this is. If you manage to figure it out,

132
00:08:56,640 --> 00:08:58,980
then feel free to add it to the Q/A so that

133
00:08:58,980 --> 00:09:03,870
other students can learn from it. But so far I've been coming up empty.

134
00:09:04,320 --> 00:09:05,580
But this is just a warning

135
00:09:05,580 --> 00:09:10,580
in case you find this behavior as well on your computer and you get confused and

136
00:09:10,620 --> 00:09:12,990
thinking it's something wrong with your code. It's not.

137
00:09:13,290 --> 00:09:17,520
It's something about the turtle module that causes it to behave like this.

138
00:09:17,850 --> 00:09:19,650
So don't worry if that's happening to you.

139
00:09:20,130 --> 00:09:22,080
And if you're playing pong with a friend,

140
00:09:22,290 --> 00:09:24,690
you can always trick them to use the left sided player.

