To start, we're going to need to create a new scoreboard file.
And then inside this file
we're going to add our usual imports and we're going to create the scoreboard
class.
Now the scoreboard class is going to inherit from the turtle class,
so we can go ahead and add all of our inits and our super.init.
Now let's define our scoreboard.
It's going to have a white color so it shows up on the black background,
it's going to have pen up because we don't want it to draw,
and it's also going to have its turtle hidden because we're only interested in
it being able to write something.
And the thing that we want it to write is the current score.
Let's go ahead and create some attributes for the scoreboard class.
One is going to be the l_score which starts off at zero,
and the other is going to be the r_score which starts off at zero.
So our scoreboard is going to keep track of how the left player and the right
player are doing.
And then it's going to write this onto the screen.
So we're going to write the self.l_score first,
and we're going to change the alignment to center and also change the font to
something quite large,
like a courier font with 80 points and have a normal weight.
Where do we want this to be written though? Because by default,
this is going to be written at the center of the screen, (0, 0).
But instead,
what we want it to do is we probably want it to go to somewhere on the top-left
of the screen. This is the left player score.
So you can have a play around with the coordinates,
but I found that -100 in the X-axis,
so a little bit to the left of the center and then 200 on the Y-axis,
so near the top of the screen seems to do well for this left-sided score.
Now let's make this show up by going back to our main.py and importing our
scoreboard class and creating a new scoreboard object.
So when we hit run, you can see that big zero show up bright here.
Now,
the next thing we need to do is to show the right-sided player's score.
We're going to make our scoreboard go to the opposite side of the screen,
so that's going to be +100 and again
with a Y value of 200.
And then we're going to get it to write the right-sided score,
so our score, and we're going to have the same alignment and the same font.
Now you can see 0 0 when the game starts out. Now,
what we actually want our scoreboard to do is the keep track of the score.
So when the right paddle misses, it's a score to the left-sided player.
We can define that in our main.py.
When the right paddle misses,
then we're going to get these scoreboard to
give the left side a point.
So maybe we could have a method called l_point in our scoreboard that increases
the point in the left-sided player.
So we can say self.l_score +=1.
In addition, we're going to need to update our scoreboard,
but all of that code is stuck in the init.
So let's go ahead and move it out into a separate function
which we'll call update_scoreboard.
And we can call self.update_scoreboard
when we first initialized the scoreboard but also when we score a point.
Now, when we run our code,
you can see that when the right-sided misses, our left
side gets a point. But at the moment, it's overwriting the previous score.
So to prevent that,
we have to add a self.clear before we update the scoreboard.
And you can see now it erases everything that used to be there and it updates
the score. So we've addressed when you are right paddle misses,
but what about when our left paddle misses? Well,
we should have a method called r_point as well
that gives the right side a point.
So let's add that over here.
Increase the r_score by one and we'll again call
update_scoreboard.
Right.
Now when our left side misses, we get a point to the left,
and when our left side misses, we get a point to the right.
That's pretty much all of pong. Now,
if you want an extra challenge both in terms of playing pong
as well as the code,
see if you can figure out a way of getting the ball to increase its speed
every time it hits a paddle,
because that will make the game a lot more exciting instead of having it always
move so slowly. If you can't figure it out,
then continue watching and I'll show you the solution.
So in order to speed up the ball,
the key is in how much time we make our game loop sleep.
Because the shorter that this sleep is,
let's say instead of 0.1 second
let's do 0.01 second.
And if we run now, you can see our ball moves a lot faster.
Let's make it even more extreme and add another zero.
So how can we reduce this number by a little bit each time,
but never make it go into the negative
because if this becomes a negative number,
then we actually get an error over here telling us that the sleep length must be
non-negative. In order to do this,
we have to figure out a way of making this number a little bit smaller each
time.
So let's make this number a attribute in our ball class.
Now I don't want to call it speed because that might be a bit confusing because
the turtle class already has a method called speed
as you can see here. So instead I'm going to call it move_speed,
and I'm going to set it to 0.1.
So now inside our main.py, instead of having a hard-coded
number I'm going to get hold of my ball.move_speed.
Now, every single time our ball bounces in the X-axis,
it means that has been touched by a paddle.
So inside this method, in addition to getting it to reverse its direction,
we can get the move_speed to reduce by multiplying it by a decimal number.
So I've stuck with 0.9 because that seems to work for me.
So for example,
if we start out with a value of 5 and we multiply that by 0.9,
it becomes 4.5.
And if we multiply that by 0.9 becomes 4.05.
So basically each time the ball bounces on a paddle,
it's going to increase in speed. But this can't go on indefinitely.
When we reset the game once one player has lost,
we have to set this move_speed back to the original 0.1,
just so that it doesn't keep on increasing in speed indefinitely.
So now you can see whenever the ball hits a paddle,
it will increase its speed ever so slightly making it just a little bit more
challenging for the players.
Now you can of course tweak around with that number so instead of 0.9,
you could have 0.5, which basically haves the speed each time,
but have a play around with that and see what you prefer. Now
one last thing to mention before we finish off today is that when I'm testing
this program on my computer,
it seems like when I hold down the up hey or the down key,
my paddle will continuously move.
But there seems to be a bug with turtle where if I hold down the 'w' key,
it can only move one bit at a time,
but it doesn't seem to affect the 's' key.
So I've been doing a bit of digging around online
and I can't figure out exactly why this is. If you manage to figure it out,
then feel free to add it to the Q/A so that
other students can learn from it. But so far I've been coming up empty.
But this is just a warning
in case you find this behavior as well on your computer and you get confused and
thinking it's something wrong with your code. It's not.
It's something about the turtle module that causes it to behave like this.
So don't worry if that's happening to you.
And if you're playing pong with a friend,
you can always trick them to use the left sided player.