COLTON OGDEN: All right.
Pong 5 was a bit more work.
We ended up actually refactoring our code base
to use object-oriented programming or a little taste
there of, a little bit different than having a bunch of variables
keeping track, a little more procedural style as before,
much like how C actually is written.
But the way we structure things now is more akin to what you might see
in Java, or what you might see in C#, or even in Python,
depending on what code base you're working on--
a little bit easier to keep track of multiple things moving around.
And games in particular are very sort of prone to this model of programming.
Now this is a little bit of a break.
It's a little bit of a simpler iteration in the Pong track.
We're going to be taking a look just at implementing a simple FPS
marker at the top left of the screen.
And to do that, you can see there it's written in green text, usually
the same font, same size as the text at the top of the screen.
But this is literally just going to use a simple function built
into love that just tells us what our frame rate is currently at.
And so here are a couple of the functions
in particular that we're going to look at, so love.window.setTitle.
So this one is not germane to what we've just talked about,
but if you've noticed, so far at the very top we've had untitled.
But now, as is shown in this particular example,
we have the word "Pong" actually written there.
So this is a little bit of a cleanup, just something that
looks better if you're running your game in a windowed setting.
So we're going to be doing that.
It literally just sets the title to a string, whatever you want it to be.
More germane to what we've talked about is love.timer.getFPS,
which is just going to return as a float the FPS value of our game--
so very simple, very straightforward.
And that's pretty much it.
So let's actually go right into the code here.
So what I'm going to do is I'm going to scroll down to the very bottom.
And what we're going to do is just render this to the screen.
So we know we want a string rendered at the top left.
All I'm going to do really is I just need to insert something right here.
Basically after everything else is rendered, I want to display the FPS.
Now I don't want to get into the habit of just writing a bunch of code
in place inside these methods.
I think this would be more suited to a function.
So let's say I want to display FPS like that.
I'm calling it, like you might have seen in C, just a very simple, sort
of procedural function call.
Because what I'm going to do is just write it here in main.lua itself.
So I'm going to say function displayFPS.
I'm not even going to have it take a parameter.
This is going to be very straightforward.
I'm going to say love.graphics.setColor.
And this, actually, I don't think we've taken a look at either.
We've taken a look at love.graphics.clear.
Love.graphics.setColor is very similar to that,
only this actually toggles the color for things
like drawing text to the screen, which by default, everything,
as you've noticed, is white.
So we're going to say love.graphics.setColor.
I want this to be green, so, again, it's RGB alpha.
So the G component is what we care about.
So I'm going to say 0 for the red.
Let's say 255-- well, rather--
we'll skip the 255 stuff.
We'll just say 0,1 for full green, 0 for the blue, and 1 for the alpha.
Then I'm going to say love.graphics.setFont
of smallfont and love.graphics.print.
And then we'll say our FPS is going to be--
well, we need to get the FPS.
So in order to do that, I'm going to say tostring love.timer.getFPS
as a function call.
And we need this tostring method-- this is very important--
because this is going to actually give us an integer value.
And this value is going to be incompatible with adding to a string.
You might actually notice that we have this dot dot here.
This dot dot is the string concatenation operator.
So this just means we add two strings together, one
and then tail it at the end of the other one.
You probably touched on this in C throughout the course.
Now it's a different operator in Lua.
In Python, it's a plus.
And in C-- well, you can't really do that with C. You
have to use something else altogether.
But what we're going to do is say FPS, concatenate with that.
And then we're going to-- let's just draw this, let's say, at 40.
And for-- actually, no.
We'll do it at, yeah, 40 and 20.
And that's it.
And then one last thing I do want to do actually
is set everything back to white as the default
color for whenever it draws the next thing-- the paddles,
and the text, and whatnot.
So I'm going to say 1, 1, 1, 1 at the end of that.
And if we run the code here, we do indeed
see we have an FPS display at the top of the screen.
And it started off at 49.
It's a little bit of a weird thing.
Love actually has to get a little bit of frame data
so that it can calculate what the FPS is, because it actually
tracks it across multiple frames.
That's why at first it was 0, then 49, and now it is smoothly at 60.
Thankfully, my computer is strong enough to run this very simple, barely
functioning Pong example.
So that was it for Pong 6, very simple.
In the next update, we're going to take something a little bit meatier
on our hands, the collision update.
So so far, the ball does not actually collide with the paddle at all.
It just will go straight through because we
have no code to test for whether the ball and the paddle have intersected.
So we're going to add this major change to the code base in the next section
and start talking about collision with Pong 7.