SPEAKER: All right, so in the last example,
we took a look at how to actually implement
serving, so adding on to the sort of set of states that our game has.
In this example, we'll take a look at another state, a victory state,
which will allow it to show that a player wins when they
reached a certain amount of points.
Now, typically it's 10 points with Pong.
For the sake of illustration, what we'll probably
end up doing is making it two or three points just for the--
If we want to actually test it and make sure it works,
it would kind of save a little bit of time just
to do that instead of having it be 10 points.
So why do we go over to the code.
This isn't going to be a terribly difficult thing to add.
What we will need, like I said again, is a victory state.
And a victory state is just going to be whenever player one score or player 10
score is equal to 10.
So what we're going to go ahead and do first
is I have a display score function here that I've also
separated out in the distro in case you want to take a look at that.
But let's go ahead and add another if condition
here to the draw sort of side, the love.draw
function that we've implemented.
So else if game state is equal to victory,
then we're going to draw a victory message.
Now first of all, we need to do is set the victory stage
somewhere in our code for this to actually trigger.
So I'm going to go up to the update function where people get points.
Let's see that is over here.
So if ball X is less than zero or it's greater than virtual width,
then we end up having it be the case that someone scores.
So in this case, we'll add a situation here.
So we'll say if player 2 score is greater than or equal to 10.
It will only mean only ever be 10 and we'll be greater than 10,
but we'll set the game state to be victory.
Else, we'll take this line here, and just plug it in right there.
So if it's 10 then they'll get set to the victory state,
if not they'll get set to the serve state.
And I want to do the exact same thing over here.
So I'm going to do is just get rid of this,
and set that to the player one score not the player two score.
So that's good.
So the game state should be set to victory Additionally,
we're probably going to want to say who the winning player was.
So I'm going to create a new variable.
I'm actually going to come up here to the top.
Underneath serving player, I'm going to say winning player
and I missed that it's a zero for right now because no one has actually won.
I'm gonna save it.
I'm gonna come back down to where I just added the victory state code,
I'm going to say winning player is equal to 2,
in the case that player 2 score is greater than or equal to 10.
And in the case that player one score is greater than or equal to 10,
I'm gonna set that to winning player is equal to one.
Let's go down back to the bottom where we have the print messages right here.
And what I want to do is, like I showed in the slide,
there's maybe a little bit of a larger header and then a smaller header
that says first off that Player X 1, and then the smaller header that
says press Enter to restart.
So what I'm going to do is say love.graphics.setfont
and let's say that we're going to have a font called victory font.
We haven't created it yet, but we will very shortly.
I want to say love.graphics.printf, and let's just take this code here.
Copy it, print it, right here, player plus
dot dot, the string of the not serving player but the winning player,
and then I'm going to save space wins.
And then we're going to go ahead and just say that's going to be at 10,
and then it's going to be virtual width and center et cetera, et cetera.
Then we'll take the next message for press Enter to serve
and let's assume that our font is going to be 24 pixels.
So what we need to do is instead of setting
it 10 pixels below the other one, we need
to set it at least 24 pixels, actually 24 plus 8.
So let's just say 32 pixels below 10.
So let's just say that that's going to be at 42, just like that.
And we'll test that out just to make sure it works.
We might have to finagle it a little bit to make sure it actually works well.
And that's it for the victory message that we have.
So again, the next thing we need to do is actually
set such that when the user presses enter during the victory state,
it resets the game.
Because currently it's going to sort of be
like almost like a serve state or a start state in and of itself,
but just showing the victory message.
So we'll say in the event that enter or return
is pressed, if the game state is start, then we need to set it to serve.
Else if game state is equal to victory, then we'll
set the game state equal to to start actually.
We'll start the game from the very beginning.
So it'll show who disturbing player actually is,
and then that should be most of the code.
So let's go ahead, and actually one more thing too,
instead of setting the square root check for 10 and 2, I'm a little bit,
scored a little bit above where I wanted to go, let's
just set it to three for now just for testing purposes.
Set to 3, do that, and let's go ahead and run this.
So we have everything looking normal.
We have the FPS marker.
Let's go ahead, player one serves, just going to go to the right.
Player two serves, and then eventually we're going to have player,
looks like player one is going to win.
Oh, and we have, right we forgot to create the victory font.
OK, that is my bad.
Let's go ahead and do that really quickly.
Where we have the other fonts, I'm gonna set
create a victory font is equal to love.graphics.newfont.
And it's gonna be font.ttf.size24.
Save that.
And we are setting, actually no we're not
setting the font I don't think back to the normal one afterwards.
So we set it to victory font.
We do the player something wins.
Then we need to set love.graphics. setfont to the small font
for the message after that let's run that again.
And we'll painstakingly do this one more time.
I apologize.
But thankfully we set it to three and not 10, so this shouldn't be too bad.
And boom, player one wins.
And then we see we have the 3 and the 2 there we can still move.
If I press Enter, it gets back to the start state.
And If I press Enter one more time.
Oh what I'm realizing actually, we forgot to set the scores back
to 0 0, very important piece.
So why don't we do that as well.
Let's go over to the victory state over where it says, oh, it's
in love.key pressed, key handler here.
So we're in the victory, press start, player
one score is going to be equal to zero.
Player two score is equal to zero.
Let's try it one more time, fingers crossed this should be everything.
OK.
1,0, 1,2, 2, 1, 2, 2.
And finally, 3 2, press Enter and the scores get reset back to 0 0.
So that's it for the victory update.
Everything is more or less there in terms of game functionality.
We have two very simple updates coming up.
Actually the next update is probably my favorite.
It's the audio update we're actually going
to have sound effects working with the ball hitting the paddle,
and with getting a score upgrade.
And the very last minute can be a very minor thing I had to resize the canvas,
but the core functionality for Pong is here, it's very exciting.
I'll see you on the next video for Pong 11, the audio update.