I coded Agar.io with Python (Using Sockets/Networking and Pygame)
Key Takeaways
The video demonstrates how to code a multiplayer game of Agar.io using Python, Pygame, and Sockets for networking, allowing multiple players to connect and play from different machines on different networks. The project uses a client-server system to manage connections and log messages, and implements game balance and a scoreboard.
Full Transcript
so I'm sure many of you guys are aware the popular internet game called agario which is actually what I'm playing right now so I thought it'd be a cool idea as I used to play this game a ton to try to duplicate it using Python sockets and the module PI game and allow a bunch of my friends and actually students to play against each other on a local network or an external server so I'm gonna show you guys how I created that game what it looks like and how you guys can mess rent with the project at home alright so this is the game running right now on my computer now it's difficult to show you this playing with a bunch of people at once but I will put up some footage from actually when I was running this in the classroom with a bunch of my students but anyways you guys can see here kind of how this works it's obviously not nearly as advanced as the last version of the game that I was showing you but this is kind of my attempt to make agario in Python now this is probably maybe like six or seven hours of work so nothing too crazy and I can definitely add a ton of stuff to it but for now I decided to keep it simple and just get a working kind of prototype anyway a prototype going here so you can see that I have two clients on my computer and one of them is test player the other one is Tim and obviously if test player goes inside of Tim Tim will gain all of his mass and then he will respawn and that's kind of the gist of the game what you're trying to do is get as large as possible and you can do that obviously by eating other players or by collecting all of these dots now the way that I kind of made this game balanced is that the bigger you get the slower you are so it's much more difficult to move around I also made it so that the bigger you are the faster you lose mass so you'll notice in the server code here that we have all of these different kind of log messages coming in so the server is starting connection happened another connection happened tim's mass is depleting test players mass is depleting but the speed at which your mass depletes depends on your size so if you are you know much bigger you're gonna lose mass much faster and that's kind of the way that I tried to balance this now obviously this window is not massive which means that when you have a ton of people playing at once well if you can get someone that gets very large entraps people essentially through different halves of the game so there's a lot of work to do but I think this is kind of cool and I'm gonna show you guys some footage of well my students playing this while I kind of explain why I wanted to make this in the first place so for those of you that don't know I actually teach coding in a classroom in the summer and that's what all this footage is from here and I apologize for the poor quality but it's just filmed on my iPhone is that's all I could really do while I was there but anyways what I do is I teach kids coding and when we have breaks I like to show them some of the stuff that I made and kind of inspire them and give them some ideas on stuff that they can do so I thought it'd be a really cool idea to get them all to connect and play this game against each other and that's exactly what we're doing here you can see that some of them were having a lot of fun and they were connecting and feeding each other and teaming up and they really enjoyed the game and it was kind of almost bad that I showed them because they were just begging to play it the entire week so the way that I made this game as well a little bit different from the real agario is rounds last five minutes so as soon as you hit five minutes the game freezes you can no longer eat people you can no longer collect dots all you can do is move around and then you can have a look at the scoreboard and see who was in first so now that we kind of understand how the game works and what it is I actually created let me walk you guys through kind of the backend of this and how I actually did it in case any of you are interested and I'll also show you how you can run it in kind of some of the limitations and problems that I'm running into and you know it feel free to help me out with this and contribute to the github repo that is in the description down below if you have any fixes or things you want to add to the game and that could be a cool kind of community thing we can do with this all right so now time to talk about how this actually works so this right here is the github repo for the project and if you're interested in looking at it downloading it or even playing the project yourself what you can do is simply click the link in the description for my github find this repository and play with the code so anyways I'm gonna kind of walk you through how this works so that you guys know how to modify it so you can actually use it at home so this works on a client-server system using something called socket so I'm gonna walk you through this which is a brief little drawing here so I'm gonna represent the server within blue here with an S now the server you can kind of think of as like the heart of the game and this is what's handling kind of all of the information that's handling all the connections and its handling sending information to and from the client so receiving information from the client sending it back to the other clients now what is a client well a client is just kind of like the game running so when you saw that PI game window there that was the client so I'm just gonna draw three clients here to kind of get you guys an idea of how this really works so the clients have a series of commands that they can send to the server and based on what they send they're gonna receive back information now that information might be something like the clients score it might be something like other players positions right that's the kind of information you might be getting now in this case the type of information I'm sending is commands and what these commands are are just strings that say certain things so the first one is called get okay and get simply means excuse my handwriting here I have to plug in this cord because it's gonna die um get just means give me all the information for the game and the information for the game is like where are all those little bubbles where is other player's position where is how big is everybody how fast is everybody like all of that is the information that we need and that's gonna be sent back to us in a dictionary so say like this is the command and this is what we get back so what we get back is a list of all of the players so we have like inside of this dictionary we have a list we have one for players we have one for all those little orbs and balls and then we have another one I believe for the time that's what it is so how much time is left in the game because I don't know if I explain this but essentially the game lasts for five minutes and after at the end of five minutes um you can't eat people you can't get any larger you can't get any smaller and what happens is you can just have a look at the scoreboard and see kind of who won that round so anyways this is kind of the information we get back when we say get the other command that we have is move now what move does is it actually will send the position that each client moved to so typically the command that we're gonna be using a lot is called move now what move is again we're just sending where we move so maybe we move to position 50/50 on the screen we're gonna send that to the server and we're gonna get back this same list so right here we're gonna get back the same thing as up here when we send move so you can kind of get the point here what's happening is each client is going to be sending information to the server and pending on what kind of information it sends it's gonna get some information back now what its gonna do once against that information is kind of break it up look at it and then display that information onto the screen so that we can see it and that's happening between all of the clients and all of the yeah between all the clients to the server multiple times per second right so that means our server has to be as fast as possible to be able to handle all of this going on at once now the last command that we can send is known as ID and this simply returns to us what player ID we are now the ID is just a number that represents which player because what sometimes will happen is if you have players with the same name you can't just use that as the ID so I've just created a very arbitrary number system so that each player has an ID and the reason you need that is because when you join the server the server is gonna send back to you this player list that's gonna have like 0 1 2 3 4 all the player IDs so you have to know which one you are so you know who you know which one you're gonna be moving where you're gonna put the name like just all that kind of stuff so that's kind of the gist of how this works so anyways let me close this now and let's actually go and dig into the code just a little bit I'm gonna show you what you need to modify to make this work so first thing we're gonna do if we want to actually run this just go to CMD and type IP config now here you're gonna be looking for your ipv4 address this is if you're gonna be running on a local network so mine is 192.168.1.1 62 now I've actually made this easier for you but what you usually have to do is find this on the server machine so whatever machine you can be running the server on you're gonna want to know this number now fortunately for you guys what I've actually done is I've coded it such that when you run the server it automatically figures out what IP your dress you're on and it just runs the server on that IP so I'll show you here if I can find where this code is so agario I go to CMD Python server dot hi you can see it says server started with local IP 192.168.1.1 sorry our client code and we're gonna click Edit what we're gonna do is we're going to look for a field inside of this in it here that says host now host is going to be what we're connecting to so all we have to do is change this number to be the same as our server and once we've done that we're actually set and we're good to go and that's all we need to do to make this game work on any machine so what you're gonna do then is run the server code on the fastest machine that you have ideally make sure it's on the same network as all your other machines and on all your other machines you're just gonna run game make sure that client has that same IP address inside of you know that little field that I just showed you and that it'll be able to connect and and play and do all of that fun stuff now if you're looking to use this over a public network or you want like other people to be able to connect from different computers around the world on different networks what you're gonna do is just go on Google and just search up my IP address now that's gonna give you your public IP address and you're gonna run the code now from a public IP address now I don't recommend this because that's gonna mean that other people are gonna have your public IP address but if you want to do that you're welcome to so run it using your public IP address that means you're just gonna change the server to run on the public IP and you're gonna change the client to run on the public IP as well so with that being said that is kind of how this game works and operates I thought it was something that was really interesting and I had a lot of fun making this if you want to look into the code and kind of see why this works I've commented it quite a bit so you should be able to understand what most of this is doing and you know see if you can create something similar to it as well so with that being said I hope you guys enjoyed the video if you did make sure you leave a like and subscribe to the channel for more and I'd love to hear if any of you guys are actually going to be using this project and what you thought of it in the comments down below
Original Description
I tried coding agar.io in python using sockets, networking and the python module pygame. This project is coded using entirely python and can host multiple people playing from different machines on different networks.
Code: https://github.com/techwithtim/Agar-IO
Playlist: https://www.youtube.com/watch?v=iLHAKXQBOoA&list=PLzMcBGfZo4-nTARLniGMmigJT7P17wDDX
Get %30 off a GitPod.io subscription with the code: techwithtim
https://www.gitpod.io/
◾◾◾◾◾
💻 Enroll in The Fundamentals of Programming w/ Python
https://tech-with-tim.teachable.com/p...
📸 Instagram: https://www.instagram.com/tech_with_tim
🌎 Website https://techwithtim.net
📱 Twitter: https://twitter.com/TechWithTimm
⭐ Discord: https://discord.gg/pr2k55t
📝 LinkedIn: https://www.linkedin.com/in/tim-rusci...
📂 GitHub: https://github.com/techwithtim
🔊 Podcast: https://anchor.fm/tech-with-tim
💵 One-Time Donations: https://www.paypal.com/donate/?token=...
💰 Patreon: https://www.patreon.com/techwithtim
◾◾◾◾◾◾
⚡ Please leave a LIKE and SUBSCRIBE for more content! ⚡
Tags:
- Tech With Tim
- Python Tutorials
- agar.io
- coding agar.io with python
- python coding
- agar.io coding
- pygame
#python #sockets #networking
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from Tech With Tim · Tech With Tim · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
A* Path Finding Algorithm(Visualization)
Tech With Tim
Python Programming Tutorial #1 - Variables and Data Types
Tech With Tim
Python Programming Tutorial #2 - Basic Operators and Input
Tech With Tim
Python Programming Tutorial #3 - Conditions
Tech With Tim
Python Programming Tutorial #4 - IF/ELIF/ELSE
Tech With Tim
Python Programming Tutorial #5 - Chained Conditionals and Nested Statements
Tech With Tim
Python Programming Tutorial #6 - For Loops
Tech With Tim
Python Programming Tutorial #7 - While Loops
Tech With Tim
Python Programming Tutorial #8 - Lists and Tuples
Tech With Tim
Python Programming Tutorial #9 - Iteration by Item (For Loops Continued...)
Tech With Tim
Python Programming Tutorial #10 - String Methods
Tech With Tim
How to Overclock a NVIDIA GPU
Tech With Tim
Python Programming Tutorial #11 - Slice Operator
Tech With Tim
Python Programming Tutorial #12 - Functions
Tech With Tim
Python Programming Tutorial #13 - How to Read a Text File
Tech With Tim
Python Programming Tutorial #14 - Writing to a Text File
Tech With Tim
Python Programming Tutorial #15 - Using .count() and .find()
Tech With Tim
Python Programming Tutorial #16 - Introduction to Modular Programming
Tech With Tim
Python Programming Tutorial #17 - Optional Parameters
Tech With Tim
Python Programming Tutorial #18 - Try and Except (Python Error Handling)
Tech With Tim
Python Programming Tutorial #19 - Global vs Local Variables
Tech With Tim
Python Programming Tutorial #20 - Classes and Objects
Tech With Tim
Cool VBS Script to Prank Your Friends!
Tech With Tim
How to Overclock an AMD GPU
Tech With Tim
Best GPU'S For Mining Ethereum (2018)
Tech With Tim
Recursion and Memoization Tutorial Python
Tech With Tim
Ethereum Mining Rig - Hardware Guide
Tech With Tim
Pygame Tutorial #1 - Basic Movement and Key Presses
Tech With Tim
How to Install Pygame (Windows 8/10)
Tech With Tim
How to Trade Your Cryptocurrency (Bitcoin, Ethereum etc.) For Cash!
Tech With Tim
How to Mine Ethereum 2018 - WORKING (Super-Easy)
Tech With Tim
Microphone Comparison - $10 Mic vs $150 Mic (Blue Yeti USB)
Tech With Tim
Pygame Tutorial #2 - Jumping and Boundaries
Tech With Tim
Pygame Tutorial #3 - Character Animation & Sprites
Tech With Tim
Pygame Tutorial #4 - Optimization & OOP
Tech With Tim
OBS Studio Tutorial - Best OBS Settings
Tech With Tim
Linear Search Algorithm - Python Example and Code
Tech With Tim
Make Any Mic Sound AMAZING! (WITH OBS)
Tech With Tim
Binary Search Algorithm - Python Example & Code
Tech With Tim
Pygame Tutorial #5 - Projectiles
Tech With Tim
Pygame Game - Mini Golf
Tech With Tim
Pygame Tutorial - Projectile Motion (Part 1)
Tech With Tim
Pygame Tutorial - Projectile Motion (Part 2)
Tech With Tim
Pygame Tutorial #6 - Enemies
Tech With Tim
Pygame Tutorial #7 - Collision and Hit Boxes
Tech With Tim
Pygame Tutorial #8 - Scoring and Health Bars
Tech With Tim
Cloud Mining vs. Hardware Mining - 2018
Tech With Tim
How to Install Pygame on Mac OSX (Fast-Simple)
Tech With Tim
Pygame Tutorial #9 - Sound Effects, Music & More Collision
Tech With Tim
Pygame Tutorial #10 - Finishing Touches & Next Steps
Tech With Tim
How to Fade Your Screen in Pygame [CODE IN DESCRIPTION]
Tech With Tim
How to Create a Button in Pygame [CODE IN DESCRIPTION]
Tech With Tim
Pygame Side-Scroller Tutorial #1 - Scrolling Background/Character Movement
Tech With Tim
Pygame Side-Scroller Tutorial #2 - Random Object Generation
Tech With Tim
Pygame Side-Scroller Tutorial #3 - Collision
Tech With Tim
Pygame Side-Scroller Tutorial #4 - Scoring and End Screen
Tech With Tim
How to Create A Message Box in Python - Tkinter
Tech With Tim
Is Ethereum Mining Still Profitable - Is It Worth It (April 2018)
Tech With Tim
How to Run MAC OSX on a WINDOWS PC (Clover Boot-loader)
Tech With Tim
Programming Problem #1 - Alphabet Soup (Beginner/Novice)
Tech With Tim
More on: Tool Use & Function Calling
View skill →Related Reads
📰
📰
📰
📰
Claude Sonnet 5 Just Launched. Is It Actually Better Or Just Newer?
Medium · AI
Claude Sonnet 5 Just Launched. Is It Actually Better Or Just Newer?
Medium · Machine Learning
Claude Sonnet 5 Just Launched. Is It Actually Better Or Just Newer?
Medium · LLM
Claude Sonnet 5 Didn’t Just Get Smarter. It Changed the Economics of AI.
Medium · LLM
🎓
Tutor Explanation
DeepCamp AI