字幕列表 影片播放 列印英文字幕 COLTON OGDEN: Hello, world. This is CS50 Twitch. My name is Colton Ogden. And happy 2019. Happy new year to everybody. Whether you're new or a regular from last year, welcome to our Twitch channel where we do sort of live coding, demos, tutorials, explanations, and all-- excuse me-- all kinds of stuff. We might even get into some hardware-related stuff if some of the staff is privy to that sort of thing. We've got a bunch of people in the chat. I'm going to say hello as usual to everybody who's here with us already. And let me know if I sound OK. We just completely re-setup our stream setup. We took all the stuff that we had setup in this room, on our streaming room last year in December, we took it all, basically, and used it for the fair, brought it back, and then reset a lot of it up today. So hopefully everything looks and sounds like it did last year if not better. But definitely let me know if not. I'm going to go through the chat here really fast, just to see who joined us here to begin with. Looks like MKloppenburg, a regular from last year when I was doing a little test message. This test received. [? BavikKnight, ?] another regular. Hey, everyone. Hi. How is everybody? Happy 2019 to everyone. Happy 2019 to you as well. [? SewerTon, ?] who just followed, actually. Thank you very much for the follow. Hello, CS50 family with the popcorn emoji. They say-- Buddy2610, sup? Nice. Oh, I can't read that name at all. The Gangus-- oh, that's the GangusGoat19. Also thank you for the follow. I see GangusGoat19 just follow as well. Thanks to everybody who followed over the break. It looks like there are about 200 people that followed while we were not streaming over the winter break. So I was in California. I was far away from Boston over the winter for the last three or four weeks or so visiting family. Back in Boston, it's very cold today. It was fairly nice temperature for the last week or so, and now it's, like, 20 degrees today. It's freezing. [? KiraU, ?] with the two clown faces, thank you. [INAUDIBLE] smiley faces. TuxMan29 [? int ?] new year, last year. Return last year plus equals 1. That's TuxMan29, a little bit-- a little code snippet for everybody to increment the year. And then [? SuraTon ?] is another thing. It's basically saying, if last year is null, age is 1, right? Because if we weren't any year-- if we weren't any age last year, null is not a valid a valid age, so we need to be at least 1, right? [? BellaCures, ?] happy 2019. [? ForSunlight, ?] hello. Colton, the regulars, and all, thank you very much. Let me see, make sure I'm not missing anybody. Didn't do the assignments yet, but watched most of it and worked with [? Django. ?] Oh, that's for the-- talking about Brian's web dev class, yes. Brian actually may be in the chat today. He and I chatted before the stream started. [? SerotonOFX. ?] Hi, Johnny Bravo. [? OFX is ?] surely a reference to my hair. It is a little Johnny Bravo looking. I need to get a haircut, like, super badly. I haven't gotten it cut for, like, a month. It's pretty insane. Man, some of these names are hard to read because Twitch puts them in this really obnoxious blue font on black. M [INAUDIBLE], hi, Colton. I hope I'm pronouncing that right. I'm probably not pronouncing that right. Sounds good. Hello, regulars. Hello, everybody. OK, awesome. So I think I got everybody that was in the chat. Thank you all so much for tuning in. Looks like my head's getting cut off. I think the camera might be a little too zoomed in, but that's OK. Today's stream is on Space Invaders. Let me go ahead and switch my screen to the full view here. And this is sort of where we left off last year. And I noticed that the GitHub repo was actually from November. So if you're in-- if you want to basically get the code base that we work with last November, I'll write the URL here in the chat. Actually, if I do that it's going to screw up the chat overlay. Basically, it's Coltonoscopy-- GitHub.com/coltonoscopy, that's my GitHub handle, slash space dash invaders dash stream. And then that will give you the code base that we worked with last year in November where we basically had the following working. Let me go ahead and run this. So it's not really super feature rich yet. We do have a ship that can move around. And if you press spacebar, you shoot lasers. You shoot projectiles. But it doesn't do anything beyond that. The projectiles don't collide with the ships, and the ships are static. They're basically just sprites that are being rendered. And that's pretty much-- that's pretty much it. That's pretty much all that's going on. One second. But yeah, not a whole lot really going on there. But today's goals are-- we have a few goals, actually. First goal is actually get the ships to do something, to move around left and right. The next goal will be once they're moving to actually have the projectiles collide with the ships in order to trigger a death, essentially, and to give us some sort of score. And then another thing that we want to do is the game is currently in just a single state where we sort of start the game up, the ship's there, all the enemy ships are there, and we do stuff. But there's not really a title screen. There's not, like, a game over screen, anything like that. So I would say that our goals today are sort of triple faceted. We want to get the ships to move, get the ships to sort of come down towards the player, move left and right, which is how it worked in the original Space Invaders. Then we want to have the projectiles that we shoot from the player not only dispose of themselves after they've gone past the edge of the screen-- for performance reasons if we just let an unlimited number of projectiles exist when we shoot, eventually, we'll theoretically run out of memory. Although they would be very difficult to do that because the projectiles currently are so small. I'm not sure exactly how many bytes they take up offhand. But relatively few per projectile. But it could eventually end up slowing down the game. And they'll exist and they'll update every single frame. And really, the updating of the projectiles is probably going to be more of a performance issue than the Memory Cost because in our code, and I have to sort of remember how this all works. But we do have-- make sure it's an update. In our update function online 51 to 53, and I'll answer some of the questions in the chat in just a second. But on lines 51 to 53, we have this sort of for loop that says for every projectile in a table of projectiles that we have created in advance, an array of projectiles, you can think of it as, we want to call this update functions. Update function essentially just moves a projectile up or down depending on its orientation, whether it's going up, or it's going down, whether it's going towards the enemies, whether it's going towards the player. And so for every projectile we add to this array of projectiles, it's going to add more work that gets done in this loop. And eventually, this loop could get cumbersome and bring us down from 60 frames to maybe 58 frames, 40 frames. It would take a lot of projectiles to do that. I'm sure it would take thousands of projectiles, but it's something that feasibly could be done. So essentially, what we want to do is when a projectile goes beyond the edge of the screen if it doesn't hit an alien, we want to get rid of it. We want to just essentially set it to nil on the table or repurpose it. You can use what's called an object pool. We're just going to delete it, essentially, and then just create new ones as we press spacebar. And so we'll only ever have maybe 5 to 10 projectiles max on the screen at one time. But that's something that you want to take into consideration. If you're spawning objects, you want to de-spawn them, sort of like freeing memory in C. Whenever you alloc memory, malloc memory, you have to free it in order to prevent memory leaks. It's kind of the same idea, although a little bit more complicated in this sense, because you're actually dealing with things that are taking up CPU cycles that are moving and doing stuff every frame. But that's the projectile side of it. The ship side of it we just need to have a loop where the ships kind of move left and right and come down when they've moved all the way to the edge of the screen until they reach the player, essentially. And then the last part we'll be using what's called the state machine, which is a very important concept in game programming which allows us to divide up our game into multiple different states that function sort of as modules. For example, a title menu state, a game state, a game over state. Rather than put all this logic in our main.lua and bloat it unnecessarily, we can divide it up using a state machine class which we'll implement from scratch here a little bit. And this is a very effective technique for modularizing your code. And you can use it to not only create states for the game itself, but for individual entities within the game. For example, an entity might be in an attack state, or an idle state, or a moving state, anything that you can think of. If you can create a state for it and piece it out, it makes your life a lot easier when you're dealing with the actual editing of the code. And you can let other people, therefore, edit different states independently. [? BevIgnite is ?] saying, yeah, he's in the CS50-W course, the web course right now, on hold, concentrating on algorithms course of Princeton, taking on a massive load. KirbytheDonkey says, what language are you using? This is in Lua. So Lua is a scripting language. We are using Lua and also LOVE 2D, which is a framework and sort of an engine for developing 2D games using Lua. Lua is the language, LOVE 2D is the actual framework, the functions, and the runtime that allows us to do all of the things that we can, all the graphics, and sound, and input, and all that stuff. If you haven't downloaded it already, go ahead and go to love2D.org and grab the version most appropriate for your operating system. And also, watch the-- if you haven't seen this, for the first part of the stream, you might want to go back and look at part one because this is a continuation of that. And we're using that code and sort of building on it and adding the new features. But that's on our Twitch channel and our YouTube. You can look up CS50 on Twitch Space Invaders. You can go on our Twitch [? VODs ?] and pull it up there. But we have all of the stuff that we did in November of last year of the Space Invaders is part one on GitHub, the Space Invaders stream, so you can clone that code and also add the stuff that we're going to be talking about today to that. And TuxMan29 also said the same thing. Andre says, Unity's animator controller is essentially a state machine, although it does allow blending animation so not strictly a discrete state machine. Yes. And Andre [INAUDIBLE] actually made a clone of a game that I created for our games course using those animator controllers. If I'm not mistaken, you used those to sort of replicate that in Unity 2D, which was a really cool project.