COLTON OGDEN: All right.
So with Pong 7, we actually took a big step forward.
We implemented collision detection, which
is a huge buzzword, especially in game programming.
But it turns out, it's very simple.
If you have just simple rectangles and you're using aligned
axis bounding box collision detection, which we were doing,
or AABB, just a few lines of code, and now we
have the ball bouncing off of our paddles
and also from the top and bottom of the screen.
But you didn't have to use AABB before.
Fortunately, we can just check to see whether the y
value is above or below some amount.
Now, I realize, actually, also, in the last couple of examples,
I've been meaning to add the love.window.settitle.
So right now, before I actually get into this update,
love.window.settitle of Pong.
Run it, and it's white again, of course.
So let's go ahead and change this down here to 255, 255, 255, and 255.
Run it, and boom.
Now we have a functional Pong title.
So great.
Told you I was going to do it.
I knew that someone was going to comment on it and make fun of me for it,
and that's totally fine.
I totally deserve it.
But anyway, Pong 8, the score update.
So in this example, we're going to actually implement adding score.
So not only do we just have these static 1
and 0 on the left, which is fitting for a computer science class,
but when the ball goes to the left and the right of the paddles,
if it goes beyond the edges, the right and the left edge,
we want to increment the corresponding score.
So if the ball goes to the left edge of the screen,
the right number should increment, and vice versa.
If the ball goes past the right edge of the screen,
the left number should increment.
And that's all the slides we have for this, actually.
It's going to be a very straightforward, I think, bit of code here.
So let's go ahead and implement this in the update function.
So let's go up to the very top here.
So if the game state is play-- now, we are going to have start,
and we are going to have play.
And actually, what I want to do is go to the very bottom.
I want to delete this game state equals start thing.
We don't really need that anymore.
I'm to go back up here.
And towards the top, when we're in the play state--
I can really do this anywhere, but I'm going to just put it here.
So I'm going to say, if the ball.x is less than or equal to 0,
then we want the left--
sorry.
If the ball is less than 0, we want the right paddle score to increment.
So player 2 score should be equal to player 2 score plus 1.
Now, you might be looking at this and thinking
that this looks a little weird.
Why are we using player 2 score equals player 2 score plus 1?
Can't we use plus equals 1?
Can't we use plus plus?
And unfortunately, Lua is a bit strange in that it doesn't actually
let you do that shorthand syntax.
It will only allow you to do the equals itself plus something.
There is no shorthand plus equals or plus plus, unfortunately.
But we can deal with that.
Not a terrible inconvenience.
We're going to do the same thing for whether it
goes past the right edge of the screen.
So if ball.x is greater than or equal to virtual width minus 4, then--
and remember, we're accounting for that size of the rectangle there--
we're going to say player 1 score is equal to player 1 score plus 1.
And I think I have declared those score variables already,
which I have indeed done.
They're set to 0 initially.
Now, once it is the case that we have added to the score,
we want to actually reset the ball.
So what I can do is I can say, ball reset,
and we'll say game state is equal to start so that we can give them a chance
to press Enter again once someone has scored so that it just doesn't happen,
and they're caught off-guard, like, oh, we just scored.
Now the ball's in the center and it's moving.
We're going to add that little layer, that little pressing
Enter little buffer there.
So game state is equal to start.
Save it.
And then that should be it.
So let's go ahead, and let's make sure also
that we are accounting in the love.key press
function for if they press Return.
Cool.
So we can have this here.
This is fine.
But in reality, all we really need to do is, if game state is equal to start,
game state should be equal to play.
So that's what we're going to do.
We're going to set that to end because now we
don't have to press Enter to go back to the start state.
We can just let the ball go past the left or the right edge.
So I'm going to go ahead and run this.
And let's just give it a test.
Let it bounce off the side.
I will not purposely do that.
And perfect.
It ended getting up one incremented on the right player
because it went past the left edge, which is great.
So let's go ahead and do it again.
I'm going to purposefully hit it back.
Let's just get the other player to slowly get a point onto them,
just to make sure that the left score is working as well.
Got to try not to do it.
And perfect.
So we not only have the scores incrementing,
but the ball is getting reset in the middle of the screen accordingly.
And we made sure to set the title in this episode as well.
So that was it for Pong 8.
Very simple update-- just adding the ability to keep
score on the left and the right side.
We don't actually have keeping track of whether one player has won or not.
We will take care of that soon.
In the next update, we're going to actually take
care of making sure that if player 1 scores, for example, onto player 2,
player 2 gets to serve the ball back to player 1 out of fairness.
We need to keep track of who actually got scored on
and then send the ball in the opposite direction in that circumstance.
We'll see you for Pong 9.