1 00:00:00,000 --> 00:00:02,250 SPEAKER: All right, in the last episode, Pong 10, 2 00:00:02,250 --> 00:00:04,630 we ended up actually implementing the victory update. 3 00:00:04,630 --> 00:00:06,960 So now our game is more or less complete. 4 00:00:06,960 --> 00:00:09,120 But we're missing a little bit of the key polish. 5 00:00:09,120 --> 00:00:12,310 In particular, audio can add a lot to a game, at least in my opinion. 6 00:00:12,310 --> 00:00:15,630 This is one of my favorite parts of this track. 7 00:00:15,630 --> 00:00:18,630 And in this update, we're not going to have anything visually different, 8 00:00:18,630 --> 00:00:19,463 as you can see here. 9 00:00:19,463 --> 00:00:22,122 But we are going to introduce some sounds, particularly 10 00:00:22,122 --> 00:00:25,080 when the ball hits the paddle, when the ball hits the top of the screen 11 00:00:25,080 --> 00:00:27,390 or the bottom of the screen, and when the ball goes 12 00:00:27,390 --> 00:00:31,710 beyond the left or the right side of the screen when someone scores a point. 13 00:00:31,710 --> 00:00:34,740 So an important function, the most important function we look at here, 14 00:00:34,740 --> 00:00:38,490 is love.audio.newSource, where this creates an audio object. 15 00:00:38,490 --> 00:00:41,500 So it points to a path, which is where it's 16 00:00:41,500 --> 00:00:44,640 going to find an audio file, a dot wav, dot MP3, whatever 17 00:00:44,640 --> 00:00:45,990 you have in your directory. 18 00:00:45,990 --> 00:00:49,410 And it can take optionally a type, whether it's streamed a piece of audio, 19 00:00:49,410 --> 00:00:51,150 so it'll load audio dynamically. 20 00:00:51,150 --> 00:00:54,302 If it's large, you have a bunch of audio files that can be useful. 21 00:00:54,302 --> 00:00:57,010 Or static, meaning that's going to be stored in memory somewhere. 22 00:00:57,010 --> 00:00:58,718 And therefore, it will be faster loading, 23 00:00:58,718 --> 00:01:00,990 but it's going to incur a memory loss. 24 00:01:00,990 --> 00:01:03,510 And what I want to introduce you to is a program 25 00:01:03,510 --> 00:01:05,190 that I really like for Windows and Mac. 26 00:01:05,190 --> 00:01:07,140 Unfortunately, it's not a Linux build for it. 27 00:01:07,140 --> 00:01:11,190 But it's called bfxr, which allows you to generate a pre-configured, sort 28 00:01:11,190 --> 00:01:13,890 of from a pre-configured set of options sound effects 29 00:01:13,890 --> 00:01:16,740 that really match with a retro sort of aesthetic. 30 00:01:16,740 --> 00:01:18,010 So it's a very simple program. 31 00:01:18,010 --> 00:01:20,010 And we're going to use it here in just a second. 32 00:01:20,010 --> 00:01:24,120 You go to bfxr.net if you want to download a version, a copy for your OS. 33 00:01:24,120 --> 00:01:26,620 So I'm going to skip to the code, I'm going to go over here. 34 00:01:26,620 --> 00:01:27,930 This is bfxr.net. 35 00:01:27,930 --> 00:01:30,150 Up here is a download link. 36 00:01:30,150 --> 00:01:31,830 And bfxr is the program here. 37 00:01:31,830 --> 00:01:34,120 I'm going to hide this behind the screen. 38 00:01:34,120 --> 00:01:36,420 And if you just click any of these options, 39 00:01:36,420 --> 00:01:40,680 you'll get a randomly generated audio clip that sort of fits that paradigm. 40 00:01:40,680 --> 00:01:44,310 You can see there's pickups and coins, lasers and shooting, explosions, 41 00:01:44,310 --> 00:01:47,640 et cetera, et cetera, before I actually found a great sound effect just 42 00:01:47,640 --> 00:01:48,780 on the-- 43 00:01:48,780 --> 00:01:51,450 I believe it was the, this is the blip select. 44 00:01:51,450 --> 00:01:54,000 So this is a sound 45 00:01:54,000 --> 00:01:57,348 And I couldn't really think of a more perfect sound just for the Pong 46 00:01:57,348 --> 00:01:58,140 hitting the paddle. 47 00:01:58,140 --> 00:01:59,682 So we're going to use this, actually. 48 00:01:59,682 --> 00:02:01,110 I'm going to export wav here. 49 00:02:01,110 --> 00:02:07,740 I'm going to go over to jharvard, dev, games, Pong, and then Pong 11. 50 00:02:07,740 --> 00:02:11,653 And I'm just going to save this as paddlehit.wav. 51 00:02:11,653 --> 00:02:13,320 So that's when the ball hits the paddle. 52 00:02:13,320 --> 00:02:16,445 And I want something for when it hits the top and the bottom of the screen. 53 00:02:16,445 --> 00:02:18,400 So hit hurt is really good for that. 54 00:02:18,400 --> 00:02:21,870 So I'm going to just kind of go through this and find a good sound effect, so. 55 00:02:21,870 --> 00:02:26,760 56 00:02:26,760 --> 00:02:27,918 That was pretty good. 57 00:02:27,918 --> 00:02:28,960 So let's play that again. 58 00:02:28,960 --> 00:02:31,635 59 00:02:31,635 --> 00:02:32,510 Yeah, that's not bad. 60 00:02:32,510 --> 00:02:34,302 We'll use that, just for the sake of speed. 61 00:02:34,302 --> 00:02:37,920 We won't find the perfect audio file, but it gives you an example. 62 00:02:37,920 --> 00:02:41,570 So we'll call this wall hit. 63 00:02:41,570 --> 00:02:45,030 And then lastly, when something hits the left or the right side of the screen. 64 00:02:45,030 --> 00:02:47,770 So we'll go to explosion. 65 00:02:47,770 --> 00:02:49,555 So let's go to-- 66 00:02:49,555 --> 00:02:53,870 67 00:02:53,870 --> 00:02:57,030 That one sounds OK, but we'll keep going. 68 00:02:57,030 --> 00:02:57,530 There we go. 69 00:02:57,530 --> 00:03:00,020 And that one sounds a little bit more final. 70 00:03:00,020 --> 00:03:01,400 So we'll choose that one. 71 00:03:01,400 --> 00:03:04,865 So we're going to export a wav, and we'll call that point scored. 72 00:03:04,865 --> 00:03:07,318 73 00:03:07,318 --> 00:03:08,610 These are completely arbitrary. 74 00:03:08,610 --> 00:03:11,277 In the final distro, you'll actually get different sound effects 75 00:03:11,277 --> 00:03:14,383 than this that are a little bit more curated and, I think, 76 00:03:14,383 --> 00:03:15,300 a little more fitting. 77 00:03:15,300 --> 00:03:18,030 But for the sake of speed, just to illustrate the use of the program, 78 00:03:18,030 --> 00:03:19,000 we're going to do that. 79 00:03:19,000 --> 00:03:22,740 You'll see in VsCode, I have the three wav files here nicely illustrated 80 00:03:22,740 --> 00:03:25,823 with this wav icon, the sound icon. 81 00:03:25,823 --> 00:03:28,740 The first thing that we need to do is, again, these need to be stored, 82 00:03:28,740 --> 00:03:31,670 these need to be pulled into love through some sort of reference. 83 00:03:31,670 --> 00:03:33,815 So we're going to use love.audio.newSource. 84 00:03:33,815 --> 00:03:35,940 I want to store them together in a table, actually. 85 00:03:35,940 --> 00:03:37,830 And we haven't really looked at tables too much, 86 00:03:37,830 --> 00:03:39,080 but we're going to do it here. 87 00:03:39,080 --> 00:03:40,725 I'm going to call this sounds. 88 00:03:40,725 --> 00:03:42,600 And I'm going to use these curly braces again 89 00:03:42,600 --> 00:03:46,380 to mean a table, a key value pair association. 90 00:03:46,380 --> 00:03:49,545 So we'll call the first one paddle_hit. 91 00:03:49,545 --> 00:03:50,670 And I'm going to assign it. 92 00:03:50,670 --> 00:03:52,470 So this is how you would assign a key. 93 00:03:52,470 --> 00:03:56,070 So the string paddle hit is going to be associated. 94 00:03:56,070 --> 00:03:59,320 And we use those square brackets, again, to signal 95 00:03:59,320 --> 00:04:01,830 this is going to be a key in a table. 96 00:04:01,830 --> 00:04:06,000 Paddle hit is going to be equal to love.audio.newSource 97 00:04:06,000 --> 00:04:09,690 for new audio source, and this is going to be paddlehit.wav. 98 00:04:09,690 --> 00:04:11,533 And we'll set this to be a static file. 99 00:04:11,533 --> 00:04:12,700 So I'm going to put a comma. 100 00:04:12,700 --> 00:04:14,700 I'm going to also do points scored. 101 00:04:14,700 --> 00:04:16,140 So we'll do points scored. 102 00:04:16,140 --> 00:04:19,769 It's going to be equal to love.audio.newSource 103 00:04:19,769 --> 00:04:24,060 of point_scored.wav static. 104 00:04:24,060 --> 00:04:25,880 And lastly, wall hit. 105 00:04:25,880 --> 00:04:27,840 So wall_hit. 106 00:04:27,840 --> 00:04:35,220 And that'll be equal to love.audio.newSource wall_hit.wav, 107 00:04:35,220 --> 00:04:36,360 and static against. 108 00:04:36,360 --> 00:04:39,240 Now I have a table with three audio objects. 109 00:04:39,240 --> 00:04:43,170 On these audio objects, I can then call the function play. 110 00:04:43,170 --> 00:04:47,190 And in my code, I can fire when they actually play in the game that way. 111 00:04:47,190 --> 00:04:48,907 And also call pause and stop. 112 00:04:48,907 --> 00:04:50,740 You can set them to loop over and over again 113 00:04:50,740 --> 00:04:52,622 if you want to, which we won't do. 114 00:04:52,622 --> 00:04:54,580 That would get a little obnoxious very quickly. 115 00:04:54,580 --> 00:04:57,480 But let's go ahead and find where it collides 116 00:04:57,480 --> 00:04:59,320 with the ball, which is right here. 117 00:04:59,320 --> 00:05:01,770 So this is a ball colliding with the paddle. 118 00:05:01,770 --> 00:05:03,360 So again, this is paddle hit. 119 00:05:03,360 --> 00:05:07,030 So I can say sounds at paddle_hit, colon-- 120 00:05:07,030 --> 00:05:09,450 colon is very important because this is an object-- 121 00:05:09,450 --> 00:05:11,820 play, and then like that, just like that. 122 00:05:11,820 --> 00:05:12,890 We'll copy this. 123 00:05:12,890 --> 00:05:15,238 We'll come down here, and we'll do the same thing. 124 00:05:15,238 --> 00:05:18,030 So now it's going to play the paddle hit sound, whether it collides 125 00:05:18,030 --> 00:05:21,087 with the right or the left paddle. 126 00:05:21,087 --> 00:05:22,170 I'm going to go ahead now. 127 00:05:22,170 --> 00:05:26,590 We need to find where it actually finds the screen boundaries, which is here. 128 00:05:26,590 --> 00:05:32,250 So this will be wall_hit, so I'll say sounds at wall_hit colon play, 129 00:05:32,250 --> 00:05:35,040 copy that, come down here, do it there as well. 130 00:05:35,040 --> 00:05:37,020 So now if it hits the bottom or the top-- 131 00:05:37,020 --> 00:05:39,060 sorry, of the top or the bottom of the screen, 132 00:05:39,060 --> 00:05:41,730 it will trigger the wall hit sound. 133 00:05:41,730 --> 00:05:43,830 And then lastly, we need the points scored 134 00:05:43,830 --> 00:05:45,720 sound, which is going to be right here. 135 00:05:45,720 --> 00:05:52,710 So we'll do sounds point_scored colon play, copy that down here, 136 00:05:52,710 --> 00:05:58,740 and then we'll do that, just like that, which should be good, I think. 137 00:05:58,740 --> 00:06:00,220 Yep, that looks good to me. 138 00:06:00,220 --> 00:06:01,728 Let's go ahead and run this now. 139 00:06:01,728 --> 00:06:03,520 So everything looks more or less identical. 140 00:06:03,520 --> 00:06:07,870 So what I'm going to do now is start the game. 141 00:06:07,870 --> 00:06:09,620 So it triggers it when it hits the paddle. 142 00:06:09,620 --> 00:06:12,490 143 00:06:12,490 --> 00:06:15,300 It did trigger when it hit the top of the screen, 144 00:06:15,300 --> 00:06:17,925 and then when it hits the left or the right side of the screen, 145 00:06:17,925 --> 00:06:21,090 it does have that very sad crunchy explosion sort of sound. 146 00:06:21,090 --> 00:06:22,755 So that was it to the audio update. 147 00:06:22,755 --> 00:06:24,880 You don't have a music track or anything like that, 148 00:06:24,880 --> 00:06:26,782 which is very easy to also do. 149 00:06:26,782 --> 00:06:28,990 And for that, you might want to actually set looping. 150 00:06:28,990 --> 00:06:30,930 So we're going to get to Pong 12 in the next section, which 151 00:06:30,930 --> 00:06:33,210 is the very last section and a very simple one, 152 00:06:33,210 --> 00:06:36,848 where we take a look at how to actually resize the application whenever 153 00:06:36,848 --> 00:06:38,640 we want to, let's say the window is-- maybe 154 00:06:38,640 --> 00:06:40,765 you want to make it a little bit larger or smaller, 155 00:06:40,765 --> 00:06:42,610 but maintain the aspect ratio. 156 00:06:42,610 --> 00:06:45,180 It's very nice and easy to do with the push library. 157 00:06:45,180 --> 00:06:49,500 So take a look at the next section at Pong 12, the resize update. 158 00:06:49,500 --> 00:06:50,000