1 00:00:00,000 --> 00:00:00,140 2 00:00:00,140 --> 00:00:01,140 COLTON OGDEN: All right. 3 00:00:01,140 --> 00:00:02,793 Pong 5 was a bit more work. 4 00:00:02,793 --> 00:00:04,710 We ended up actually refactoring our code base 5 00:00:04,710 --> 00:00:07,320 to use object-oriented programming or a little taste 6 00:00:07,320 --> 00:00:10,710 there of, a little bit different than having a bunch of variables 7 00:00:10,710 --> 00:00:14,070 keeping track, a little more procedural style as before, 8 00:00:14,070 --> 00:00:15,923 much like how C actually is written. 9 00:00:15,923 --> 00:00:18,840 But the way we structure things now is more akin to what you might see 10 00:00:18,840 --> 00:00:21,682 in Java, or what you might see in C#, or even in Python, 11 00:00:21,682 --> 00:00:23,640 depending on what code base you're working on-- 12 00:00:23,640 --> 00:00:26,432 a little bit easier to keep track of multiple things moving around. 13 00:00:26,432 --> 00:00:30,745 And games in particular are very sort of prone to this model of programming. 14 00:00:30,745 --> 00:00:32,245 Now this is a little bit of a break. 15 00:00:32,245 --> 00:00:36,300 It's a little bit of a simpler iteration in the Pong track. 16 00:00:36,300 --> 00:00:39,630 We're going to be taking a look just at implementing a simple FPS 17 00:00:39,630 --> 00:00:41,245 marker at the top left of the screen. 18 00:00:41,245 --> 00:00:44,120 And to do that, you can see there it's written in green text, usually 19 00:00:44,120 --> 00:00:47,085 the same font, same size as the text at the top of the screen. 20 00:00:47,085 --> 00:00:49,710 But this is literally just going to use a simple function built 21 00:00:49,710 --> 00:00:52,622 into love that just tells us what our frame rate is currently at. 22 00:00:52,622 --> 00:00:54,330 And so here are a couple of the functions 23 00:00:54,330 --> 00:00:57,940 in particular that we're going to look at, so love.window.setTitle. 24 00:00:57,940 --> 00:01:00,760 So this one is not germane to what we've just talked about, 25 00:01:00,760 --> 00:01:04,739 but if you've noticed, so far at the very top we've had untitled. 26 00:01:04,739 --> 00:01:06,992 But now, as is shown in this particular example, 27 00:01:06,992 --> 00:01:08,950 we have the word "Pong" actually written there. 28 00:01:08,950 --> 00:01:10,740 So this is a little bit of a cleanup, just something that 29 00:01:10,740 --> 00:01:13,720 looks better if you're running your game in a windowed setting. 30 00:01:13,720 --> 00:01:14,520 So we're going to be doing that. 31 00:01:14,520 --> 00:01:17,640 It literally just sets the title to a string, whatever you want it to be. 32 00:01:17,640 --> 00:01:21,640 More germane to what we've talked about is love.timer.getFPS, 33 00:01:21,640 --> 00:01:26,610 which is just going to return as a float the FPS value of our game-- 34 00:01:26,610 --> 00:01:28,170 so very simple, very straightforward. 35 00:01:28,170 --> 00:01:28,980 And that's pretty much it. 36 00:01:28,980 --> 00:01:31,060 So let's actually go right into the code here. 37 00:01:31,060 --> 00:01:35,078 So what I'm going to do is I'm going to scroll down to the very bottom. 38 00:01:35,078 --> 00:01:37,620 And what we're going to do is just render this to the screen. 39 00:01:37,620 --> 00:01:40,578 So we know we want a string rendered at the top left. 40 00:01:40,578 --> 00:01:43,620 All I'm going to do really is I just need to insert something right here. 41 00:01:43,620 --> 00:01:46,860 Basically after everything else is rendered, I want to display the FPS. 42 00:01:46,860 --> 00:01:50,370 Now I don't want to get into the habit of just writing a bunch of code 43 00:01:50,370 --> 00:01:51,780 in place inside these methods. 44 00:01:51,780 --> 00:01:55,030 I think this would be more suited to a function. 45 00:01:55,030 --> 00:01:58,662 So let's say I want to display FPS like that. 46 00:01:58,662 --> 00:02:01,620 I'm calling it, like you might have seen in C, just a very simple, sort 47 00:02:01,620 --> 00:02:03,090 of procedural function call. 48 00:02:03,090 --> 00:02:06,130 Because what I'm going to do is just write it here in main.lua itself. 49 00:02:06,130 --> 00:02:09,352 So I'm going to say function displayFPS. 50 00:02:09,352 --> 00:02:11,310 I'm not even going to have it take a parameter. 51 00:02:11,310 --> 00:02:13,018 This is going to be very straightforward. 52 00:02:13,018 --> 00:02:15,465 I'm going to say love.graphics.setColor. 53 00:02:15,465 --> 00:02:18,090 And this, actually, I don't think we've taken a look at either. 54 00:02:18,090 --> 00:02:20,880 We've taken a look at love.graphics.clear. 55 00:02:20,880 --> 00:02:24,840 Love.graphics.setColor is very similar to that, 56 00:02:24,840 --> 00:02:27,810 only this actually toggles the color for things 57 00:02:27,810 --> 00:02:30,600 like drawing text to the screen, which by default, everything, 58 00:02:30,600 --> 00:02:32,370 as you've noticed, is white. 59 00:02:32,370 --> 00:02:34,530 So we're going to say love.graphics.setColor. 60 00:02:34,530 --> 00:02:37,770 I want this to be green, so, again, it's RGB alpha. 61 00:02:37,770 --> 00:02:39,930 So the G component is what we care about. 62 00:02:39,930 --> 00:02:42,130 So I'm going to say 0 for the red. 63 00:02:42,130 --> 00:02:44,190 Let's say 255-- well, rather-- 64 00:02:44,190 --> 00:02:45,570 we'll skip the 255 stuff. 65 00:02:45,570 --> 00:02:51,000 We'll just say 0,1 for full green, 0 for the blue, and 1 for the alpha. 66 00:02:51,000 --> 00:02:54,390 Then I'm going to say love.graphics.setFont 67 00:02:54,390 --> 00:02:58,560 of smallfont and love.graphics.print. 68 00:02:58,560 --> 00:03:01,710 69 00:03:01,710 --> 00:03:05,220 And then we'll say our FPS is going to be-- 70 00:03:05,220 --> 00:03:06,480 well, we need to get the FPS. 71 00:03:06,480 --> 00:03:13,950 So in order to do that, I'm going to say tostring love.timer.getFPS 72 00:03:13,950 --> 00:03:14,762 as a function call. 73 00:03:14,762 --> 00:03:17,220 And we need this tostring method-- this is very important-- 74 00:03:17,220 --> 00:03:21,000 because this is going to actually give us an integer value. 75 00:03:21,000 --> 00:03:25,260 And this value is going to be incompatible with adding to a string. 76 00:03:25,260 --> 00:03:28,210 You might actually notice that we have this dot dot here. 77 00:03:28,210 --> 00:03:31,590 This dot dot is the string concatenation operator. 78 00:03:31,590 --> 00:03:34,125 So this just means we add two strings together, one 79 00:03:34,125 --> 00:03:36,000 and then tail it at the end of the other one. 80 00:03:36,000 --> 00:03:39,580 You probably touched on this in C throughout the course. 81 00:03:39,580 --> 00:03:42,340 Now it's a different operator in Lua. 82 00:03:42,340 --> 00:03:44,130 In Python, it's a plus. 83 00:03:44,130 --> 00:03:46,560 And in C-- well, you can't really do that with C. You 84 00:03:46,560 --> 00:03:48,720 have to use something else altogether. 85 00:03:48,720 --> 00:03:53,610 But what we're going to do is say FPS, concatenate with that. 86 00:03:53,610 --> 00:03:56,850 And then we're going to-- let's just draw this, let's say, at 40. 87 00:03:56,850 --> 00:03:57,900 And for-- actually, no. 88 00:03:57,900 --> 00:04:01,380 We'll do it at, yeah, 40 and 20. 89 00:04:01,380 --> 00:04:02,720 And that's it. 90 00:04:02,720 --> 00:04:05,670 And then one last thing I do want to do actually 91 00:04:05,670 --> 00:04:08,005 is set everything back to white as the default 92 00:04:08,005 --> 00:04:10,380 color for whenever it draws the next thing-- the paddles, 93 00:04:10,380 --> 00:04:11,590 and the text, and whatnot. 94 00:04:11,590 --> 00:04:14,250 So I'm going to say 1, 1, 1, 1 at the end of that. 95 00:04:14,250 --> 00:04:17,430 And if we run the code here, we do indeed 96 00:04:17,430 --> 00:04:21,930 see we have an FPS display at the top of the screen. 97 00:04:21,930 --> 00:04:23,470 And it started off at 49. 98 00:04:23,470 --> 00:04:24,928 It's a little bit of a weird thing. 99 00:04:24,928 --> 00:04:27,090 Love actually has to get a little bit of frame data 100 00:04:27,090 --> 00:04:29,310 so that it can calculate what the FPS is, because it actually 101 00:04:29,310 --> 00:04:30,750 tracks it across multiple frames. 102 00:04:30,750 --> 00:04:35,130 That's why at first it was 0, then 49, and now it is smoothly at 60. 103 00:04:35,130 --> 00:04:39,000 Thankfully, my computer is strong enough to run this very simple, barely 104 00:04:39,000 --> 00:04:40,980 functioning Pong example. 105 00:04:40,980 --> 00:04:43,290 So that was it for Pong 6, very simple. 106 00:04:43,290 --> 00:04:46,950 In the next update, we're going to take something a little bit meatier 107 00:04:46,950 --> 00:04:48,660 on our hands, the collision update. 108 00:04:48,660 --> 00:04:51,938 So so far, the ball does not actually collide with the paddle at all. 109 00:04:51,938 --> 00:04:53,730 It just will go straight through because we 110 00:04:53,730 --> 00:04:57,060 have no code to test for whether the ball and the paddle have intersected. 111 00:04:57,060 --> 00:05:00,310 So we're going to add this major change to the code base in the next section 112 00:05:00,310 --> 00:05:03,500 and start talking about collision with Pong 7. 113 00:05:03,500 --> 00:05:04,000