1 00:00:00,000 --> 00:00:01,960 SPEAKER: All right, so in the last example, 2 00:00:01,960 --> 00:00:03,752 we took a look at how to actually implement 3 00:00:03,752 --> 00:00:07,850 serving, so adding on to the sort of set of states that our game has. 4 00:00:07,850 --> 00:00:11,167 In this example, we'll take a look at another state, a victory state, 5 00:00:11,167 --> 00:00:13,500 which will allow it to show that a player wins when they 6 00:00:13,500 --> 00:00:14,958 reached a certain amount of points. 7 00:00:14,958 --> 00:00:17,248 Now, typically it's 10 points with Pong. 8 00:00:17,248 --> 00:00:19,290 For the sake of illustration, what we'll probably 9 00:00:19,290 --> 00:00:21,950 end up doing is making it two or three points just for the-- 10 00:00:21,950 --> 00:00:24,200 If we want to actually test it and make sure it works, 11 00:00:24,200 --> 00:00:25,980 it would kind of save a little bit of time just 12 00:00:25,980 --> 00:00:27,870 to do that instead of having it be 10 points. 13 00:00:27,870 --> 00:00:29,020 So why do we go over to the code. 14 00:00:29,020 --> 00:00:31,500 This isn't going to be a terribly difficult thing to add. 15 00:00:31,500 --> 00:00:34,300 What we will need, like I said again, is a victory state. 16 00:00:34,300 --> 00:00:37,680 And a victory state is just going to be whenever player one score or player 10 17 00:00:37,680 --> 00:00:39,430 score is equal to 10. 18 00:00:39,430 --> 00:00:41,460 So what we're going to go ahead and do first 19 00:00:41,460 --> 00:00:44,790 is I have a display score function here that I've also 20 00:00:44,790 --> 00:00:48,670 separated out in the distro in case you want to take a look at that. 21 00:00:48,670 --> 00:00:51,420 But let's go ahead and add another if condition 22 00:00:51,420 --> 00:00:56,087 here to the draw sort of side, the love.draw 23 00:00:56,087 --> 00:00:57,420 function that we've implemented. 24 00:00:57,420 --> 00:01:02,640 So else if game state is equal to victory, 25 00:01:02,640 --> 00:01:06,960 then we're going to draw a victory message. 26 00:01:06,960 --> 00:01:10,205 Now first of all, we need to do is set the victory stage 27 00:01:10,205 --> 00:01:12,330 somewhere in our code for this to actually trigger. 28 00:01:12,330 --> 00:01:17,970 So I'm going to go up to the update function where people get points. 29 00:01:17,970 --> 00:01:19,960 Let's see that is over here. 30 00:01:19,960 --> 00:01:23,280 So if ball X is less than zero or it's greater than virtual width, 31 00:01:23,280 --> 00:01:26,400 then we end up having it be the case that someone scores. 32 00:01:26,400 --> 00:01:30,130 So in this case, we'll add a situation here. 33 00:01:30,130 --> 00:01:36,220 So we'll say if player 2 score is greater than or equal to 10. 34 00:01:36,220 --> 00:01:39,720 It will only mean only ever be 10 and we'll be greater than 10, 35 00:01:39,720 --> 00:01:43,530 but we'll set the game state to be victory. 36 00:01:43,530 --> 00:01:49,860 Else, we'll take this line here, and just plug it in right there. 37 00:01:49,860 --> 00:01:53,010 So if it's 10 then they'll get set to the victory state, 38 00:01:53,010 --> 00:01:55,240 if not they'll get set to the serve state. 39 00:01:55,240 --> 00:01:57,520 And I want to do the exact same thing over here. 40 00:01:57,520 --> 00:02:02,630 So I'm going to do is just get rid of this, 41 00:02:02,630 --> 00:02:06,835 and set that to the player one score not the player two score. 42 00:02:06,835 --> 00:02:07,460 So that's good. 43 00:02:07,460 --> 00:02:10,280 So the game state should be set to victory Additionally, 44 00:02:10,280 --> 00:02:13,257 we're probably going to want to say who the winning player was. 45 00:02:13,257 --> 00:02:14,840 So I'm going to create a new variable. 46 00:02:14,840 --> 00:02:16,790 I'm actually going to come up here to the top. 47 00:02:16,790 --> 00:02:19,763 Underneath serving player, I'm going to say winning player 48 00:02:19,763 --> 00:02:22,930 and I missed that it's a zero for right now because no one has actually won. 49 00:02:22,930 --> 00:02:23,980 I'm gonna save it. 50 00:02:23,980 --> 00:02:27,890 I'm gonna come back down to where I just added the victory state code, 51 00:02:27,890 --> 00:02:31,310 I'm going to say winning player is equal to 2, 52 00:02:31,310 --> 00:02:34,220 in the case that player 2 score is greater than or equal to 10. 53 00:02:34,220 --> 00:02:37,095 And in the case that player one score is greater than or equal to 10, 54 00:02:37,095 --> 00:02:39,730 I'm gonna set that to winning player is equal to one. 55 00:02:39,730 --> 00:02:45,380 Let's go down back to the bottom where we have the print messages right here. 56 00:02:45,380 --> 00:02:47,720 And what I want to do is, like I showed in the slide, 57 00:02:47,720 --> 00:02:50,678 there's maybe a little bit of a larger header and then a smaller header 58 00:02:50,678 --> 00:02:54,440 that says first off that Player X 1, and then the smaller header that 59 00:02:54,440 --> 00:02:56,900 says press Enter to restart. 60 00:02:56,900 --> 00:03:01,530 So what I'm going to do is say love.graphics.setfont 61 00:03:01,530 --> 00:03:04,280 and let's say that we're going to have a font called victory font. 62 00:03:04,280 --> 00:03:06,690 We haven't created it yet, but we will very shortly. 63 00:03:06,690 --> 00:03:12,050 I want to say love.graphics.printf, and let's just take this code here. 64 00:03:12,050 --> 00:03:16,850 Copy it, print it, right here, player plus 65 00:03:16,850 --> 00:03:21,590 dot dot, the string of the not serving player but the winning player, 66 00:03:21,590 --> 00:03:24,812 and then I'm going to save space wins. 67 00:03:24,812 --> 00:03:27,770 And then we're going to go ahead and just say that's going to be at 10, 68 00:03:27,770 --> 00:03:30,770 and then it's going to be virtual width and center et cetera, et cetera. 69 00:03:30,770 --> 00:03:33,590 Then we'll take the next message for press Enter to serve 70 00:03:33,590 --> 00:03:36,810 and let's assume that our font is going to be 24 pixels. 71 00:03:36,810 --> 00:03:38,690 So what we need to do is instead of setting 72 00:03:38,690 --> 00:03:40,610 it 10 pixels below the other one, we need 73 00:03:40,610 --> 00:03:45,080 to set it at least 24 pixels, actually 24 plus 8. 74 00:03:45,080 --> 00:03:48,290 So let's just say 32 pixels below 10. 75 00:03:48,290 --> 00:03:53,315 So let's just say that that's going to be at 42, just like that. 76 00:03:53,315 --> 00:03:55,440 And we'll test that out just to make sure it works. 77 00:03:55,440 --> 00:03:58,910 We might have to finagle it a little bit to make sure it actually works well. 78 00:03:58,910 --> 00:04:01,850 And that's it for the victory message that we have. 79 00:04:01,850 --> 00:04:04,760 So again, the next thing we need to do is actually 80 00:04:04,760 --> 00:04:08,390 set such that when the user presses enter during the victory state, 81 00:04:08,390 --> 00:04:10,060 it resets the game. 82 00:04:10,060 --> 00:04:11,810 Because currently it's going to sort of be 83 00:04:11,810 --> 00:04:14,630 like almost like a serve state or a start state in and of itself, 84 00:04:14,630 --> 00:04:16,550 but just showing the victory message. 85 00:04:16,550 --> 00:04:20,390 So we'll say in the event that enter or return 86 00:04:20,390 --> 00:04:24,380 is pressed, if the game state is start, then we need to set it to serve. 87 00:04:24,380 --> 00:04:30,410 Else if game state is equal to victory, then we'll 88 00:04:30,410 --> 00:04:35,480 set the game state equal to to start actually. 89 00:04:35,480 --> 00:04:37,590 We'll start the game from the very beginning. 90 00:04:37,590 --> 00:04:40,130 So it'll show who disturbing player actually is, 91 00:04:40,130 --> 00:04:42,690 and then that should be most of the code. 92 00:04:42,690 --> 00:04:45,140 So let's go ahead, and actually one more thing too, 93 00:04:45,140 --> 00:04:49,862 instead of setting the square root check for 10 and 2, I'm a little bit, 94 00:04:49,862 --> 00:04:52,070 scored a little bit above where I wanted to go, let's 95 00:04:52,070 --> 00:04:55,880 just set it to three for now just for testing purposes. 96 00:04:55,880 --> 00:05:00,530 Set to 3, do that, and let's go ahead and run this. 97 00:05:00,530 --> 00:05:02,300 So we have everything looking normal. 98 00:05:02,300 --> 00:05:03,570 We have the FPS marker. 99 00:05:03,570 --> 00:05:06,470 Let's go ahead, player one serves, just going to go to the right. 100 00:05:06,470 --> 00:05:10,100 Player two serves, and then eventually we're going to have player, 101 00:05:10,100 --> 00:05:13,370 looks like player one is going to win. 102 00:05:13,370 --> 00:05:16,280 Oh, and we have, right we forgot to create the victory font. 103 00:05:16,280 --> 00:05:17,340 OK, that is my bad. 104 00:05:17,340 --> 00:05:18,820 Let's go ahead and do that really quickly. 105 00:05:18,820 --> 00:05:20,653 Where we have the other fonts, I'm gonna set 106 00:05:20,653 --> 00:05:25,050 create a victory font is equal to love.graphics.newfont. 107 00:05:25,050 --> 00:05:28,940 108 00:05:28,940 --> 00:05:31,520 And it's gonna be font.ttf.size24. 109 00:05:31,520 --> 00:05:32,780 Save that. 110 00:05:32,780 --> 00:05:35,660 And we are setting, actually no we're not 111 00:05:35,660 --> 00:05:39,350 setting the font I don't think back to the normal one afterwards. 112 00:05:39,350 --> 00:05:40,700 So we set it to victory font. 113 00:05:40,700 --> 00:05:42,590 We do the player something wins. 114 00:05:42,590 --> 00:05:47,660 Then we need to set love.graphics. setfont to the small font 115 00:05:47,660 --> 00:05:50,570 for the message after that let's run that again. 116 00:05:50,570 --> 00:05:52,830 And we'll painstakingly do this one more time. 117 00:05:52,830 --> 00:05:53,520 I apologize. 118 00:05:53,520 --> 00:05:57,230 But thankfully we set it to three and not 10, so this shouldn't be too bad. 119 00:05:57,230 --> 00:05:59,453 And boom, player one wins. 120 00:05:59,453 --> 00:06:02,120 And then we see we have the 3 and the 2 there we can still move. 121 00:06:02,120 --> 00:06:05,020 If I press Enter, it gets back to the start state. 122 00:06:05,020 --> 00:06:06,770 And If I press Enter one more time. 123 00:06:06,770 --> 00:06:09,740 Oh what I'm realizing actually, we forgot to set the scores back 124 00:06:09,740 --> 00:06:11,747 to 0 0, very important piece. 125 00:06:11,747 --> 00:06:13,080 So why don't we do that as well. 126 00:06:13,080 --> 00:06:19,640 Let's go over to the victory state over where it says, oh, it's 127 00:06:19,640 --> 00:06:22,130 in love.key pressed, key handler here. 128 00:06:22,130 --> 00:06:24,170 So we're in the victory, press start, player 129 00:06:24,170 --> 00:06:26,390 one score is going to be equal to zero. 130 00:06:26,390 --> 00:06:28,870 Player two score is equal to zero. 131 00:06:28,870 --> 00:06:32,720 Let's try it one more time, fingers crossed this should be everything. 132 00:06:32,720 --> 00:06:33,696 OK. 133 00:06:33,696 --> 00:06:37,460 1,0, 1,2, 2, 1, 2, 2. 134 00:06:37,460 --> 00:06:42,890 And finally, 3 2, press Enter and the scores get reset back to 0 0. 135 00:06:42,890 --> 00:06:46,010 So that's it for the victory update. 136 00:06:46,010 --> 00:06:48,950 Everything is more or less there in terms of game functionality. 137 00:06:48,950 --> 00:06:50,958 We have two very simple updates coming up. 138 00:06:50,958 --> 00:06:53,000 Actually the next update is probably my favorite. 139 00:06:53,000 --> 00:06:54,370 It's the audio update we're actually going 140 00:06:54,370 --> 00:06:57,260 to have sound effects working with the ball hitting the paddle, 141 00:06:57,260 --> 00:06:58,635 and with getting a score upgrade. 142 00:06:58,635 --> 00:07:01,885 And the very last minute can be a very minor thing I had to resize the canvas, 143 00:07:01,885 --> 00:07:04,680 but the core functionality for Pong is here, it's very exciting. 144 00:07:04,680 --> 00:07:08,500 I'll see you on the next video for Pong 11, the audio update. 145 00:07:08,500 --> 00:07:09,000