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