1 00:00:00,000 --> 00:00:01,000 COLTON OGDEN: All right. 2 00:00:01,000 --> 00:00:03,242 So with Pong 7, we actually took a big step forward. 3 00:00:03,242 --> 00:00:04,950 We implemented collision detection, which 4 00:00:04,950 --> 00:00:07,110 is a huge buzzword, especially in game programming. 5 00:00:07,110 --> 00:00:08,568 But it turns out, it's very simple. 6 00:00:08,568 --> 00:00:11,160 If you have just simple rectangles and you're using aligned 7 00:00:11,160 --> 00:00:13,890 axis bounding box collision detection, which we were doing, 8 00:00:13,890 --> 00:00:16,440 or AABB, just a few lines of code, and now we 9 00:00:16,440 --> 00:00:18,360 have the ball bouncing off of our paddles 10 00:00:18,360 --> 00:00:20,845 and also from the top and bottom of the screen. 11 00:00:20,845 --> 00:00:22,470 But you didn't have to use AABB before. 12 00:00:22,470 --> 00:00:24,595 Fortunately, we can just check to see whether the y 13 00:00:24,595 --> 00:00:26,790 value is above or below some amount. 14 00:00:26,790 --> 00:00:30,330 Now, I realize, actually, also, in the last couple of examples, 15 00:00:30,330 --> 00:00:33,450 I've been meaning to add the love.window.settitle. 16 00:00:33,450 --> 00:00:36,090 So right now, before I actually get into this update, 17 00:00:36,090 --> 00:00:38,790 love.window.settitle of Pong. 18 00:00:38,790 --> 00:00:41,760 Run it, and it's white again, of course. 19 00:00:41,760 --> 00:00:51,646 So let's go ahead and change this down here to 255, 255, 255, and 255. 20 00:00:51,646 --> 00:00:53,310 Run it, and boom. 21 00:00:53,310 --> 00:00:57,960 Now we have a functional Pong title. 22 00:00:57,960 --> 00:00:58,510 So great. 23 00:00:58,510 --> 00:00:59,320 Told you I was going to do it. 24 00:00:59,320 --> 00:01:01,750 I knew that someone was going to comment on it and make fun of me for it, 25 00:01:01,750 --> 00:01:02,390 and that's totally fine. 26 00:01:02,390 --> 00:01:02,940 I totally deserve it. 27 00:01:02,940 --> 00:01:04,769 But anyway, Pong 8, the score update. 28 00:01:04,769 --> 00:01:07,920 So in this example, we're going to actually implement adding score. 29 00:01:07,920 --> 00:01:10,350 So not only do we just have these static 1 30 00:01:10,350 --> 00:01:14,140 and 0 on the left, which is fitting for a computer science class, 31 00:01:14,140 --> 00:01:16,860 but when the ball goes to the left and the right of the paddles, 32 00:01:16,860 --> 00:01:19,800 if it goes beyond the edges, the right and the left edge, 33 00:01:19,800 --> 00:01:21,790 we want to increment the corresponding score. 34 00:01:21,790 --> 00:01:25,110 So if the ball goes to the left edge of the screen, 35 00:01:25,110 --> 00:01:27,300 the right number should increment, and vice versa. 36 00:01:27,300 --> 00:01:29,425 If the ball goes past the right edge of the screen, 37 00:01:29,425 --> 00:01:30,890 the left number should increment. 38 00:01:30,890 --> 00:01:33,098 And that's all the slides we have for this, actually. 39 00:01:33,098 --> 00:01:36,250 It's going to be a very straightforward, I think, bit of code here. 40 00:01:36,250 --> 00:01:40,060 So let's go ahead and implement this in the update function. 41 00:01:40,060 --> 00:01:41,560 So let's go up to the very top here. 42 00:01:41,560 --> 00:01:43,800 So if the game state is play-- now, we are going to have start, 43 00:01:43,800 --> 00:01:45,050 and we are going to have play. 44 00:01:45,050 --> 00:01:47,730 And actually, what I want to do is go to the very bottom. 45 00:01:47,730 --> 00:01:50,580 I want to delete this game state equals start thing. 46 00:01:50,580 --> 00:01:52,730 We don't really need that anymore. 47 00:01:52,730 --> 00:01:54,150 I'm to go back up here. 48 00:01:54,150 --> 00:01:57,390 And towards the top, when we're in the play state-- 49 00:01:57,390 --> 00:02:01,080 I can really do this anywhere, but I'm going to just put it here. 50 00:02:01,080 --> 00:02:09,030 So I'm going to say, if the ball.x is less than or equal to 0, 51 00:02:09,030 --> 00:02:13,000 then we want the left-- 52 00:02:13,000 --> 00:02:13,500 sorry. 53 00:02:13,500 --> 00:02:18,420 If the ball is less than 0, we want the right paddle score to increment. 54 00:02:18,420 --> 00:02:22,563 So player 2 score should be equal to player 2 score plus 1. 55 00:02:22,563 --> 00:02:24,480 Now, you might be looking at this and thinking 56 00:02:24,480 --> 00:02:25,772 that this looks a little weird. 57 00:02:25,772 --> 00:02:28,860 Why are we using player 2 score equals player 2 score plus 1? 58 00:02:28,860 --> 00:02:30,000 Can't we use plus equals 1? 59 00:02:30,000 --> 00:02:31,770 Can't we use plus plus? 60 00:02:31,770 --> 00:02:34,985 And unfortunately, Lua is a bit strange in that it doesn't actually 61 00:02:34,985 --> 00:02:36,360 let you do that shorthand syntax. 62 00:02:36,360 --> 00:02:40,440 It will only allow you to do the equals itself plus something. 63 00:02:40,440 --> 00:02:43,920 There is no shorthand plus equals or plus plus, unfortunately. 64 00:02:43,920 --> 00:02:45,390 But we can deal with that. 65 00:02:45,390 --> 00:02:47,392 Not a terrible inconvenience. 66 00:02:47,392 --> 00:02:49,350 We're going to do the same thing for whether it 67 00:02:49,350 --> 00:02:50,975 goes past the right edge of the screen. 68 00:02:50,975 --> 00:02:55,890 So if ball.x is greater than or equal to virtual width minus 4, then-- 69 00:02:55,890 --> 00:03:00,060 and remember, we're accounting for that size of the rectangle there-- 70 00:03:00,060 --> 00:03:04,620 we're going to say player 1 score is equal to player 1 score plus 1. 71 00:03:04,620 --> 00:03:08,130 And I think I have declared those score variables already, 72 00:03:08,130 --> 00:03:09,300 which I have indeed done. 73 00:03:09,300 --> 00:03:11,100 They're set to 0 initially. 74 00:03:11,100 --> 00:03:15,030 Now, once it is the case that we have added to the score, 75 00:03:15,030 --> 00:03:16,770 we want to actually reset the ball. 76 00:03:16,770 --> 00:03:19,830 So what I can do is I can say, ball reset, 77 00:03:19,830 --> 00:03:24,133 and we'll say game state is equal to start so that we can give them a chance 78 00:03:24,133 --> 00:03:27,300 to press Enter again once someone has scored so that it just doesn't happen, 79 00:03:27,300 --> 00:03:29,592 and they're caught off-guard, like, oh, we just scored. 80 00:03:29,592 --> 00:03:32,203 Now the ball's in the center and it's moving. 81 00:03:32,203 --> 00:03:34,620 We're going to add that little layer, that little pressing 82 00:03:34,620 --> 00:03:35,850 Enter little buffer there. 83 00:03:35,850 --> 00:03:38,970 So game state is equal to start. 84 00:03:38,970 --> 00:03:40,630 Save it. 85 00:03:40,630 --> 00:03:42,160 And then that should be it. 86 00:03:42,160 --> 00:03:44,190 So let's go ahead, and let's make sure also 87 00:03:44,190 --> 00:03:46,440 that we are accounting in the love.key press 88 00:03:46,440 --> 00:03:49,030 function for if they press Return. 89 00:03:49,030 --> 00:03:49,530 Cool. 90 00:03:49,530 --> 00:03:51,270 So we can have this here. 91 00:03:51,270 --> 00:03:52,170 This is fine. 92 00:03:52,170 --> 00:03:56,432 But in reality, all we really need to do is, if game state is equal to start, 93 00:03:56,432 --> 00:03:57,890 game state should be equal to play. 94 00:03:57,890 --> 00:03:58,860 So that's what we're going to do. 95 00:03:58,860 --> 00:04:00,750 We're going to set that to end because now we 96 00:04:00,750 --> 00:04:03,083 don't have to press Enter to go back to the start state. 97 00:04:03,083 --> 00:04:05,760 We can just let the ball go past the left or the right edge. 98 00:04:05,760 --> 00:04:07,800 So I'm going to go ahead and run this. 99 00:04:07,800 --> 00:04:09,885 And let's just give it a test. 100 00:04:09,885 --> 00:04:11,010 Let it bounce off the side. 101 00:04:11,010 --> 00:04:12,510 I will not purposely do that. 102 00:04:12,510 --> 00:04:13,580 And perfect. 103 00:04:13,580 --> 00:04:16,529 It ended getting up one incremented on the right player 104 00:04:16,529 --> 00:04:18,743 because it went past the left edge, which is great. 105 00:04:18,743 --> 00:04:20,160 So let's go ahead and do it again. 106 00:04:20,160 --> 00:04:21,743 I'm going to purposefully hit it back. 107 00:04:21,743 --> 00:04:28,290 Let's just get the other player to slowly get a point onto them, 108 00:04:28,290 --> 00:04:31,490 just to make sure that the left score is working as well. 109 00:04:31,490 --> 00:04:33,060 Got to try not to do it. 110 00:04:33,060 --> 00:04:33,567 And perfect. 111 00:04:33,567 --> 00:04:35,400 So we not only have the scores incrementing, 112 00:04:35,400 --> 00:04:38,317 but the ball is getting reset in the middle of the screen accordingly. 113 00:04:38,317 --> 00:04:42,150 And we made sure to set the title in this episode as well. 114 00:04:42,150 --> 00:04:43,350 So that was it for Pong 8. 115 00:04:43,350 --> 00:04:45,588 Very simple update-- just adding the ability to keep 116 00:04:45,588 --> 00:04:47,130 score on the left and the right side. 117 00:04:47,130 --> 00:04:50,610 We don't actually have keeping track of whether one player has won or not. 118 00:04:50,610 --> 00:04:52,010 We will take care of that soon. 119 00:04:52,010 --> 00:04:54,010 In the next update, we're going to actually take 120 00:04:54,010 --> 00:04:58,200 care of making sure that if player 1 scores, for example, onto player 2, 121 00:04:58,200 --> 00:05:01,710 player 2 gets to serve the ball back to player 1 out of fairness. 122 00:05:01,710 --> 00:05:04,050 We need to keep track of who actually got scored on 123 00:05:04,050 --> 00:05:07,530 and then send the ball in the opposite direction in that circumstance. 124 00:05:07,530 --> 00:05:10,500 We'll see you for Pong 9. 125 00:05:10,500 --> 00:05:11,000