Creating a Client-Server Multiplayer Game using Python and Pygame: A Step-by-Step Guide
**Threading**
So, it's going to say connected to address... It's going to start a new thread and then this loop will actually keep running while this function is running at the same time. That's how threading works okay. So I have my current ID in my position now. This little array or array list... I've been doing too much Java recently. Is what can you guys see? Let's make the webcam smaller essentially. What this is going to do is it's just going to store the positions of the two different characters so we can again send that information back and forth to both clients.
So, when we first connect, it's going to send a little message with the user's ID... either zero or one. Then we can then store that and I'll show you that in a second. Then in here what's going to happen is pretty much we're just going to look for information being sent and then we're going to send information back to or we're going to receive information and we're going to send information back to the user. And the information that we send back is just going to be the other person's position. Okay, the other player's position.
So in here... uh that's kind of what I'm doing here. I'm not really going to talk about all of that. Uh yes so now let's talk about the game. So this is the class that on all this does is... it's actually just like the game code. So there's not really any like networking stuff in here that you guys probably wouldn't understand other then in my main game I make a instance of network which is a class that I'll talk about in just a second.
And then every time I move... uh you can see where do I do this? h i do it somewhere. Uh canvas.update.send data here it is okay. So in send data this is what's going to send my current position in a certain form to the server so the server can read that and then use that.
Now, let's talk about the network class. This is just a really simple class and all this does is set up again on the client side connecting to the server. So it says... uh we're setting up a socket giving the host we have the port and then again we're just getting the address to the host and the port.
And then we're just going to call our little connect method here and all this is going to do is connect to the given address which is this right. And then we're going to be receiving data from the server... in this case what we're doing is we're getting the ID so our current ID so we know when we send information to send it with a zero or one before it.
And then we have this send method here and what this is going to do is we'll send information back to the server. And then receive some kind of reply... and use that in some uh some way. Then this run method or this run file all this does is Run the game. So essentially, that is how this networking game works.
**Game Implementation**
Obviously I have this in like a really simple game but you can apply this if you just use my network class and maybe even my server class into any game you want. And as long as you have the positions of different characters then you can essentially do whatever you want because if you think about creating a multiplayer game not online well you just check between collision between two characters using their positions.
And all you really need is the character's position... maybe you might want to pass something like Health or like some other information like score for each character but that's really easy to do once you know how to send information back and forth to the server. Which you can kind of figure out if you're a bit more advanced and you read through this.
So, anyways this has kind of been it for this client-server U python game. I just want to show you guys this get you exposed to it understand that you can do stuff like this with pygame or py game with python. This is py game though... um if you guys want to see more stuff like this and you want to see like a full tutorial on how I would go about actually implementing this into a game and using in like a full P game let me know cuz I'm happy to do some stuff like that.
And for a lot of you guys that have been waiting uh the machine learning tutorials are coming very soon. I'd say within the next 2 weeks you should start to see like the intro video I've just begun writing them now... yeah so once Java is done those should be out with that being said if you guys enjoyed the video please make sure you leave a like And subscribe and I will see you again in another one.
"WEBVTTKind: captionsLanguage: enhey guys and welcome back to another YouTube video so in today's video I'm going to be showing you how I created a online multiplayer game using python now this is not really going to be a full tutorial per se but I'm going to show you some of the tools and uh things that I use to do this and hopefully lead you on the right track if you're attempting to do something like this now before I start all of the code that I go through in this video will be available on GitHub so you can check either the pinned comment or the description for that feel free to play around with it take whatever code you want it's not copyrighted like use it for whatever you want I don't care um and also a quick plug I just recently started a Twitter um it's at techwithtim with 2m so that's going to be in the description below make sure you guys go follow that if you want to hear some exclusive updates uh be a part of some polls uh and give your feedback to the channel so that I can incorporate that into my future videos now also I have a Discord server if you guys want help with anything please uh join that I already have close to 100 people on that and that's been amazing people asking questions and talking with me me so if you guys would like to be a part of that uh please don't hesitate to join now pretty much what I'm doing here and what this kind of like game is is just a really simple there's just two red blocks on the screen and essentially I have actually I'll show you I have my laptop here and what's going to happen is I'm going to be running a program on my laptop and while I run it on there it's going to run simultaneously on my uh PC which I'm working on right now so pretty much when I move something on there it's going to move on my PC and you'll see that I don't have them like wired up this is working over the uh the local internet connection or server whatever you want to call it that we have now before I go into all of this code which you can see that I'm scrolling through uh I want to talk about a client server system really quick and give you guys some information about that so essentially what you do when you create something that's online or over a network is you have something called a client and something called a server now the client is what you actually see or what the user actually sees and that is what runs on every single machine so actually let me just get up a uh a little drawing window here so I can kind of show you up with a picture all right so let's just say here we're going to have uh two clients and there'll be these little red boxes okay now we have when we have two clients we have um one server okay so we can have as many clients as we want in my case you can only have two because it's just a two-player game but these clients are what you see and what the user sees and what is running the game um and displaying everything to like someone's screen okay now the server is what's translating information between the clients and what's uh holding stuff and really it's just translating information so essentially what happens when we run a game is every time or at least this is what happens in my program uh the client does something so say you click an arrow key it's going to send that information to the server and then that server is going to send the information back to all of the clients okay so every time I move on say client one so I say that's C1 then it is going to send information to client 2 saying hey we moved okay and then same thing here with client 2 client 2 is going to send information to the server and then the server is going to send information back to client one now you can imagine if we have a lot of different clients on here um there's a ton of information going back and forth now this server too uh has to be coded right so we can send information in different ways you can send them in strings um but typically we send something called encoded information so that means we have like say in my game we have a bunch of positions right so we have like 00001 and what we're going to be doing is we're going to be sending a bunch of positions to the server in an encoded form it's going to decode them look at it encode them again and then send them back to our other client where they can then be decoded and used this is just to keep everything safe when you're transferring things over the network and this is what python requires so that's a little bit about how clients and server work so essentially client is what runs on your machine server is kind of what's running at one instance and all of your clients are going to connect to that send information back and forth and that's how the game essentially works so what I'm using is uh in Python I'm using something called sockets and threading to do this okay so you know what maybe I'll actually uh show you how this works and then run through the code and you'll get a better idea of what I mean so essentially since we have a client in a server that means we have to be running a server instance now typically you're going to run a server instance on a different machine than uh your actual game is running on but in my case I'm going to run the server on this machine client on this machine and then a client on my laptop which I'll bring up and I'll bring my face cam a bit bigger so you can see that okay so let's do that now uh change it to that okay there we go wow all right that's really big okay so I'm going to run server and you can see here in my uh what do you call console it just says waiting for connection so first I will run an instance of the game on my PC here and just make sure I get it in frame okay so there we go this is what the game looks like you can see I each have red squares uh that other red square is going to be player two and he'll be able to move that on my laptop so let me grab my laptop okay so I'm just going to quickly run uh sorry this is not the most elegant way to do this but I want to show you that I'm actually doing it on two different computers you can see that now it shows up on here and watch I'm going to be hitting the keys on my uh desktop and on my laptop you can see the Red Square moving around now other way around if I hit the keys on my laptop you can see that the red square is moving on my PC now this is happening happening on my local network I don't have like an external server anywhere um but yeah that's pretty cool and if you guys can obviously apply this to make much more complicated games as I'm just doing stuff with a few red squares so let's go ahead and close this and I'll show you what the uh the server looks like now okay so you can see here this is my server and the whole time it was running uh it was giving us a bunch of output so we could see what everything looks like so in this case it says connected to and then it said whatever the host is and like the specific Port okay and then it would say what information it was sending so in this case while I wasn't doing anything it was just sending like static position positions but as we go down here you can see that what's happening is it's sending the position of a certain ID now what I do is I have ID Z represent one of the clients and ID one represent the other one and then it sends the positions of both of these uh IDs and clients and it receives them constantly so it can send it to the other client so it knows where to move that red square so now I'm just going to quit this server as you see um it says connection closed because we both left the uh the server so I'll quit that for now and I'll start running through kind of how some of this code works so that if you guys are playing around with it on GitHub you understand you can apply this to your own games so essentially um we have to have one main file which is going to be our server file so this what I'll go through first and remember how I had to run this before my games could Connect into the server and actually work together so uh I'm using sockets and threading now this is pretty Advanced I'm not really going to talk about how all of them work but pretty much I'm just setting up a socket connection defining my server my Port um and then binding my server to my port okay then I'm ENT essentially listening for connections to the server and once I get a connection uh then I'm going to do something you'll see that in a second so this is going to hold here until we get um or this sorry what this actually does is say we can have a maximum of two connections it says waiting for a connection and then down here in this while true Loop it will keep getting all different connections now essentially what I do here is every time I get a new connection up to two right uh I'm going to start a new thread now this thread is just going to continuously run in the background um until eventually the person disconnects from uh the server okay so it's going to say connected to address it's going to start a new thread and then this Loop will actually keep running while this function is running at the same time and that's how threading works okay so I have my current ID in my position now this little array uh or array list I've been doing too much Java recently is what can you guys see this right let's make the webcam smaller essentially what this is going to do is it's just going to store the positions of the two different characters so we can again send that information back and forth to both clients so what it's going to do here when we first connect is send a little message with the users's ID um either zero or one then we can then store that and I'll show you that in a second then in here what's going to happen is pretty much we're just going to look for information being sent and then we're going to send information back to or we're going to receive information and we're going to send information back to the user and the information that we send back is just going to be the other person's position okay the other player's position so in here uh that's kind of what I'm doing here I'm not really going to talk about all of that okay uh yes so now let's talk about the game so this is the class that on all this does is uh it's actually just like the game code so there's not really any like networking stuff in here that you guys probably wouldn't understand other then in my main game I make a instance of network which is a class that I'll talk about in just a second and then every time I move uh you can see where do I do this h i do it somewhere uh canvas. update send data here it is okay so in send data this is what's going to send my current position in a certain form to the server so the server can read that and then use that now parse data is going to get the information from the server and read it in a form that uh we can understand and then we can then draw the other character in okay so that's about how that works and then network is just a really simple class and all this does is set up again on the client side connecting to the server so it says uh we're setting up a socket giving the host we have the port and then again we're just getting the address to the host and the port and we're just going to call our little connect method here and all this is going to do is connect to the given address which is this right and then we're going to be receiving data from the server and in this case uh what we're doing is we're getting the ID so our current ID so we know when we send information to send it with a zero or one before it and then we have this send uh method here and what this is going to do is we'll send information back to the server and then receive some kind of reply and use that in some uh some way then this run method or this run uh file all this does is Run the game so essentially that is how this networking game works now obviously I have this in like a really simple game but you can apply this uh if you just use my network class and maybe even my server class into any game you want and as long as you have the positions of different characters then you can essentially do whatever you want because if you think about creating a multiplayer game not online well you just check between collision between two characters using their positions and all you really need is the character's position maybe you might want to pass something like Health uh or like some other information like score for each character but that's really easy to do once you know how to send information back and forth to the server which you can kind of figure out if you're a bit more advanced and you read through this so anyways this has kind of been it for this client server U python game I just want to show you guys this get you exposed to it understand that you can do stuff like this with py game or py game with python this is py game though um if you guys want to see more stuff like this and you want to see like a full tutorial on how I would go about actually implementing this into a game and using in like a full P game let me know cuz I'm happy to do some stuff like that and for a lot of you guys that have been waiting uh the machine learning tutorials are coming very soon I'd say within the next 2 weeks you should start to see like the intro video I've just began writing them now uh yeah so once Java is done those should be out with that being said if you guys enjoyed the video please make sure you leave a like And subscribe and I will see you again in another onehey guys and welcome back to another YouTube video so in today's video I'm going to be showing you how I created a online multiplayer game using python now this is not really going to be a full tutorial per se but I'm going to show you some of the tools and uh things that I use to do this and hopefully lead you on the right track if you're attempting to do something like this now before I start all of the code that I go through in this video will be available on GitHub so you can check either the pinned comment or the description for that feel free to play around with it take whatever code you want it's not copyrighted like use it for whatever you want I don't care um and also a quick plug I just recently started a Twitter um it's at techwithtim with 2m so that's going to be in the description below make sure you guys go follow that if you want to hear some exclusive updates uh be a part of some polls uh and give your feedback to the channel so that I can incorporate that into my future videos now also I have a Discord server if you guys want help with anything please uh join that I already have close to 100 people on that and that's been amazing people asking questions and talking with me me so if you guys would like to be a part of that uh please don't hesitate to join now pretty much what I'm doing here and what this kind of like game is is just a really simple there's just two red blocks on the screen and essentially I have actually I'll show you I have my laptop here and what's going to happen is I'm going to be running a program on my laptop and while I run it on there it's going to run simultaneously on my uh PC which I'm working on right now so pretty much when I move something on there it's going to move on my PC and you'll see that I don't have them like wired up this is working over the uh the local internet connection or server whatever you want to call it that we have now before I go into all of this code which you can see that I'm scrolling through uh I want to talk about a client server system really quick and give you guys some information about that so essentially what you do when you create something that's online or over a network is you have something called a client and something called a server now the client is what you actually see or what the user actually sees and that is what runs on every single machine so actually let me just get up a uh a little drawing window here so I can kind of show you up with a picture all right so let's just say here we're going to have uh two clients and there'll be these little red boxes okay now we have when we have two clients we have um one server okay so we can have as many clients as we want in my case you can only have two because it's just a two-player game but these clients are what you see and what the user sees and what is running the game um and displaying everything to like someone's screen okay now the server is what's translating information between the clients and what's uh holding stuff and really it's just translating information so essentially what happens when we run a game is every time or at least this is what happens in my program uh the client does something so say you click an arrow key it's going to send that information to the server and then that server is going to send the information back to all of the clients okay so every time I move on say client one so I say that's C1 then it is going to send information to client 2 saying hey we moved okay and then same thing here with client 2 client 2 is going to send information to the server and then the server is going to send information back to client one now you can imagine if we have a lot of different clients on here um there's a ton of information going back and forth now this server too uh has to be coded right so we can send information in different ways you can send them in strings um but typically we send something called encoded information so that means we have like say in my game we have a bunch of positions right so we have like 00001 and what we're going to be doing is we're going to be sending a bunch of positions to the server in an encoded form it's going to decode them look at it encode them again and then send them back to our other client where they can then be decoded and used this is just to keep everything safe when you're transferring things over the network and this is what python requires so that's a little bit about how clients and server work so essentially client is what runs on your machine server is kind of what's running at one instance and all of your clients are going to connect to that send information back and forth and that's how the game essentially works so what I'm using is uh in Python I'm using something called sockets and threading to do this okay so you know what maybe I'll actually uh show you how this works and then run through the code and you'll get a better idea of what I mean so essentially since we have a client in a server that means we have to be running a server instance now typically you're going to run a server instance on a different machine than uh your actual game is running on but in my case I'm going to run the server on this machine client on this machine and then a client on my laptop which I'll bring up and I'll bring my face cam a bit bigger so you can see that okay so let's do that now uh change it to that okay there we go wow all right that's really big okay so I'm going to run server and you can see here in my uh what do you call console it just says waiting for connection so first I will run an instance of the game on my PC here and just make sure I get it in frame okay so there we go this is what the game looks like you can see I each have red squares uh that other red square is going to be player two and he'll be able to move that on my laptop so let me grab my laptop okay so I'm just going to quickly run uh sorry this is not the most elegant way to do this but I want to show you that I'm actually doing it on two different computers you can see that now it shows up on here and watch I'm going to be hitting the keys on my uh desktop and on my laptop you can see the Red Square moving around now other way around if I hit the keys on my laptop you can see that the red square is moving on my PC now this is happening happening on my local network I don't have like an external server anywhere um but yeah that's pretty cool and if you guys can obviously apply this to make much more complicated games as I'm just doing stuff with a few red squares so let's go ahead and close this and I'll show you what the uh the server looks like now okay so you can see here this is my server and the whole time it was running uh it was giving us a bunch of output so we could see what everything looks like so in this case it says connected to and then it said whatever the host is and like the specific Port okay and then it would say what information it was sending so in this case while I wasn't doing anything it was just sending like static position positions but as we go down here you can see that what's happening is it's sending the position of a certain ID now what I do is I have ID Z represent one of the clients and ID one represent the other one and then it sends the positions of both of these uh IDs and clients and it receives them constantly so it can send it to the other client so it knows where to move that red square so now I'm just going to quit this server as you see um it says connection closed because we both left the uh the server so I'll quit that for now and I'll start running through kind of how some of this code works so that if you guys are playing around with it on GitHub you understand you can apply this to your own games so essentially um we have to have one main file which is going to be our server file so this what I'll go through first and remember how I had to run this before my games could Connect into the server and actually work together so uh I'm using sockets and threading now this is pretty Advanced I'm not really going to talk about how all of them work but pretty much I'm just setting up a socket connection defining my server my Port um and then binding my server to my port okay then I'm ENT essentially listening for connections to the server and once I get a connection uh then I'm going to do something you'll see that in a second so this is going to hold here until we get um or this sorry what this actually does is say we can have a maximum of two connections it says waiting for a connection and then down here in this while true Loop it will keep getting all different connections now essentially what I do here is every time I get a new connection up to two right uh I'm going to start a new thread now this thread is just going to continuously run in the background um until eventually the person disconnects from uh the server okay so it's going to say connected to address it's going to start a new thread and then this Loop will actually keep running while this function is running at the same time and that's how threading works okay so I have my current ID in my position now this little array uh or array list I've been doing too much Java recently is what can you guys see this right let's make the webcam smaller essentially what this is going to do is it's just going to store the positions of the two different characters so we can again send that information back and forth to both clients so what it's going to do here when we first connect is send a little message with the users's ID um either zero or one then we can then store that and I'll show you that in a second then in here what's going to happen is pretty much we're just going to look for information being sent and then we're going to send information back to or we're going to receive information and we're going to send information back to the user and the information that we send back is just going to be the other person's position okay the other player's position so in here uh that's kind of what I'm doing here I'm not really going to talk about all of that okay uh yes so now let's talk about the game so this is the class that on all this does is uh it's actually just like the game code so there's not really any like networking stuff in here that you guys probably wouldn't understand other then in my main game I make a instance of network which is a class that I'll talk about in just a second and then every time I move uh you can see where do I do this h i do it somewhere uh canvas. update send data here it is okay so in send data this is what's going to send my current position in a certain form to the server so the server can read that and then use that now parse data is going to get the information from the server and read it in a form that uh we can understand and then we can then draw the other character in okay so that's about how that works and then network is just a really simple class and all this does is set up again on the client side connecting to the server so it says uh we're setting up a socket giving the host we have the port and then again we're just getting the address to the host and the port and we're just going to call our little connect method here and all this is going to do is connect to the given address which is this right and then we're going to be receiving data from the server and in this case uh what we're doing is we're getting the ID so our current ID so we know when we send information to send it with a zero or one before it and then we have this send uh method here and what this is going to do is we'll send information back to the server and then receive some kind of reply and use that in some uh some way then this run method or this run uh file all this does is Run the game so essentially that is how this networking game works now obviously I have this in like a really simple game but you can apply this uh if you just use my network class and maybe even my server class into any game you want and as long as you have the positions of different characters then you can essentially do whatever you want because if you think about creating a multiplayer game not online well you just check between collision between two characters using their positions and all you really need is the character's position maybe you might want to pass something like Health uh or like some other information like score for each character but that's really easy to do once you know how to send information back and forth to the server which you can kind of figure out if you're a bit more advanced and you read through this so anyways this has kind of been it for this client server U python game I just want to show you guys this get you exposed to it understand that you can do stuff like this with py game or py game with python this is py game though um if you guys want to see more stuff like this and you want to see like a full tutorial on how I would go about actually implementing this into a game and using in like a full P game let me know cuz I'm happy to do some stuff like that and for a lot of you guys that have been waiting uh the machine learning tutorials are coming very soon I'd say within the next 2 weeks you should start to see like the intro video I've just began writing them now uh yeah so once Java is done those should be out with that being said if you guys enjoyed the video please make sure you leave a like And subscribe and I will see you again in another one\n"