How to Program a Game! (in Python)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what's up guys and girls in today's video we are going to go over how to program your first game in Python so in this video we'll be programming in Python 3 and then using sublime text 3 as our editor and by the end of this video you'll have a game that looks like this it's a very basic game where you're controlling the red block and you're trying to dodge these blue blocks that are falling and as the score increases the speed of the blocks increases as well so as you can see now it's getting a little faster getting a little harder and if I do ever collide with a blue block it says game over down here and your score so this is what you'll have completed by the end of this video and you should have some programming knowledge to do this video all the knowledge you need for this is in the first 5 Python tutorials I've posted on my youtube channel so if you check the description for those videos and watch through them all you'll be like more than prepared for this video and also if you haven't set up your development environment with sublime text in Python 3 I'll also have a link in the description to set up everything so you're ready to get going in this video alright in this video we're going to be using the PI game library of Python so if you don't have this already installed open up a command prompt if you're on Windows or a terminal if you're on Mac and type in the command pip install PI game and that should it install it properly if the the command pip isn't recognized you probably didn't install python properly so i recommend just rewatching my setup video uninstalling whatever you have and reinstalling and potentially if you're on Mac you might have to instead of typing pip install my game you'll have to type pip 3 install PI game so if the first command doesn't work for you try typing pip 3 in salt PI game okay once you have PI game installed open up a new Python file I just called mine game py and we'll go through the basic setup of the PI gate like whenever you do a PI game project the basic setup of that so the first line we always need to write is import PI game and we should run that and make sure it doesn't give us there if it did give you an error right there with that import make sure you install the PI game properly so maybe run through those pip commands again and make sure that they actually said PI game was installed and if you're on Mac remember you might have to do pip 3 instead of just pip install PI game ok so we have pi game that the first line we need to always do is initialize PI game so we do PI game done it and when I when I forget how to do this I usually just look it up online so if you look up like build a like building a PI game in Python this these like the basic commands you'll start your file out with will be provided by someone else on the internet so PI game dot an it run that still nothing is happening so we want to actually create a screen in PI game so to do a screen we can do screen equals PI game display dot set mode and then we pass in a tuple of width and height so the width would be like something like 800 and the height will say is something like 600 and this is in pixels so if I run that you should see a screen pop up and then quickly close and don't worry about it quickly closing we'll fix that in a few minutes so we have a screen of with 800 and height 600 pixels one thing I recommend you do is instead of directly passing in these numbers here let's initialize two variables that hold those values so we're gonna initialize a width variable which is equal to 800 and height variable which is equal to 600 and the reason we're doing this is because we'll want to probably use our width and height when we're building this game so if we set variables to equal the width and height then we can easily access those things throughout our game and also if we wanted to say one day change our width to like a thousand pixels if we have a variable it makes everything else easier because we don't have to switch every one of these values in our code we just have to switch it right here so let's say ok we'll go back to 800 with the width let's say we wanted the height 600 so we'll just pass in hug it right here so now if ever on that run you still the same screen pops up and then just closes because we haven't handled the closing condition yet okay so the next thing we want to do is develop our game loop so basically what is gonna happen with PI game is we're gonna have a loop that's just running while our games not over so we'll have like while not game over and then we'll have PI game is an event based programming library so we'll have a for loop inside of this while loop that will track all of our events so it's gonna look like something like this and as I said I'm whenever I'm making this from scratch I'm usually looking up that just kind of the basics online and then just kind of importing them in so don't worry about like having to memorize this so we're gonna do our instance a variable called game over is initially equal to false and then we're gonna have a loop that says while not game over so that's going to keep running until we hit the game over condition and what we want to do is have now a for loop which is going to get the events in PI game so for event in PI game dot event dot get and to start out I'm just going to print event so if I run this if you see down here at the bottom of my screen it's tracking everything I do on the screen and this is super helpful for us because now we can any action we do during our game we track it with this events and we can use it to help us program specific things to happen based off of certain events so this is super useful these events and notice there's like a bunch of different mouse motion mouse button up mouse button down key presses key like down key up let me think like you can do escape all sorts of different things you can do with events and then I'll close out and actually the closing out won't be handled right now properly so if the manually close it shoot I'll fix that in one sec so I'm just gonna cancel build up here so if you can't exit out just go up to tools cancel build or do control + break okay so the first event we have to handle and this happens in I think every PI game you're gonna make is we have to do one event type that's called the kind of quit event so if we do top we do in this four that we're gonna do if event dot type equals equals PI game quit that happens then we want to do a system exit so assist exit end will actually have to import this system library and that comes pre-installed on every Python version so now if I run this and I click this X right here it lets me quit out properly alright so that's our basic screen all right so now that we have our screen let's begin drawing shapes on our screen that will eventually move around and manipulate so whenever I need to know how to do something either do a Google search or I look up on this official PI game 0 r slash Doc's so this has all the different things you can do in PI game so I want to right now I want to draw something so I'm going to click on the draw section of the docs and I just want to begin by just drawing a rectangle it's the simplest shape so we'll begin with just drawing a rectangle so this gives us the command to draw a rectangle so I can do PI game rect to draw rect then I need to pass in something like this so this is how a rectangle is defined so let's do that real quick so let's we want to just draw a shape somewhere on our screen so I'm going to just do we want to do this outside of the event loop want to do it just at the same level as this for loop so outside of that but inside the wild not game over we want to do PI game draw rect and then if I look at the documentation one more time it takes in a surface so the surface is going to be our screen in this case the color this is going to be an RGB color value which RGB is just there's three values that each each have 0 to 255 and they together make a color so look into RGB colors if you haven't heard of them before it's pretty straightforward you don't really need to know too much about it and then the rectangle and the rectangle is defined by I think X location Y location width and height and then with so to figure out the rectangle real quick I'll go to rect and ya left top width height that's gonna be how we draw a rectangle so let's do this so PI game dot draw rect surfaces the screen next we need to do the color so we'll just say we want to draw a Red Square so to do red in PI game we do to 5500 that's an RGB for red because red has 255 in the green and blue slots have zeros which means no color so it's just gonna be red now we get into our rectangle and to define that we did left position top position and then width and height so we'll just do it somewhere in the middle of the screen so I'm going to say like 300 400 or actually live with you over 400 300 and then width will be like 50 and height would be like 50 and then finally the last part of the rectangle drawing the rectangle was the width and that's an optional parameter if it says equal to 0 that means optional so this is just the outline of our shape and I'm just not gonna bother with it so we'll just draw this ok so right now I'm drawing a rectangle but we actually don't see it on the screen and that's because I'm PI again we need to do a command we need to update our screen every iteration so I can do something like PI game dot display update and now we have that Red Square drawn in the center of our screen and a couple of things to note for good practice is we should instead of just defining these things inside of this let's try to make this a little neater and kind of allow someone reading our code to understand more what we're doing so for example with this red let's delete this and go up to the top of our screen and just define it as a like constant variable so red is equal to 255 0 0 and now when we're drawing the rectangle we could pass in red as opposed to that like a number that might not mean as much to people same thing for these 400 and 350 s instead of defining them just as numbers inside of this draw function let's give them variables that add meaning to what they are so we'll say we'll call a variable maybe player position so this is the Red Square is going to be our player and so we're gonna find a tuple that is the position of the player so that's going to be something like well we'll find it as a list that is 400 and 300 and we could even go a step farther we call this player act player Y but we'll just do 400 300 so when I was drawing this I could do player position index of 0 and player position this is from the indexing video the list video I have on Python 1 to get these two values and then that's also define a player size so player size is going to be equal to 50 both width and height will do make this a square for now so this should be player size and this next variable or this next value is going to be player size as well and it still works cool so we just made it neater so now I can read this code we can read this code easier than when it was just a bunch of numbers floating all over the place so what this is called in programming is we had initially a bunch of magic numbers floating around our screen and basically they're called magic numbers because someone reading our code might not know have any idea of where they come from but now that we define some of these variables it's a lot easier to read our code and understand what's actually happening here okay now let's get the movement happening so when we press a key we can move this block to the left and right so to do this we're gonna have to track the event that is mouse button down and mouse or not mouse button down but a key press down and key press up so we're going to track a new event so below the kwid event we're going to do another event so this event is going to be if event dot type equals equals and once again I'm finding this this event type from the PI game library so if I go to events they have all sorts of event types listed here so we're going to want to do keydown and keyup so if the event type equals equals pi game dot key down then we're going to want to make some movement so let's specify what a key it is so we can do if event key equals equals pi game dot then all the keys are listed in this documentation stew let's see where the key is event key somewhere down here maybe it's actually up here then there should be kiddie yet key so all the keys are found here so in key tab key clear all of it is located here so I'm going to use the left and right arrows so if I can find left and right in here so we've left arrow and right out right here so K underscore right and caters for left so if PI game event I key equals PI game dot K left then we want to move it left so I'm just going to pass for now we'll fill that in a sec and then the other F is else--if event key equals equals PI game dot key right we want to do something else and and the reason I use an else--if as opposed to else is because if we use else then it would attract any other key to be the the right movement we want it to be specifically the right arrow so let's fill in these functions these are these if statements so if event.keycode equals pi game you've got K left what do we want to happen well we're drawing this rectangle down here using the player position so if we can tweak the player position in this if statement that's going to allow us to move so take a second and try to do that on your own and then unpause the video when you want to see how I will go ahead and do that okay to do this I'm going to begin by decoupling this list in doing x equals player position 0 and y equals player position 1 so this is just taking our position right here and just grabbing the X&Y coordinate just so it's easier for us to read so when wherever we press the key down it will grab the x and y coordinates and then what we want to do is we want to if the key left is pressed we want to shift the x value so I'm going to do X minus equals 5 and if we hit event key right I'm going to do X plus equals 5 so it's basically just changing whatever our x-coordinate is in our position whenever we press these keys and then finally whatever happens after this I'm going to just set the new position to equal the list of X comma Y so the new values of x and y whatever happen to them in these if statements we're just going to reset it down here and one thing to note is I took out this Y just because in case you want to make your player go up and down in the future you'll be able to do that with this right here so let's run this cool it's kind of working I keep pressing the value I don't know if you can see the the block extending the the problem is the block is moving but it's just tracking the red with it it's not moving probably how we want it to be moving and so that the problem with that is it's always drawing the rectangle but it's never resetting the screen so when the rectangle has move it's not refilling the entire screen black so one thing we can do to fix this is there's a function called screen dot fill and that takes into RGB value so just like we use the RGB value red we can pass in the RGB value black and if I run this now well we got our we got our red block moving around and there's no annoying trace of where it's been but we're moving really slowly so let's go ahead and make this instead of maybe minus equals five and plus equals five let's just move it one whole block size so player size and player size and so finally we were on this and now it's jumping around our screen nice and fast so that looks really good and just for good practice let's take out this zero zero zero delete that and pass it up here so we're gonna do background color and we'll just pass that in here so you can change this to whatever background color you want we'll just stick with black for now so screen fill background color and now it's easier to read that line as well cool look at that okay now let's go ahead and get blocks starting to drop from the sky so we'll get blocks dropping from the sky and actually real quick I think it would be helpful to initialize our position to a slightly different location so let's do how about instead of this initial location of 400 300 let's set it right in the middle so we'll do with divided by 2 which actually will still be 400 but it just if we change this with now it'll stay in the middle and the 300 instead of that we'll do let's say height and if we did the full height it's gonna be off the screen so the in pygame the zero zero spot stops that the starts at the top left so if I went all the way down on the height I'd be down here and it's drawing it then below the screen so what we'll do instead is we'll do like height minus two times player size oh shoot I think I used the capital when I wasn't supposed to so two times player size now we were on that what happened player size is not to find out I'll have to do this right above this line save run cool and now it's in a nice position at the bottom of the screen so it's a little bit more friendly now to having blocks fall from this guy because it's not right there in the middle okay let's begin with just one block falling from the sky so we'll define an enemy let's say well we will call these blocks from the sky enemies so we'll say enemy size and to begin we can just set this to 50 I'm gonna set this the same size this player is but you can play around with this as you see fit and we'll say enemy position equals we want it to be at the top of the screen so we'll say something like I don't know we'll do some random value here now for begin with 100 and then we want it to be around the top of the screen and so I'm going to just say like 100 0 and I'm going to print this out right before we print out the the the player so screen let's this declare the enemy at blue so blue and RGB is you mean it's RGB so 0 0 red and green are zeros and then the blue value is the 255 in this case and we run that oh shoot I didn't actually finish this line so screen blue now and then we want to do enemy position so it's the same exact line except now we're switching in enemy position as opposed to player position so enemy position 1 so this is the X&Y of enemy and then we'll make them square so with your enemy sized enemy size so the width and the height is the same for the enemy run that and now we have this enemy at the top of the screen and it's set to 100 right now so we should probably set it to a different value so ideally what we want to do is set it to a random value so maybe take a second right now and see if you can figure out how to set this initial enemy position to a random value on the screen and unpause the video when you're ready for me to show you how to do that okay to do a random value on the screen we need to use this random library that I have mentioned in the first video I did in the tutorial series on Python and that was in the math and variables video and there's actually this library called random so we can use random to do the following in do enemy position equals random dot random in so we'll get just an integer between 0 and our width so that will put us right in the middle of the screen as we want just some random spot at the top of the screen on the x-axis we do that and as you can see it's in the kind of right side the first time I run it if I run it again it will be in a different spot so see it's now on the left and there's actually got a small error with this line as is and that is that this could actually have a value of width so if I instead made this width real quick you can't actually see the block because width is right here at the very edge and then it draws it to the right so we actually need to do is do random int zero - with - enemy size enemy size save that and now that will make sure it's in between the width of our screen okay cool okay now let's get the actual enemy to start following downwards so to do that we can do the following so right now this is the Y location of the enemy and it's never changing so to get it to start falling we need to on each iteration have this value change so that it actually goes down on the screen so we can do that pretty simply so we'll do something like let's say if enemy enemy position one is greater than zero or greater than or equal to 0 and let enemy position 1 so enemy position 1 remember is the Y position of the enemy is less than the height then we want to get it to fall downwards so we can do enemy position 1 minus equals like 20 we'll say and then else will do enemy position will want to reset it the else condition in this case means it's off the screen so we'll want to set it to back to 0 equals 1 and one nice thing about using lists is I can directly modify the enemy position in place so I actually don't have to reset it like I did here like I could have just modified player position 0 directly and not reset it like I did here so like this is another way to do what we did up here equals 0 now let's see what happens ok it's like freaking out why is it freaking out and the reason it's freaking out is because as I mentioned before the top of the screen is actually 0 0 right here so when we decrease the height we're actually going upwards so what we actually have to do is add 22 enemy position plus it equals 20 see what happens now oh my gosh it is flying and one thing we can do to kind of tame how fast it's going is we can set up frames per second rate a frame rate to our game and to do that we define a clock so we'll define our clock outside the while loop clock equals PI game time clock and let's just set our hour speed of our game to go that say like 30 frames per second I think that seems fair so in the wild loop now we need to do clock dot tick and we'll do pass in 30 for 30 seconds and this should slow it down a bit yeah so now it falls nice and smooth it's actually still going pretty fast I might slow it down a bit more we'll make this like plus equals 10 now let's see what happens that's like a nice fall and as you can see right when I get to the bottom it goes back to the top and falls again so now we have a falling block couple changes will make to make this a little bit better is first off the block is falling from the same spot every time so we should reset the the exposition of the anime so enemy position 0 which is the x position we want to reset that to another random value so we can do random Rand int again 0 - width - enemy size so let's see what that looks like yeah it's going different spots every time left and right side way - right side cool but we're still not gonna have any collisions right now like right now it just runs right through it so we need to do collision still and also one thing I think is nice to do is instead of doing 10 here this is a magic value we don't really know what that 10 means let's define a variable called speed up top so we'll say speed equals 10 and now we can just do plus equals speed here so now if we change that speed variable so if I change the speed variable like 1 it's gonna move really slowly and if you lose in this game I come on you gotta ain't gotta be better than that but that's really slowly and we can change it to like 50 to make it move really fast oh so as you can see that's one way we can play around with like the level of our game we already set it to ten so we have a falling block now okay so the next thing we're gonna do is go ahead and detect collisions so first off I'm just gonna write a comedy right here that makes that just lets us know that this is updating the position of enemy but let's detect collisions between the enemy position in the player position so to do this we're gonna define a function and we're probably to go by the end of this video we're gonna kind of move all this stuff in here into their own function because it just makes the code neater but we'll begin the first function will write today is the function that detects collisions so we're gonna do detect collision and that's going to take in the player position and the enemy position and we'll just begin by doing like player X so this is the player's x-coordinate just to make this neater player actually equals player position zero and then player y equals player position one then we can do the same thing with enemy enemy x position equals enemy position zero and that's the index of 0th index and enemy y equals the enemy position first index so now we have their positions and now we need to figure out if they overlap to help us do this I created a little visual so we could see what happens when overlaps occur so here we have the player block down below so and we have the enemy block falling from above so collisions could happen like this they could happen like this and if the player moved into the enemy they could also happen like this and then like this let's focus on what happens to the x coordinates of these blocks first and then we'll focus on the y coordinate so if a collision happens and also we know about the enemy we know about the player we know their top left position so the players top left position is piece of X piece of Y and the enemy's top left position is e sub x e sub y so if a collision happened like this we know that this e sub X this left side of the enemy has to be between P sub X and then if the player has size piece of X plus player size or the size is player size the coordinate over here would be piece of X plus player size so one condition we can write down that we know will have to be included in our collision is if Y sub X is greater than piece of X and E sub X is less than less than or equal to I guess we could do greater than or equal to and less than or equal to greater than or equal to and I guess this side of each strictly less than e sub X is less than P sub X I mean it won't really matter because this is just a pixel difference but piece of X plus player size so that's one conditional off that include and then we can also think about the reverse of that so the reverse of that would look like this would be if instead of the player being the farthest to the left we have the enemy actually to the farthest to the left and if that was the case then we can ignore this right side for now but we know that piece of X now right here has to fall in between east of X and then east of X plus enemy size so the other condition we can write here is we also need so this is an or condition so Oh and this is we're just looking at the x coordinate or piece of X is greater than or equal to e sub X so this is the reverse and piece of X is less than E sub X plus enemy enemy Wow sorry sighs let's type that up in our sublime text window we said if a sub X so I'm going to just go back real quick and look at what it said each of X is greater than equal to piece of X oh my god piece of X is greater than or equal to piece of X and E sub X is less than piece of X plus player size so this is the coordinates that's one condition and X can overlap and there be a collision or the other condition was piece of X just the reverse is greater than equal to e sub X and piece of X is less than E sub X plus enemy size and I'll put this in parentheses just to be careful and just so that this stuff evaluates in the proper way I'm going to surround this with parentheses don't know if it really matters but just I guess good to be safe in this in parentheses or here so these are the two ways the x coordinate could overlap so let's do the same thing for the y coordinate so I'm gonna erase the screen here give me one sec it just raised off the enemy again so we've taken care of whether or not the ex this is overlap now we need to make sure that the Y's when you check to see if the Y's overlap so I'm gonna hide this so if the wives overlap then we're gonna have something looking like this so we have let's say this is the overlap position we have a piece of X piece of Y and he sub XC sub y and if the wiser to overlap then we would have something that looks like this so we need a sub y to be between piece of Y and piece of y plus player size so it's the same exact thing so we either need that to happen or the reverse of that is if this enemy is the top lock we need piece of Y to be between UI and E sub y plus enemy size so I don't know if you can see that so in if both of those conditions happen so if the X condition happens and then either one of the y conditions happens then we know we both have a X intersect and a Y intersect so we know that there's a collision so to write the Y condition will basically just mirror the X condition and I will do if each of Y is greater than or equal to piece of Y and E sub y is less than piece of y plus player size and we'll have two parentheses here or this is just the mirror of the other one piece of Y is greater than equal to e sub y and piece of Y is less than E sub y plus enemy size so this is going to be for the Y condition and if you know we either have an X the first condition up here checks to see if there's an X overlap and then once we check if there's an extra overlap if there's also a Y overlap with one of these corners then we know there is going to be a collision so we want to return true here and if it doesn't enter these then outside here we want to return false and if it does enter once you return false it exits out of the the function and would never run this so this only runs if you this doesn't pass for this line doesn't pass okay let's check to see if that works so to do this we'll go back to our game loop and basically we'll just do if so we've updated the enemy position at this point so if detects collision of let's see how we wrote the function player position enemy position so if detect collision of player position enemy position so if that returns true then the game should be over right game over equals true and we don't really need to worry about the else condition because the else condition would just allow the game to keep running so let's see if that works ok I'll let this one pass okay second one will it it yay it looked like it worked and it might look better if we actually break out of the loop right here because if we break out of the loop then we don't draw the rectangles one additional time so I'm going to just check this it might not look any better but I just want to see what I think of this run so now if we collide all at the next block it it's more there's less overlap in the collision it kind of stops it right when they touch and so I guess it's kind of a personal preference there what you think looks better so if you do want it to just break up immediately use the break command if not just you can get rid of it doesn't really matter okay now that way you've done collisions let's go ahead and actually draw multiple enemies so right now we only have a single enemy falling let's change our code up so that we can actually have many different enemies following following to do this we're going to do a couple things so I guess we need a function that controls how many enemies should be on the screen at a certain time so that will be one function and then another function will probably the modified detect collision or do some sort of update positions function for all the enemies so that we can you know control each enemy updating instead of just doing this right here so we'll do a drop drop enemy function to also do an update enemy position function I think and we'll see if we need to do anything more than that so let's begin with the drop enemy function so what we're gonna do is that instead of having just an enemy position here I'm going to define a enemy list as well and that will equal be empty at first or I guess we could keep our original enemy and I could just pass in any position to start so it just has a single enemy in it right now and so the function we're going to call drop enemy or drop enemies will look at this enemy list and to begin let's say we want to keep adding enemies until we have ten total in our list so to do this we're going to do if length enemy list is less than 10 then we want to I guess generate a random enemy so we'll do X position equals random dot R and int so this is just random spot on the width of the screen zero to width minus enemy size and you've seen this before and then in Y position will be zero it'll be the top of the screen you can make this a little indented if you want to but we'll just start with the top of the screen and then we'll have to append this new XY coordinates to our aim or our enemy list and we list dot append a list of x-position y-position so that will be our drop enemy's function and then we'll have to have I guess a draw enemies function and a update positions function and we might let's say so we'll do a draw enemies first draw enemies and this could be put right into the loop but I'm gonna just bring it out because it's easier to read that way so a lot to do for this is for enemy in enemy list we'll just want to copy whatever we're doing down here to draw the enemy so just copy this line I'm going to take it out of here and paste it in here and so this is no longer enemy position this is now called enemies or I guess I could change it to enemy position in enemy list draw the rectangle for that enemy so now it iterates through multiple enemies cool it what else do we have to change down here well we'll actually have to call that draw enemies function of enemy list and also have to call that function we just wrote called drop enemies and that takes in the enemy list as well I think we could probably we're gonna have to comment out this stuff here because this would no longer be relevant but I think we can probably run this as is and see what happens oh my god well you have a bunch of things at the top of the screen so I guess that's a good thing so let's see what we can do to fix this so first off I guess this we just commented out the update code so that's a problem that's why they weren't following falling so that's this write a function called I guess update enemy positions so find a function called Def update enemy positions and that will take in the enemy list and let's say do we need a stake in anything else I don't think so so it takes in the enemy list and what we'll wants to do with that is that will basically just copy this code right here so copy this code and paste it into this list and now we need to uncomment it so update enemy positions and now we're gonna also have to do if for do a for loop for enemy position and enemy list and okay so we have this so that's going to update each individual one and instead of resetting this will actually pop it off our list so that our drop enemy's function can take care of it now so this updates the position and we'll want to have a else statement which basically says so this is saying it's on the screen the else condition would be it has gotten off the screen and if it gets off the screen we'll just pop that specific enemy off the list so one thing we can do to figure out what the index of it is in the enemy list is we can do this is from the for and while loops tutorial I have so you numerate enemy list and then we can do enemy position and also get the index so index and enemy position and we want to pop off this index and then basically the drop enemies function will handle this again so let's see we have draw enemies update enemy positions drop enemies and we'll play I have to change this detect collision or we'll make a new function called like collision check or something like that and they'll take in the enemy list and this will return true this will cause a game to end if any of this collision check ever returns true so so for enemy position in enemy list I'm doing a lot of functions right now so hopefully this all is making sense if you have any questions about a specific function I'm writing here make sure to just leave a comment I'll help you guys out so enemy position in enemy we can do the following we just need to use our detect collision function here so we can guess maybe it will also pass in the current player position so if detect collision is true here for the enemy position player position then we want to return it true and then if we get through that for loop successfully with no returns of true we want to return false all right we have a bunch of things now we just need to add them to our loop so we have drop enemy's draw enemy's update enemy positions collision check so let's see drop enemies DRI enemies I need to do update enemy positions that will take in the enemy list and I guess we want to update the positions before we draw them probably no no update I just copied blank space I didn't let me just copy that line in enemy lists and finally we'll want to do a collision collision check on our enemy list using the player position and do we update yeah the player position will be updating from this code up here so we'll do if collision check game over equals true then we'll break so this is exactly what we were doing up up here but now collision check uses detect collision inside of it but it loops over the entire list of enemies so let's see what happens now whoa we got an arrow enemy collision check is not defined I must have spelled it wrong up here collision check that looks right how did I spill it down here collision God name enemy list is not defined what the heck oh and then me oh I can't spell it anymore it's getting hard as we get to the end of this video come on oh my god oh my god yes we got it we got it working pretty well oh my god but they fall so fast at the start so one thing you could do if you want it to stagger a little bit because right now they're all following it around the same time is a little bit of a hack and it looks like this so right now in our drop enemy's right we have if length mu list is 10 we automatically add the new enemy so that means we're adding them all at the same time what we could do is is add a little bit of a pseudo delay and how we would do this maybe try to think of your own way to do this at first to just delay this function work like a pending so like only certain times does it actually append it's like every like 10 times it appends one of these when it could be appending all of them at the same time so what I would do is I would do a just gonna write at a variable called delay and that equals random dot R and random random dot random and what random dot random does if you don't know is it generates a random decimal or a random float value between 0 and 1 and any of those values between 0 & 1 are equally likely so this is a good way for us to get some sort of probabilistic do something based on a probability so we can now add a line that says and delay is less than like let's say 0.5 so only half the time does it actually append to this list and that will allow us to like stagger the blocks a little bit and we might have to tune this value so if that is the case then only append to the list let's see what happens down okay it looks like it's staggering a little bit more but it's still they're all falling pretty much at the same time so we'll make this even smaller will they make it point 1 and look at that so now the blocks are following a much more staggered rate alright to end this video let's add a score let's add you know increase in difficulty as we progress in score then also maybe like changing up the background color or like changing the the block sizes so we'll do some additional features right now so first off let's do score so to do score what we need to see is that if I run the game run game run game oh maybe the game is already running oops so if I was to run this game right basically we want our score to increase as we successfully dodged each block so what we really need to do is just a pet or add one to our score each time one of these blocks on the screen that you can see with my mouse falls all the way under the screen and so this is going to be pretty straightforward so outside of our loop at the top of our screen we'll just define a score right and our score is going to start out at zero so now we have a score at zero and each time these blocks fall and get to the ground will increase our score by one so let's see how do we do this so it's gonna happen in this update enemy position so the first if statement here get checks up the enemies in the screen this else would check to see if they fell off the screen so here's where we want to increase our score so we'll do score plus equals one here and see what happens local variable score reference before assignment so basically what happened here is we actually need to up to pass in our score because it doesn't know where the score variable is in this function name because I guess yeah sure yeah it doesn't doesn't recognize this score because we never passed it in so if I do this now now we actually have to change up how we call our function so we can do score plus equals one where is it we're sort of where's the update enemy positions I'll delete this room quick so update any positions and we want to pass in our score now to see what happens now okay so our score should be increasing and to check that how would we print out our score right below update enemy position so print score and we should be able to see this at the bottom of the screen over here when we run it again so zero zero zero should be one now it should be one okay it's not one what did we do wrong and what we did wrong here is that score is a integer and integers are a immutable type so it can just directly increase the score by doing plus equals one we actually have to reset score to point to the new value so what we can do is a pretty simple change and that will just be in the update enemy positions function will return to score when it finishes and we'll just reset our score so now we'll do a score equals update enemy positions so this will do the update and also just return a value so this is totally fine for us to do okay Oh am I still printing score I guess I'm not printing score anymore so we'll print score again so now the score should increase as the blocks fall off the screen come on yes yes five six seven so it's down here on the side wow I just dodged a bunch one I was late trying to show you that the numbers right over here okay so that's the score and it looks pretty good it's going up like we want it to and so let's actually get that score to print to the screen so to do this in PI game we can at the top of our screen we're gonna need to define a font so I'll just do this below the clock it doesn't really matter where we do this so I'm just going to save my font equals and I'm looking this this is a command that I would look up online to remember I forget this off the top of my head we can do PI game font dot sis font and what I did in my example that I showed you guys at the start of this video so I use some font called mono space but there's a bunch of options I think if you look at the PI game website you can find all the options Samanta space of 35 pixels and then to actually print out the score what I want to do is is down here in the game loop so maybe after I update the score I'll actually draw it and I can do that by doing text equals score plus so we can use plus when we're appending two strings together so I'm to score and then we'll do label equals my font render text and then we need to do the position so the position will just say actually I don't remember exactly what this my font on render does but just follow me on how I do this so forget what exactly this one is here I think this might be the direction is going so the one means horizontally I think if I change this to a zero it would be vertical text then I want to use a color for it so the color will define will define yellow so yellow is 255 255 0 and I'm actually going to copy this out and do what I did with yellow and blue or in red blue and I'm gonna do yellow up here so yellow equals to 3 5 255 0 and so Fassett yellow here the last thing we need to do is actually attach our label to the screen to do that we can do this function called screen bullet it label and then wouldn t we need to select a position so the position I'm going to select is this is just something I played around with before and it worked so I'm gonna do with minus 200 and then height minus 40 so if you know we know our font size is 35 that we chose so height minus 40 gives us a little more space than what our font size is and this offsets it to the left a little bit so let's see if that worked yeah must be strained not in so the problem here is we're trying to add a number to the string so we actually have to do is we can typecast values so if I have a number that I want to make not the number but actually a string that represents the number I could surround it with STR of score look at that come on increase yes yes I don't know if you saw that but it was increasing over there on the right side and that gives us a little bit of extra space so that if we get to like hundreds the numbers of the hundreds still will work properly but you can play around with that how you see fit cool we have our score all right what else should we do we want to do a level based on the score so how do we define a function that's called like set level so I'll do this up here so the fine set level so that's gonna need to take in the score and basically we'll change our speed variable so we have this speed variable here based on the score so maybe it's right in our speed is 10 maybe we start out really easily so if our score is less than 20 maybe our speed is just me you know three so if score less than twenty speed equals three hell if score less than forty speed equals four and we can keep doing this l if score is less than 60 speed equals five and then L if score I will do else so once you get over sixty we'll we'll make it really hard so speed equals fifteen and so if we remember speed was being added here so we'll probably have to let's see what if this works I don't know it was it I don't I don't think it was updating at all so speedo is set to 10th here these values here is not going to reset the speed it's just gonna call some variable some new variable called speed inside of this function 3 so we'll actually have to do is pass in our speed variable and then at the end of this function will return the speed so our set level function will happen down here maybe right after we update our score so where do we update our score so the score is updated here so we can set the level right after that score and then speed and we're gonna have to say speed equals set level score speed so now it should go slow yay we got it we got it oh my god this is gonna take forever for me to actually see if it speeds up maybe I'll change these values around a bit yeah I'll change these values a bit so you don't have to wait all day just to see me playing around with that level all right speed set level score speed so instead of the value as I said initially let's make this 5 let's make this 8 let's make this 12 and see if that worked so it should never guess we get faster so we got this this is the first 10 blocks and now after I'd passed the second 10 blocks we should see a substantial increase in speed so oh look at that yep at 20 they started going faster and we should get another increase at 40 let's see if it happens yep I definitely see an increase and our final increase will be at 60 and yeah it's definitely moving faster and in addition in the set level function you can really play around with this as you see fit you could also maybe change the amount of you could make this like a variable like enemy number of enemies and as the level increased you can also make this value right here and drop enemies increase so you maybe have 50 blocks dropping at the same time you can also make speed proportional to the score so you could do something like I'll show you I'll just comment this out real quick you could do something like speed equals score divided by 5 so at the start of the game it would be I guess you'd have to do like score divided by 5 plus like 1 or something just so it actually has some motion when the score is at 0 but like this now if you got to 50 you would be at speed 11 if you got to 100 you'd be at speed 100 divided by 5 which is 20 so 21 so this would be a proportional to the score this way and I guess you'd still have to return speed so that's just another thing you could do you make this set level proportional to speed if we drew this draw this out and we like I'll leave this as an exercise if you want to make more enemies drop or if you can't figure out how to get more enemies to drop I just leave me a comment and I'll tell you in the comments but there's a bunch of different ways we can play around with this so I'm gonna just leave my function how I had it so I'm gonna comment this comment this out and just reset it to be this there the last thing I'll do in this video is just play around with our background colors a little bit so we got the game working if we wanted to change the colors like we can make our background color like you know some you know we could to do this we could do good drawing use a website so there's a bunch of like RGB color sight when just look that up online RGB color wheel so I could take any value I want it so maybe I wanted a light blue background so like something like this so I know it has 11 for our 238 for green and 207 for blue and I'm on a site called color spy right now so I'm gonna copy that 11 238 11 238 and then what was the last color I forget 207 so this will give us a nice light blue so now if I run this we have a different color game and this makes the score a little bit hard to see but you can see that the screen color did change and you could do the same thing you could play around with clutters here so if I wanted to say I wanted like a tannish color for a player they're gonna go into like the oranges and you know this is kind of a tannish color - 29 - OH - 137 so I changed this red or I'll leave it as red just because I don't want to change the other name so - 29 - a - 137 - 29 so oh my god it's so hard for me for this number of all talking - OH - 137 - OH - and then 137 we'd run this you know now we have different colors so play around and have fun with these colors that's another feature you can change alright that's all we're gonna cover in this video we went from having a blank screen to having a fully functional animated game so that's pretty cool if you enjoyed this video and learn something if you huge of you throw a big thumbs up and also it'd mean a lot to me if you subscribe to my channel I'm trying to get to a thousand subscribers as fast as possible thank you guys for watching and peace out
Info
Channel: Keith Galli
Views: 430,713
Rating: 4.9224625 out of 5
Keywords: Keith Galli, how to program a game, how to code a game, python, pygame tutorial, pygame, how to program a game for beginners, how to program a game in python, how to, beginner, simple, easy, game development tutorial, beginner game development, game development, how to build a game, how to build a game in python, python tutorial, easy python game, game in python, make a game in python, how to make a game in python, python programming, programming, program a game, lesson, code game
Id: -8n91btt5d8
Channel Id: undefined
Length: 70min 7sec (4207 seconds)
Published: Thu Mar 15 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.