You can access our newest Python course here: Learn Python Programming by Making a Game
Transcript Part 1
Hello and welcome to the course. My name is Pablo Farias Navarro. I’m the founder of Zenva. In this course, you’ll learn how to code in Python by creating this simple game using a library called Pygame.
The game that you’re making is similar to the Atari classic Frogger where you have to move a character across the screen avoiding the enemies that are moving and trying to get you. The main goal of this course is for you to learn the basics of Python programming. We are assuming that you have no prior programing experience in this course so we’ll cover everything from scratch and what makes this course very unique is the fact that a lot of these concepts are gonna be taught in a very visual way because you are putting all of these things in practice as you ware building the game that we just saw. The author of the course is Max Schallwig, Python programmer and online trainer and the library that we’ll use as I said is Pygame.
Pygame is an open source library for the Python programming language which allows you to create two-dimensional games. We’ll cover the installation of this library and of Python in the course.
Now, online courses are an amazing way to gain new skills. I watch online courses all the time myself and what I really like about them is that you can go back and rewatch the lessons. In case you are not sure about something, you can download the course code, that includes the assets of the projects as well and what works best when it comes to learning for a lot of people is to code along, basically have the code editor open and type what you’re seeing on the screen and trying to get things working as you go. Other people prefer to watch all the videos as if it was a Netflix show, we have people that do that, so it’s really up to your learning style and that is the great thing here is that it’s completely up to you how you want to engage with this course.
What we’ve seen from a lot of students is that those who plan for success will make a weekly plan on when they’re gonna watch the lessons are the ones that tend to finish them, so keep that in mind and just a few words about our company. We’ve taught programming online to over 200,000 students since 2012. Many of these students have used what they’ve learnt to improve their careers, start companies, publish games, so it’s an open invitation for you to really engage with the content of this course and to get the most out of it. Now, without further ado, let’s get started.
Transcript Part 2
Hey everyone. So, in this tutorial, we’re gonna learn all about the basics of python, and then we’re gonna go on and we’re gonna learn how we can create a simple game using Pygame.
So, to get started, first of all let’s see how we can download python. Now Python is a scripting language and it’s much nicer and easier to use and to read then some other languages are like C or C++ because it’s much more user friendly and it also works on a higher kind of level. So to get Python, we’re gonna go to “python.org” and on this website here, we’re just gonna look at the downloads tab. We’re just gonna click here.
Now usually if you go to downloads the website actually automatically detects what kind of operating system you’re using, so in this case for example, mac. If it gets it wrong, you can just go here to the download section and just choose whichever platform you’re using, or you can also choose it down here.
Now we’re gonna be doing all of our coding in Python 2.7. So to get that, we’re gonna click on this link here that’s gonna download Python 2.7 for us. And then we’re just gonna save that file, and we’ll be letting it download in the background in a second. So once your download is completed, we can go over here and head into the download section and just open up the downloaded file. Then we need to continue through here and also go through the read me and make sure you understand this, and afterwards we’ll continue again going through the license now, and we have to agree to the license.
Then we have to choose our destination, so I’ll just install it here on my local hard drive. It’s not a very big file so nothing to worry about here. Then we’ll click install and I’ll just put in my password here and there we go, we’ll let the installation run. Shouldn’t take all to long and… Well yeah, let’s wait for it to finish. And once that’s done, then we’re pretty much ready to go and start programming in Python.
So that’s pretty much it for the installation part. So here we go we’ve just finished the installation. Now we can close this. We can also immediately move this to the trash. We don’t really need the install anymore. We can directly clear that and yeah, that covers it. So now we’ve got this great Python environment downloaded that we can use to write code in and execute all of our code. So to open up our idle environment, we can just go into our finder, and for me, my environment is in this Python 2.7 folder that’s down in the applications.
So that’s where my Python package is an then you see here we have this idle link and where this shortcut and we can just double click this to open. If you’re not really sure where it is, so you should probably just go into the applications and look for your Python 2.7. Otherwise you can also use the Mac Spotlight and just search for idle here. And then I’ve actually got the 3.5 version installed too, but make sure that when you scroll down, you look for the version and it says version 2.7 here and then you can open that and we can get started with that. So this is how the coding shell is gonna look like for us.
To actually make a program, we can go here into file and create a new file, and now we have a new Python file here that we can later code in that we have to save as a Python executable. And we can also use that later on to just run the module here on the run module and everything will be ran in our Python environment. So that’s how we’re gonna go ahead about executing code but we’ll see a lot of that in the later tutorials too.
Hi everyone, what I’m gonna do now is show you how to download the version for windows. So I’m gonna go to downloads and I’m not gonna click on download the windows version that it says here. Instead I’m gonna go into Windows, and I’ll be presented with a list of versions. I’m gonna scroll down until I find a 2.7 version. The highest 2.7 version. And from all of these, the one I’m gonna choose is the one that says “x86” which means 32 system, 32 bit system.
Even though my computer is a 64 bit computer, we’re still gonna pick that one and I have to tell Google Chrome yes I want to download that. It’s downloading. The reason why we’re picking the 32 bit is because Pygame, the game library that we’re using in this course doesn’t support the 64 version of Python. So that’s why you have to pick the 32 version of Python here. So it’s downloaded. I’m gonna go and click “open” and run this installation. So I can choose where I want this to be installed. I’m just gonna click “next”. This will install the software.
Okay, so the installation has completed. I’m gonna click on finish, and now if I go to my start menu, I can type “Python” and that will bring me the idle which is the same that we use so on the mac installation video. So now let’s continue and start learning how to code in Python.
Transcript Part 3
Hey, everyone. It’s Max and welcome back to our Python tutorial. So, in this tutorial, we are going to learn how we can set up our Pygame instance. How we can create this Pygame to appear on the screen so that we can start doing cool stuff with it.
Alright, so, the last tutorial we downloaded and installed Pygame, and now let’s go ahead and actually use it. So, we are going to go back over to our coding environment, into this IDLE here. And actually just deleted the code that we used before, so that we have this blank space to work with. But we are still using this new file. If you want, you can just open a new file up like this, and then just start coding in here, as long as you remember to save it as a, or using a .py extension. Yeah, I just cleared my old file, and then we will just continue on like that.
Alright, so to actually use Pygame, what we are going to do is you are going to need to “import pygame.” So, what does that mean? Well, we’ve downloaded Pygame and we’ve, it’s now like an extra extension that we have to Python. But it’s not always included. And the reason for that is because if you download a bunch of modules and you have to import them all the time, your files are just going to be huge and everything is going to take up a lot of memory, just because everything needs to be imported all the time.
And there are hundreds and hundreds of modules if not enough that all since that you can have and download and so you really don’t want all of that space taking up everything. So if you want to use an extra module, you are going to have to, here you are going to have to import it. And if you are importing something, you also want to make sure that you’ve downloaded it and installed it beforehand. Like we did with Pygame.
So, what we are doing here, is we are using the import cue word, and then we are typing in, or we are putting in here the name of the module or the package for the library or framework, whatever you like to call it, and that we want to import.
And so what’s happening is Python goes and it looks up the modules or what the libraries whatever that are available and it searches for the Pygame library, and if it finds it, it imports it and it makes all the features contained in Pygame available to us. So that’s what we are doing with this “import pygame.” Alright, so now that we have that out of the way, now that we can actually start doing stuff with Pygame, let’s look at how we can do that. So, to access things from Pygame, we actually always have to go and type “pygame” first and then we put dot, and now we can start accessing stuff from Pygame.
So, the reason that is like that is because first we imported Pygame, and now, every time we want to use something from it we have to access that library, so we have to go into the library and then we put a dot, so that indicates we want to go into the library that this is not just a variable name or something, and now once we do this dot, we can now access all of the stuff inside this library.
Now if you want to know everything that’s available inside this Pygame library you can look at the documentation, but otherwise you can just follow along with this tutorial and I’ll show you some of the cool stuff that’s available inside this library that we can use to start off with.
Alright, so the first thing that we are going to need to do to actually set up our Pygame system is we are going to need to go into the Pygame library, like this, and we are going to need to call the init method. So, what we are doing here is we are going into this Pygame library and we are calling a function called “init.” So, we see that it’s a function because it has the open and close parentheses, it doesn’t take any arguments in here, that’s just law there are no defined arguments, there are only optional arguments, so doesn’t take anything in here, we don’t need to pass anything. And what this does is going to initiate everything for us. It’s going to make it ready for us to use. So we need to do that at the very beginning each time we want to use Pygame.
It’s also called a method, just because it’s part of this library and there are lots of classes and objects and stuff contained inside, so pretty much big bundles that all have their own attributes. But we are going inside here, and we are accessing this, and we are pretty much just calling this function.
So we’ve seen a function before, the function that we created was the add one, where we put in a number and it gives us back a number that adds one. This function here, or this method, is a little bit more complicated, but what it really does is it just sucks everything up for us and make it ready for us to use. Alright. So the next thing that we are going to need is we are actually going to need a screen to do stuff on. So we are going to need to define our window. So we are going to create a “screen” variable, and we are going to make this equal to the following, and so now, now we’re going to learn how we can create this screen.
First of all we’ll need to go into Pygame, so that’s where we get our screen from. And now we’re going to access something called “display.” What display is, is display is an object inside of Pygame. And now, what is an object? An object is pretty much this big structure that has a lot of functions associated with it, and it has also its own attributes, and so its own kind of aesthetics it has its own variables inside, and so it brings a lot of features with it.
In this case, it’s a display and so the object display pretty much contains everything that you’d want in the display or that you want to do with the display. So what we’re doing is we’re going into Pygame and in Pygame, there’s this object called display, and display itself has a bunch of methods or functions so functions are methods, and they’re called methods just because they’re part of this object, display has a bunch of methods that we can use and it also has some attributes that we can access as well. But what we want to do is we want to access one of the methods and specifically we want to access the method of display, which we do by adding another dot here, so the dot means we’re going to go inside here and access one of its methods or attributes.
So the method we want to use is called “set_mode” like this. And so, we open and close parentheses because it’s a method, and it can take input, and the input that we’re going to provide it, is just going to be the size of the window that we want. So for example, we can use the size, maybe, 900 by 700 or something. So that’s going to be the size of this window that we want to create.
So what we’ve done is we’ve gone into Pygame, then we went into this display object here, so this display object self, you can kind of imagine of having many features that you can use that all work around this same display principle, and we use this set mode method that’s contained inside, and we gave it the input of 900 by 700. And all this is going to do for us is going to create a screen, so that’s also why we saved it in a variable called the screen, because it’s the best fit name for that. It’s going to create a screen the size 900 by 700 for us.
Alright, good. Now we’re going to look at how we can keep this screen going so, one thing we can do for example right now, is we can start running our module and we’ll have to save it first. So we run it, and we see this thing pop up on our screen. And so this is the screen we’ve created. What we see, first of all is, I get this spinning ball, this spinning beach ball, I’ll explain in a second why. But we also see that we have this screen created here, this screen created here.
Now the reason that my screen is only a quarter of the size of my window is because my Mac is using this retina display and so the resolution is a little bit off. So that’s why by me it’s only taking up a quarter of the window, usually you get the full kind of window taking up by this black screen here, but just because of the resolution of my computer and everything, everything is a little bit confused. We only have this smaller black screen going on right here.
So if that’s the case for you, if you also have a built-in retina display or something, then you may expect this but otherwise you know, everything should go fine, and this is not really a big of a deal, the only thing that maybe a bit tedious is that this window here is a bit smaller. There are ways to fix it but we would have to use another module for that, so we’re not really going to go into that, that much in detail.
Now, the reason I’m getting this beach ball here is because our Pygame pretty much updates continuously, and there are a bunch of events coming in, and now these events, there are so many events now that I’m not doing stuff with and it’s pretty much piled up and there’s too much for my computer to handle right now for this Pygame program to handle right now so it’s lagging.
So we’ll look at in the next tutorial how we can get around that and how we can make sure that we don’t start lagging like we do right now.
Transcript Part 4
Hi everyone, it’s Max. Welcome back to our Python tutorial. Let’s continue on where we left off last time which was we created our display but it was start lagging a lot so now we’re gonna look at how we can fix that.
And now what we’re gonna do is first of all we’re gonna create a variable and we’ll call it just finished or something, and this variable is just gonna keep track of if our game is finished or not. So first we’ll just assign it the value false. Quick introduction to Boolean variables, we’ve seen that a little bit before, we had conditions, for example we checked, when we checked zero is less than ten, this gives us a value of true, and then when we checked ten is less than ten, this gave us a value of false.
So we saw that when we did while loops but let’s just talk about it for one more minute or so. So what we get here with this Boolean variable is that we have here an option of true or false. And depending on what the state of our game is, we can either change this value to be true, or we can change it to be false.
Now technically you can also think of it as numbers too, so like zero and one or something like that. But to make it a little bit more obvious, it actually has its own values of just being true or of being false, so it can be in two states, our values can either be true or they can be false. And so that’s what’s going on with these Boolean variables. And so we can check, as long as something is false, then you know we ought to do it, or we do it and as soon as it turns true, as soon as our state changes, then we’ll do something else or something like that. Alright, so let’s start off and let’s start dealing with these events that piled up. And the way that we’re gonna do that is first we’re gonna start off with a while loop. And we’re gonna say, while finished is equal to false.
So what does that mean? Right now, we’re just saying, while our game is not finished, like this. Make this a little bit bigger so that we can see that. So what we’re saying is, while and then while our value contain is finished is false, we’re gonna do the following. What we mean with this is while our game is not finished, because once our game is finished, and finished is gonna take on the value true.
Alright, so, we’re going to the next line. Make sure that we indented it to be inside of this while loop, and now we’re going to use a for loop. And we’re gonna say, for event, so event is going to be our variable name that we’re creating here, our looping variable name, in the following, we’re going to go, and I’ll just write this out first, and then I’ll explain what exactly is going on. So we’re going to pygame and now we’re going to go into the event object, so an object that pretty much takes care of all the events that are going on. And I’ll also explain what events are in a second.
And what we’re gonna do is we’re gonna call the get method. And then we’re just gonna have a colon here, and we’re gonna do the following. So what this is doing, every loop is, it goes into pygame, into the event object and it calls this get method. And this get method pretty much gives us all of the events that have happened since the last time we checked. And we go through all of these events one by one, so event takes in a value of each of these events one by one, and we go through them individually. Then we can do things with certain events.
We can also later check for certain events, or we can do other things, just using these events that we’re getting from, getting the events from this event object here. So every time something happens in the game, or in our pygame, or every time something even doesn’t happen every time there’s an update, new events are created.
And so to make sure that they don’t pile up, we’re gonna get them continuously, and we’re gonna loop through them, and we’re just gonna deal with them and even if we don’t do anything, that still deals with them because we at least checked it. Alright, so that’s what we’re doing right now. We’re just gonna go for event in pygame.event.get, and all that we’re gonna do right now is we’re going to pass.
Now what pass is, pass is just a special kind of keyword, so we can see that it’s color coded, and pass means don’t do anything. And so the reason that I put in here a pass, rather than nothing, is because as soon as I have a loop created and a colon and stuff, Python expects something to be here, I can’t just start on a next line and type more code, Python expects at least one line of code in here. And if I don’t wanna do anything, I’m just gonna put pass for now, so that I don’t really have to take care of any of this stuff and I can just go through it and go over and over.
So that’s what we’re gonna do right now, just to make sure that we’re not really lagging and just that we’re taking care of all of these events. And then we’ll add to that a little bit more in a second. So let’s just run this first. Here we go. And we see that, even if we wait a little bit now, I’m not getting this spinning ball of death anymore, the speech ball or whatever it is that you’d like to call it.
Alright, good, so, now that we’ve got this going, let me just close this here, and in the next tutorial, we’ll continue on about how we can extend this just a little bit more.
Transcript Part 5
Hey, guys; it’s Max, and welcome back to our Python tutorial.
So, you may have also noticed that at the state we were in the last tutorial, if you close the X window on the pygame window that popped up, nothing would actually close. So we’re gonna take care of that right now. And instead of just passing here and not doing anything, we’re actually gonna look at this event and see if, you know, a certain condition is fulfilled. And we’re gonna do this through learning a new thing, which is called an if statement; and while I can put up here the general structure, so if and then we have this kind of statement/condition. If the statement/condition, then; or usually if the statement/condition is true, and then we do the following here. So just, just some code; so that’s the general structure, but let’s just see that in action.
So what we can do, for example, is we can check if, and then what we’re gonna do is we’re gonna go into our event; so for each of these events that are being created here, we’re gonna look at each individual event that we get. So remember when we did the four, the number in the range, we got the numbers zero, one, two, three, four, five, six, seven, eight, nine? Here we’re getting similar things for events; but instead of getting numbers, we’re getting individual events. So we can access each event, and each event that we get from calling get, the get method on this pygame.event object, each of these events actually has its own attribute called a type.
And so you can just imagine this attribute as being a variable that’s contained inside of this event. So this event itself has many, many properties; and one of the properties that this event has is a type, and so that’s what we’re accessing right now, the type of the event. And, again, we use this dot here to indicate that we’re going inside of the event to access its property called type. So now that we have the type of the event, and the syntax may look a little bit confusing and stuff; and you’re like, well, how do you know that?
Well, you can never really know; so the only way that you really know what the event contains in everything is just to look at the documentation. But if you don’t wanna read through all the documentation just getting started, you can also just look at tutorials or like we’re doing here. I will tell you some of the properties that are contained inside of these things.
So you can never really just guess and know. You just kind of have to look it up in some way or another; and in this case, I’m telling you that type is one of the properties that is contained inside of event. That’s just the way that the creators of pygame created this event object. It contains a property called type, and so that’s what we’re gonna be using. So what are we gonna do here? We’re gonna check if the event type is the same as, and now we’re gonna look at a special event in pygame called quit; and then we use the colon here. So let me just explain what we’re doing here. We’re going into the event, and we’re looking at the type of the event.
So what kind of event is it, pretty much; is it, so you know, is it a move, is it a (mumbles) jump, you know, change color, like, all of these things. That’s kind of what you can imagine this as. What kind of event is going on; what happened? And we’re looking at if the event is, well, like an exit event or like a quit or we wanna close the window or something like that; and to check that, pygame has actually created a quit method. So, yeah, so it would just go in here, or this quit event rather; and it’s this default event that’s included in pygame, and we’re just checking if the event type that we’re getting is one that’s equivalent to a quit event that, in pygame, corresponds to us quitting the game. So, for example, us closing the window. So that’s what we’re checking right now.
So pygame itself, or the pygame module, the pygame library, contains this quit object that, you know, represents a quit event. So it pretty much indicates that the event type here is quit, and that means that we wanna quit the game. And so if that is the case, if they’re equal, what we’re gonna do is we’re gonna take our finished variable and, now that we know that the event type is a quit type, so that means the user wants to quit the game, we’re gonna say the game is done. So even though we didn’t finish the game, we’re gonna say the game is done because the user wants to quit the game; so we’re gonna say finished is true.
And what that’s gonna do is it’s gonna change our while loop here; and our while loop is no longer gonna be fulfilled, so finished is no longer gonna be equal to false. Finished is now equal to true and so true, checking if true is equal to false gives us the overall statement of false because true is not equal to false. And so we’re no longer gonna execute this while loop here. All right, so, that’s what we’re gonna do now.
And so if we run our module, then we see that everything is, you know, still good and stuff here; and then, well, my console’s a little bit slow sometimes, so let me just, well, it looks like we got this error message going on here. Oh, my bad, so we were not supposed to have open and close parentheses here. Let me just force quit Python right here or this, there we go, all right, so, my bad. The quit is not supposed to have open and close parentheses; it’s just supposed to be quit without anything. So let’s take that away and let’s rerun that. That gave us an error, and here we go. So now everything works grand.
Everything is working. We’re not getting the spinning ball of death. We also don’t see any errors popping up here; and if we click Close, then your Python should close. For me, it’s always a little bit; and, you know, if things are going on so, no matter what happens, for me, I just always have to force quit it. But if your computer is just a little bit different, then everything just should close normally without you having to deal with these nuances.
So that’s what’s going on here, so that’s what we did here with this setting this finish property to true.
Transcript Part 6
Hey, everyone, it’s Max and welcome back to our Python tutorial. So, in the last tutorial, we looked at how we can create this pygame screen, how we can go through it, process all of the events and also process what happens when someone quits the game, so when they click the close button.
So that’s great, but we only got a black screen. So, let’s deal with that a little bit, and let’s make something a little bit more interesting. So, what we’re gonna do now is we’re going to learn how we can create a rectangle on the screen. Okay, so, how are we gonna go about that? Well, what we’re gonna do is, first of all, we’re just gonna create a rectangle, and we’re gonna save it in this rect, we can call the variable rect. We can call it rectOne or something like that. Just give it a name to indicate what your rectangle should be. So this is just the variable name that we’re gonna store our rectangle object in.
So, how do we create it? Well, we create it by, first of all, going into pygame, and in pygame, we’re going to access the Rect object, and now we opened and closed parentheses, and although this opens and closed parentheses, this does not mean that it’s a method technically. So, we can have opened and closed parentheses for objects as well as methods or functions, you know. Method’s just functions for objects. But what this opened and closed parentheses allows us to do is it allows us define some starting parameters for our rectangle. And specifically, what we can define is going to be a x, a y, as well as a width and a height of our rectangle. So we can define the starting positions, the x and y conditions, as well as the width of a rectangle and the height.
So these are the properties that we can put in here. So, for example, we can put it at the x, y coordinates of zero, zero, so that’s where one of its corners is going to be. Then we can give it a width of maybe 30 pixels, and we also make it 30 pixels high, so just that everything is square, so that everything is consistent, and we’ll just create a squared rectangle at the position zero, zero, yeah, with these parameters here. So, that’s gonna be its size.
So that’s the rectangle that we’ve created, and we’ve actually saved this rectangle in this variable called rectOne. So, we can access our rectangle later on by just accessing this rectOne variable here. Alright, so now we wanna add it to the game. So, to do that, we’re going to go into pygame, and what we’re gonna do is to add this rectangle to the game. So now we’ve created the rectangle, but now it’s not really anywhere.
So now we wanna add it into our window. So what we’re gonna do is we’re gonna draw it into our window, and in this draw object, we’re actually gonna look at another property, which is going to be rect. So, what we’re doing is we’re going into pygame. We’re accessing this draw object, and inside of this draw, we’re saying what exactly do we wanna draw? So, there are different shapes that we can draw. One of those shapes is a rectangle, and that’s exactly what we wanna draw.
So, we wanna draw a rectangle, and here, again, we can pass certain parameters. So, that’s why this draw object is kind of the bigger class, the bigger object that in itself contains different attributes, different features, different functions or methods that do different things. And, generally, the object is responsible for drawing, but depending on what we wanna draw, we have to access different methods.
In this case we access the rect method, and that way it’s gonna allow us to draw rectangles. Now, we have to put in here some parameters. So first of all, we have to define the screen that we wanna draw to. So, our screen is the screen that we created up here. Now, in case you gave your screen a different name, like screenOne, your screen will be screenOne here. So you wanna make sure that the variable that you saved the display in or the screen is the one that you’re gonna be passing in here because that’s the one that you’re going to be wanting to draw to. But we’re just leave our name as screen.
Now, something else that we can do is we can put in here a color. This is gonna be in the form of red, green and blue. So, we’ll look at that in a second. And the final thing that we’re gonna put in here is going to be the rectangle object that we wanna put in there. So, we’re gonna draw onto our screen. We’re gonna draw onto our screen, or we’re gonna draw a rectangle onto our screen that has this color, and this is the rectangle that we want to draw on.
So, those are the parameters that we need to pass with it. Now, let’s quickly define our color. So, to define our color, we’re gonna create a variable called color. And we’re gonna have it be equal to the following. Now, before, when we created a variable, we usually just put in one thing. Like, we put in, you know, the word Hello or the string Hello or the number one or the integer one or the floating point number 1.0 or something like that.
So, we’ve always put in one number. But for the color here, we actually need a RGB value, red, green, blue. So, the way that we’re gonna do that is we’re gonna look at something called a tuple. So, to create a tuple, we’re gonna make it equal to, and then we open and close parentheses. And what this does is it nicely packages up several values for us. And so, now, we can pass values in the form of a tuple or in the form of these packaged numbers.
So, for example, to create a fully blue color, I’m gonna first put in the number zero. So, again, we’re creating R, G, B, so zero of the red value, and then comma is gonna indicate that we’re gonna have a new element. Zero of the green color, and 255, so maximum, of the blue color. So, that’s just the scale that this RGB works on.
So, what we’ve created here, rather than assigning a single number or a single word or string or whatever, we’ve created something called a tuple, which we can imagine as a package of, in this case, numbers. But it can also be a package of other things, like a package of strings for example. But in this case our color contains a tuple that contains itself three numbers. So, color is a little package that contains three numbers, that contains the number zero in its first position, it contains the number zero in its second position, and it contains the number 255 in its third position.
Now, this by itself may mean different things for different inputs, but if we’re putting it in here, then for the draw object and the rectangle method, these three values together correspond to RGB values. So, they correspond to red, green, blue values, and so, if we put this tuple, this package of three numbers in here, that’s gonna correspond to the color blue because blue is maximum.
So, the scale goes from zero to 255. And so that’s what we’re doing in here. We’re using this tuple is what it’s called. It’s just a package to help us pass several numbers in one element here. And we also see here, each element is separated by a comma. Alright, so there is one more thing that we need to do. So, now that we’ve created our rectangle, and we’ve drawn our rectangle, we need to update our display, we need to update our screen. So, we’re gonna go into pygame, and we’re going to access the display object, and in it we’re going to call the flip method. And, pretty much, what this allows us to do is it allows us to update.
So, what we’ve done now is we’ve created first a pretty much blank screen because we haven’t added anything to it. We’re going through the events. Once we finished going through the first events, we’re creating a rectangle, position zero, zero with width and height of 30, and we’re drawing this rectangle to the screen with the color blue. And now that we’ve done all that, we still need to update our display. So, if we’re pretty much entering the next frame or whatever it is not we know, and now we need to update it, so that we have this rectangle in there. And so that’s where we need this final call here of the display object in pygame, and we need to use this flip method.
So, that allows us to update our display, it allows us to update the screen that we’ve created. So, if we run this, then we see up here, we created this little rectangle. So, for me this rectangle is a little bit hidden in the top left corner. That’s just because it’s covered by this bar here. But if we go back into our rectangle that we’ve created and change the Y position to maybe 50 to move it down a little bit and then rerun this, then we see our rectangle is now this nice square that’s created right here.
Transcript Part 7
Hey everyone, it’s Max and welcome back to our Python tutorial. So again, let’s continue off just where we left last time.
If we remember where we left last time, then we can see here, if we run our module again, we’ve created this rectangle here on the screen, this blue little rectangle. And we’ve actually created it at a position of (0x,50y). And the width and height are 30.
And we also saw that if we created it at (0,0) it was little bit hidden, so that’s why I moved it down just a little bit by changing the y coordinate. One thing that you’ll also read from here is that the (0,0) location is in the top left corner. And then if we increase x and y, we’re pretty much going across the screen like that. So that’s how we change the x and y coordinates too, so that’s what we did in the end there. Just increasing y so that it’s not hidden under this bar, that’s still part of the screen and we just moved it down just a little bit.
Alright, so now we’re gonna look at how we can get our rectangle to start moving. So the first thing that we’re gonna do is we’re actually gonna redefine our x and our y coordinates. So, right now we’ll just put these here. We’ll have x be equal to 50, and y equal to 0. And actually we’re going to move these out and have them be before our loop because these are our starting coordinates, so that’s what we wanna start at. And all we’re gonna do is we’re just gonna replace our– Oh, we actually want x to be 0, sorry, and y to be 50. So we’re gonna replace our x coordinate here with the x variable that we’ve created up here. So that’s gonna be our x position, and 50 is gonna be replaced be our y condition.
So if we run this, and save of course, then we see there’s actually no difference right now, just because 0 and 50 have been replaced by x and y which still contain the values 0 and 50. So there’s no real difference. The only difference is that we now have these initial values stored, and if we want to move it then we can continuously update these.
So that’s why we’ve created them in a form of variable, rather than just hard coding them in here. So yeah, that way we can just continuously update them. And that’s also one of the uses of variables is that we can continuously refer to their previous state. Alright, good, cool. So now that we’ve done this little adjustment, created this variable here so we can see this rectangle here now in the same format but using x and y instead. Let’s learn how we can see, or how we can start to move our rectangle. So what we’re gonna do in Pygame is– Or in our coding environment here is we’re gonna go in to Pygame. And we are going to want to check if a certain key is pressed. So to do that, we’re going to go into key.
And what we’re gonna do here– so again we’re going into Pygame, where now I’m going into the key object and we’re gonna call this method that’s actually already being suggested to us here called “get_pressed”, like this. And what this does for us is it just returns to us all of the pressed keys, in a kind of form of– Well actually it’s in the form of an array or a list but we’ll see more of that in a second so that we can really understand this. But yeah, what we’re doing right now we’re just getting all of the pressed keys in the form of a long list. Alright. And we need to save that in somewhere.
So we can call that, maybe, “pressed keys is equal to the following”. So the pressed keys, are the keys that are returned to us from this get pressed method. So what exactly is being returned to us? So, what’s being returned to us is a variable– Oh actually sorry, it’s a list, also called an array. So I’ll show you an example of an array on just a quick side note here.
You can create an array like this, using open and closed square brackets. And arrays, just like true/false, which we’ve created for the color right here. It could also hold several elements, And each element is separated by a comma. So for example, our first number could be 0, the number 0, and then we can have 1.2, that’s our second element which we separate here by comma. Then we can say maybe 4.5 or something like that. We can also put in here the word or the string “Hello”.
And so that’s what an array is for us. And so we can see it’s pretty much a collection. Well it’s actually a way of storing data so it’s actually a form of a collection, or grouping relevant data, so similar data that we can access, and it’s held in this list. Alright so what we’re gonna do is we’ll print out our array here, just as a kind of short little side example we’re gonna do this just to understand what we really get when we have this get key pressed so that we really understand what we’re doing and why it is that we’re doing that.
We have to understand these arrays or these lists, so we’ll quickly cover that now. Alright so, if we run our module and we click okay, then we have the Pygame being loaded here and stuff what we’re really interested in is the output in console that we have right here.
And so we see again this list, which we see is a list because it has open and closed square brackets at the front and the end. And each element is separated by a comma. So that’s the whole array or list that we’re printing out. We can access individual elements by going, by having, this array variable here that stores our lists or our array and by putting open and closed square brackets behind and by putting, now, the index of the variable or of the element that we want to access.
So let me explain to you what the index is too, how they work. So the index works as follows. For a list that has several elements the first index or the first element has the index 0. The second element has the index one, the third element has the index two and the fourth element has the index three and so on. If we were to have a fifth element it would have the index four and so on. So the only difference is in programming we start counting at 0. So rather than the first element having the index one, the first element actually has the index 0. So that’s how indexes work like. Alright, let me just shift this back here, just a little bit.
So that’s in general how indexes look like or how they work. So to get out a certain element we have to access it in the list through calling its index. So for example, if we wanna access the first element which is the element with index 0, we put in the square brackets behind our variable name this index. And notice here we’re assigning a list to it, and here we also have the square brackets but since we don’t have this assignment here since it’s right behind our variable name that contains the list that means we wanna access that element. So if we run that, now we see if we go over here we get the output 0 which is our first element.
If we put in the index one then we get– Save and run– Then we get here the output 1.2 which is our second element if we look at the index two, then we get the output 4.5, which is our third element. And if we look at the index three, and save and run this, then we get the fourth element which has index three, since in programming we start counting at zero, which is “Hello”. So we can go up, we can also go backwards and to access, for example, the last element, we can put in here instead of three, we can put in here the value negative one. So we’re counting backwards. So this is 0, this is negative one. So here you will also get “Hello”. If we go the second last element, negative two, we get 4.5. Like this.
So that’s the basics of lists in Python. And we’ll be using this to access the elements that we get returned from here. So that’s kind of important for here. So we’ll comment this out just so that we can reference to it later, and that’s also what we’re getting back from here. And lists, or arrays are really really great because they allow us in one variable to store a bunch of information or to group a bunch of information to store it in the same variable that’s related.
And for a list that has, you know, maybe a length of 100 we don’t have to create a hundred different variables we can just create one variable that contains a list with 100 elements.
Transcript Part 8
Hey everyone, it’s Max, and welcome back to our Python tutorial.
So in the last tutorial, we saw a little bit of an introduction on arrays or Python lists, and how they work, and how they store data. And we looked at that because we looked at this pygame.key.get_pressed method which returns to us an array that has an index for each of the keys. And it allows us to check if a key is pressed or not. So that’s what we get back here is we get this huge, huge list, or this huge, huge array, of all the keys. And for each individual key, we can check if it has been pressed or not.
So, one more thing quickly before we get into looking at the keys. We first have to understand how we can access the keys. And so, we’ll again look at that by printing something. And you will have noticed that to print I can do the open and closed parenthesis or I can just put a space here. Whatever it is that you prefer. Whatever you’re used to. Or if you’re not used to anything, you can do whatever you like, as long as you’re consistent. Probably that’s best. All right, so how do we know what key is pressed? So, if we go into pygame, pygame actually has for each key certain values. So for each key there is a certain value that’s saved. And we can check that key is pressed.
So for example, if we’re gonna use the space bar, we’re gonna go into pygame, and we want to access the space bar key. So we’re gonna go into pygame. And then we’re gonna type in K, underscore, space like this. And what that does is this is the space bar key that’s saved in pygame. So this pretty much gives us the reference to that space bar key. And so if we run this, and we’ll see in a second why we’re running this, what we get back if we run this is we get back a number. And now the reason that this is important is because this number that we get from the space bar key that’s saved in pygame is actually the index of the space bar, or of the space bar key, that we get when we call this get_pressed method on keys.
So really what we get here is we get a list of all the keys. And to find the space bar, we have to look at the space bar key in this array. So for example, what we get back would be something like… And now this isn’t exactly how it’s structured. But it would be something like the up key, and then we’d have the down key, and then we have the left, and maybe then we have the space or something, and there is stuff before. Now there’s stuff after. And there’s stuff before. And again, this is probably not the exact order. But this is just to show you the example.
And so we get this huge list that contains all the keys. And to access the space bar key, the one that we’re really interested in, we have to know what index it has in this whole list that contains all the keys. And so that’s why it’s important to know that this K_SPACE is a number, and that number corresponds to the index in this list of all the keys that is returned to us. All right so, if you understand that great. If not, then we’ll just continue working with it and you’ll probably see in a second what I mean with that.
All right so, what we’re gonna do is we’re gonna do another if statement. And we’re gonna see if, and now we’re gonna look at the pressedKeys. If our pressedKeys, and now the element… So again, we get this whole list of all the keys returned to us. And the key that we want to look at is the pygame.K_SPACE. Oops sorry. K underscore space. So key underscore, and then what key it is. It’s the space key. And the reason that we do the pygame is just because everything that we’re doing is contained inside pygame. And it’s all made consistent inside of pygame. So that’s what we’re doing here.
Actually, maybe something that would be interesting to see is before we even check this if statement is just to print out what that value would be. So before we go any further, let’s just print out whatever the element in this list is, just so to really understand what we’re doing. So we’ll run this and save that one more time. And now, what we see here is we get an output, just a bunch of zeros. So we’re not pressing the space bar key. And now, let me move this over a little bit. If I press the space bar, we see all of a sudden get a bunch of ones returned here. So that’s what’s going on when I press the space bar key.
So let me just run that again just to keep that running in the background, but to stop all of this spamming of numbers. And well great. Now everything froze because of it. So let’s just force quit this. Okay and… Yeah, there we go. And while that’s quitting, okay good. Maybe we’ll also just close that. And again, force quit this so that we’ve reset everything. But yeah. So that’s pretty much what’s going on when we press the space bar key is it is continuously being checked. And we also see that it’s continuously being checked at certain frame rate and everything. And so, that’s the values that are stored in this pressedKeys.
All right. So now what we’re gonna do is we’re gonna check if pressedKeys. And now if the pressedKeys of, or if the pressedKeys is equal to the following value. So we notice that what we get returned from here is either zeros or ones. So if the value contained in pressedKeys for the space key is equal to one, then what we’re going to do is we’re going to increment our y coordinate a little bit. So we’re gonna make our block, our rectangle, move up and down.
So what we’re gonna do is we’re gonna say y is equal to. And now maybe we say y plus five or something like that. But remember there’s a shorthand for that. We can also say just plus equals five. So that’s the same thing as saying equals y plus five. So we’re just gonna increment y by five if the space key is pressed. That’s what we’re gonna do right now. And so if we run this, and remember the y coordinates is the up and down coordinates.
So if we run this, what should happen for us is once we press the space key, we see that we’re moving down. Now, this isn’t exactly what we wanted. But every time we press the space key, we see that we’re moving down, which is already a great start. Now, we have a little bit of a problem of this blue line appearing as kind of a trail. And the reason that’s happening is because we continuously draw a new rectangle. So what we’re doing, and that’s also why we save these x and y coordinates from before.
We’re continuously incrementing our y coordinate by five every time we have the space bar held down, for every tick of the computer, and we’re creating a new rectangle, and we’re drawing that rectangle to our screen again. But all of the old rectangles are still there. They’re not being removed. So that’s why we have this kind of tail that’s going on. And to avoid that, the one thing that we’re gonna do is we’re gonna go into our screen and we’re just going to access the fill method.
And again, here we’re gonna put in a color, an RGB color. And we can actually define the color black to be zero, zero, zero. So that’s the same as the tuple we defined above. So this here is the color blue, which has a value of zero red, zero green, and 255 blue. The color black has zero for every color. So again, RGB colors here. And what we’re gonna do is before we draw the rectangles to each tick, we’re just going to change the color of, and whoops sorry, one extra too many parenthesis there.
We’re just gonna change the color of our screen again back to black. So if we run our module now, we see that when I press the space bar, our rectangle is nicely moving up and down and we no longer have this tail because every tick or every frame of our game, our screen is taking on, is being completely black again, and then we draw the rectangle again. And if I move it, all of those rectangles are no longer seen because they’re completely overwritten because we’re making everything black again. We’re completely filling it up with black. And so that eliminates the tail. And since we only draw a rectangle once every tick, or every frame, we see only one rectangle on the screen here too.
And just through pressing the space bar, we can move it down in the y direction because we said here if the space bar is pressed, then we increment y by five.
Interested in continuing? Check out our newest Python course, Learn Python Programming by Making a Game.