SPEAKER: All right, in the last episode, Pong 10,
we ended up actually implementing the victory update.
So now our game is more or less complete.
But we're missing a little bit of the key polish.
In particular, audio can add a lot to a game, at least in my opinion.
This is one of my favorite parts of this track.
And in this update, we're not going to have anything visually different,
as you can see here.
But we are going to introduce some sounds, particularly
when the ball hits the paddle, when the ball hits the top of the screen
or the bottom of the screen, and when the ball goes
beyond the left or the right side of the screen when someone scores a point.
So an important function, the most important function we look at here,
is love.audio.newSource, where this creates an audio object.
So it points to a path, which is where it's
going to find an audio file, a dot wav, dot MP3, whatever
you have in your directory.
And it can take optionally a type, whether it's streamed a piece of audio,
so it'll load audio dynamically.
If it's large, you have a bunch of audio files that can be useful.
Or static, meaning that's going to be stored in memory somewhere.
And therefore, it will be faster loading,
but it's going to incur a memory loss.
And what I want to introduce you to is a program
that I really like for Windows and Mac.
Unfortunately, it's not a Linux build for it.
But it's called bfxr, which allows you to generate a pre-configured, sort
of from a pre-configured set of options sound effects
that really match with a retro sort of aesthetic.
So it's a very simple program.
And we're going to use it here in just a second.
You go to bfxr.net if you want to download a version, a copy for your OS.
So I'm going to skip to the code, I'm going to go over here.
This is bfxr.net.
Up here is a download link.
And bfxr is the program here.
I'm going to hide this behind the screen.
And if you just click any of these options,
you'll get a randomly generated audio clip that sort of fits that paradigm.
You can see there's pickups and coins, lasers and shooting, explosions,
et cetera, et cetera, before I actually found a great sound effect just
on the--
I believe it was the, this is the blip select.
So this is a sound
And I couldn't really think of a more perfect sound just for the Pong
hitting the paddle.
So we're going to use this, actually.
I'm going to export wav here.
I'm going to go over to jharvard, dev, games, Pong, and then Pong 11.
And I'm just going to save this as paddlehit.wav.
So that's when the ball hits the paddle.
And I want something for when it hits the top and the bottom of the screen.
So hit hurt is really good for that.
So I'm going to just kind of go through this and find a good sound effect, so.
That was pretty good.
So let's play that again.
Yeah, that's not bad.
We'll use that, just for the sake of speed.
We won't find the perfect audio file, but it gives you an example.
So we'll call this wall hit.
And then lastly, when something hits the left or the right side of the screen.
So we'll go to explosion.
So let's go to--
That one sounds OK, but we'll keep going.
There we go.
And that one sounds a little bit more final.
So we'll choose that one.
So we're going to export a wav, and we'll call that point scored.
These are completely arbitrary.
In the final distro, you'll actually get different sound effects
than this that are a little bit more curated and, I think,
a little more fitting.
But for the sake of speed, just to illustrate the use of the program,
we're going to do that.
You'll see in VsCode, I have the three wav files here nicely illustrated
with this wav icon, the sound icon.
The first thing that we need to do is, again, these need to be stored,
these need to be pulled into love through some sort of reference.
So we're going to use love.audio.newSource.
I want to store them together in a table, actually.
And we haven't really looked at tables too much,
but we're going to do it here.
I'm going to call this sounds.
And I'm going to use these curly braces again
to mean a table, a key value pair association.
So we'll call the first one paddle_hit.
And I'm going to assign it.
So this is how you would assign a key.
So the string paddle hit is going to be associated.
And we use those square brackets, again, to signal
this is going to be a key in a table.
Paddle hit is going to be equal to love.audio.newSource
for new audio source, and this is going to be paddlehit.wav.
And we'll set this to be a static file.
So I'm going to put a comma.
I'm going to also do points scored.
So we'll do points scored.
It's going to be equal to love.audio.newSource
of point_scored.wav static.
And lastly, wall hit.
So wall_hit.
And that'll be equal to love.audio.newSource wall_hit.wav,
and static against.
Now I have a table with three audio objects.
On these audio objects, I can then call the function play.
And in my code, I can fire when they actually play in the game that way.
And also call pause and stop.
You can set them to loop over and over again
if you want to, which we won't do.
That would get a little obnoxious very quickly.
But let's go ahead and find where it collides
with the ball, which is right here.
So this is a ball colliding with the paddle.
So again, this is paddle hit.
So I can say sounds at paddle_hit, colon--
colon is very important because this is an object--
play, and then like that, just like that.
We'll copy this.
We'll come down here, and we'll do the same thing.
So now it's going to play the paddle hit sound, whether it collides
with the right or the left paddle.
I'm going to go ahead now.
We need to find where it actually finds the screen boundaries, which is here.
So this will be wall_hit, so I'll say sounds at wall_hit colon play,
copy that, come down here, do it there as well.
So now if it hits the bottom or the top--
sorry, of the top or the bottom of the screen,
it will trigger the wall hit sound.
And then lastly, we need the points scored
sound, which is going to be right here.
So we'll do sounds point_scored colon play, copy that down here,
and then we'll do that, just like that, which should be good, I think.
Yep, that looks good to me.
Let's go ahead and run this now.
So everything looks more or less identical.
So what I'm going to do now is start the game.
So it triggers it when it hits the paddle.
It did trigger when it hit the top of the screen,
and then when it hits the left or the right side of the screen,
it does have that very sad crunchy explosion sort of sound.
So that was it to the audio update.
You don't have a music track or anything like that,
which is very easy to also do.
And for that, you might want to actually set looping.
So we're going to get to Pong 12 in the next section, which
is the very last section and a very simple one,
where we take a look at how to actually resize the application whenever
we want to, let's say the window is-- maybe
you want to make it a little bit larger or smaller,
but maintain the aspect ratio.
It's very nice and easy to do with the push library.
So take a look at the next section at Pong 12, the resize update.