Python Online Game Tutorial #6 - Online Rock Paper Scissors p.1
Key Takeaways
This video tutorial demonstrates how to create an online Rock Paper Scissors game using Python, sockets, and Pygame, with a client-server architecture and multiplayer capabilities. The game tracks wins, losses, and ties, and features a menu screen for starting new games or leaving.
Full Transcript
hey guys and welcome back to another online game tutorial with Python now I apologize for not having posted this for the past three days I guess but I've actually just been working on the code for what we're about to create and I want to make sure that there wasn't a massive bugs or error so that I didn't slow us down when I was teaching you guys how to do this and walking you through the code so like I mentioned in the last video I'm pretty much gonna be gutting everything we already did we'll keep a little bit of it but we're just gonna really add to a bunch of that and just yeah we're gonna be gutting most of it and we're actually working to create online rock-paper-scissors now I know this sounds a kind of a lame game but let me show you because it's actually pretty complex and if you can understand how to make this game then you're gonna be able to understand how to make any other kind of online game because the principles are the same in terms of sending information waiting for players to go and there's a lot of different things and you might not think about that we actually have to do to code something like online rock-paper-scissors okay so let's this is client number one I'm just running another PI game window or PyCharm window with the server running on it and you can see obviously it's been oversized but it says waiting for player okay so I'm gonna launch another client and then excuse me you can see when I launch that it loads both of them up into the game and it says your move opponents your move opponents and currently it's waiting so I believe this one's player two and this one's player one so if I make a move here on let's say rock it'll lock in my move it says your move is rock and then over here it says obviously the opponent's move is locked in because it's like looking for this guy's move and now if he makes a move like let's say scissors then it says you win you lost and then it just resets and you can keep playing games now I'm also gonna be adding more to this this is just like the beta version I wanted to make sure I get the tutorials out quickly to you guys I'm gonna have wins ties and losses keeping track and top hand corner and when you load in you're gonna be brought to a menu screen which will allow you to like start a new game or to leave or we'll add that later as we go but this is the main functioning game and you can see obviously it's working well tie game and it restarts this also allows for unlimited amount of clients to play so for example if I launch another two windows you can see these guys now have their own game it's kind of difficult cuz I can't really get four on the screen but anyways if I go like scissors paper that works independently of these games and these games can kind of play at their own time which is really interesting and really cool and if you disconnect one of them it automatically disconnects the pair and that's just because obviously you can't play against no one right so yeah okay sweet so that's that's let's start getting into the code and there is quite a bit of it so this is probably I'm probably just gonna code all of it in one video and then split it up into sections so if the sections are kind of like choppy in terms of I don't do an intro or an outro that's why cuz I'm just gonna code all of it and if it's too long I'll chop it up into a few videos okay so first thing we're gonna do is were actually gonna code a game class okay and this is just gonna be responsible for holding all the information for our game that we need so for example did player 1 go yet 2 player to go yet what move did player 1 make one move to player 2 make our both of them connected to the server information like that and you guys will see how much information we actually need it's also gonna store things like keeping track of who won or who lost how many ties how many wins so we're gonna do with this game class so let's start making it so class game I'm doing this in its own file by the way just called game you are gonna need to insan file because it's gonna have to be accessed by both the client and the server ok we're gonna define our initialization in here we're gonna take ID I'm gonna say self thoughts p1 wedge equals false self dot p2 went equals false and obviously you guys know what this is gonna do it's just gonna stand for if player 1 has made a move or not if player 2 is made a move we're gonna do self taught ready equals false if I could type that correctly we're also gonna add self thought ID so self that ID equals ID and this is just gonna stand for the current games ID so each game we create is gonna have its own unique numeric ID so that we can determine who is like what clients are a part of what game and what not we're gonna do self dot moves equals and then we'll just do none and not in here cuz currently the moves are none but we'll just store two positions so we can change that we'll say self thought wins equals and then zero-zero obviously this one is gonna stand for player one this one's gonna stand for player two will say self thought ties equals zero that's all we need for the anit so next one is gonna say get underscore player underscore move now what this is gonna do is exactly what it says is just gonna get I don't know how I added that there it's just gonna get the player move that we asked for so we're going to take P which is going to be either a 0 or 1 and we're gonna do is simply return self dot moves and P and just remind ourselves we'll say that P is in the range of 0 and 1 so we're only going to take value 0 or 1 and then we're going to return a move so let's say move here ok and there's that's just to remind ourselves that we have to pass here or 1 obviously 0 is gonna represent player 1 and 1 is gonna represent player 2 okay next one play so this one's a bit more complicated not crazy we're gonna take play we're gonna take a player and we're gonna take a move and what this is gonna do is it's simply going to update our moves list with that players move pretty straightforward so what we're gonna do here is we're just gonna say if player equals equals 0 then what we'll do is we'll say self dot moves 0 equals move Oh actually you know what let's do this sorry I'm just looking at my other screen right now we'll say self dot moves player equals move but now what we have to do is based on the player we have to update if p1 went or p2 went ok so what we'll do in here is if it's player 1 obviously we'll do p1 went equals true I'm gonna need a self before that and then we'll call we'll just do a little else here because it's not player 0 must be player 1 which will say self dot Pichu want if I could spell went equals trip so that will just keep track of if we've gone or not sweet next method this one's really easy it's gonna be called connected and it's just gonna tell us if the two players are currently connected to the game if they are it'll allow us to load in and that's how we can determine whether we should show waiting for player or not on the screen right so we'll say return self dot ready and that's just going to tell us obviously if we're ready and that'll be updated from the server side which we'll do later next method define both went this is just simply gonna return if both of our players left so to do that we're just gonna say self thought p1 once and why can I not spell that word hand itself thought p2 went like that okay next one is winner this one is a bit more complicated but it's just gonna keep track of where it's actually gonna tell us who's won the game so if we call this method we're assuming that both players have gone we're gonna check their moves excuse me against one another and see if they want so we're actually gonna have to check nine possible cases cuz there's three moves each player could do three times three nine so what we'll start by doing is what I say p1 equals self dot moves 0 dot upper and that's here the reason we're doing this is because we just want to get the first letter of the move because the move is going to be stored as rock paper or scissors the string and it just gonna be easier for us to type out for example R or s or what do you call it or P to check the moves as opposed to having to check the entire word so we're just going to get that first letter by doing move 0 we're gonna upper it and then we're gonna take that first letter we're gonna do the exact same thing for p2 except obviously we're going to need oh I don't know what I did there you're going to need moves 1 dot upper and now we can start checking to see who's 1 so we're going to stay to start winner is equal to negative 1 now that's because there could be no winner there could be a tie so if it's tied we're gonna say negative 1 if player 1 is the winner it's gonna be 0 if player 2 is the winner it's gonna be 1 okay so we're gonna do is going to say if p1 equals equals R and p2 equals equals Scissors what we'll do is we'll say winner equals 0 because player 1 1 that will say L if p1 equals equals s and p2 equals equals R then we'll say winner equals 1 believe do another Elif and I know this tedious but this is the way you have to check for rock-paper-scissors I don't think there's an easier way to do it if you know an easier way let me know and p2 equals equals R and if you guys don't want to type this you can always copy it from my website check with Tim dotnet okay so say winner equals paper beats rock so beat winner equals 1 or 0 sorry we'll say L if p1 equals equals R and p2 equals equals P then winner equals 1 and we've just got two last ones to check here so we'll say L if p1 equals equals s and p2 equals equals R and then p2 is the winner so winner equals 1 believe that's oh sorry I might have messed this up this should be P thanks so P rock rock paper scissors paper yeah when R equals 0 ok L if p1 equals equals paper and p2 equals equals scissors then winner equals 1 and I believe that should be correct 1 2 3 4 5 6 okay sweet and then the other cases are if it's a tie so if none of this is the case then they must have tied and then all we're gonna do is simply return winner like that ok and one very last method then we're actually done with this class we can move to something else is define reset went and all this is going to do is say self-thought p1 went equals false and self thought p2 went equals false pretty straightforward and this is the game class I know what kind of sped through this but it's pretty trivial how this works we just need to get this out of the way so we can start coding some other stuff okay sweets we've done this class next thing I think we want to work on is network so network actually you guys are gonna have to modify yours to look like mine now get P and what do you call it so this first half so an it and get P are gonna be the same as what we had previously the only thing that's changed is connect and this end so in connect instead of what he call it like unpicking an object so like pickled alt loads what we're simply doing we're gonna just connect to the client like we did before but instead of unpicking it we're just gonna decode it I'm gonna say self dot client receive 2048 dot decode would return that value and that's because when we initially connect to the server what we're gonna get from the server is our player number which means we're either player 0 or we're player 1 now that's important because that's going to determine where on the screen we're drawing certain things and how we're sending information back to the server and updating player 1 player 2 right because technically each player thinks that their player 1 but each one needs to be assigned either a 0 or 1 by the computer so we know where to store information right ok so that's how we modify that just do decode instead of pickling sending I believe is the same except what we're gonna do is instead of pickling an object to send we're simply gonna send a string and we're gonna load an object so that means we're gonna send string data to the server and we're gonna receive back object data so when we receive something we have to pickle that loads it in but when we're sending it we just have to encode the string ok so just make sure it looks like this I don't think I need to go through this we've already done this for the past two three videos and that's the network class so game and network are done the next things to do our server and client now server and client are a bit more complicated so inside of server I guess we could do this first because it doesn't really depend on the client to work where the client kind of depends on the server we're gonna change a bunch of things so this is what it mine looks like now I've kind of gutted the entire threaded client I got rid of most of the stuff like most of the other stuff I just left this beginning thing so the server IP the port the socket connecting listening waiting for connection so by the way some of you were saying you're having issues with FS don't listen you can just make this zero it doesn't really matter what's in here and some of you were saying like you're having issues just you can just delete it and type it in again and apparently that works that's what someone said so I don't know don't ask me about that but if you're writing it to issues do that okay so what we're gonna do now is we want to make it so you can have unlimited connections at once now that means we're gonna have to have unlimited games running at the same time so before what we're doing when we had those players moving around the screen we were just wearing like player 1 player 2 and we just had a list that had two entries that's how we were doing that what we're gonna do now is we're gonna have a list that contains a bunch of different games and those games actually you're sorry it's gonna be a dictionary those games will be accessed by their ID and you guys will see how this works it's a bit complicated but just follow along and yeah so I gotta just open up my other file so don't make any mistakes here okay so what we're gonna do is we're gonna do connected equals set we're gonna define some variables will talk about these two games equals a blank dictionary and ID count equals zero so the reason we're adding these is games this dictionary is going to store our games so it's gonna have an ID as a key and the game as like a game object like this okay as the value this connected is just gonna store the IP addresses of the connected clients we're starting to set just so it's easier to access later I don't actually know if we use this we might but we'll see you okay ID count obviously is just gonna keep track of our current ID so that means what game we should recreate so we don't override games and say like two games have the same ID because obviously we can't have that happening okay so that's fine for that we're not gonna deal with anything threaded client right now we're gonna go down to our while loop and this is where we're gonna create new games based on new people joining or possibly delete games actually will delete games from threaded client so we're gonna do right now is when someone connects this this runs right and we run a new so like once we accept a connection everything after this runs so what we're gonna do is we're gonna say ID count plus equals one if you notice me looking away I'm just looking at my other screen to make sure I don't make any mistakes on this now what id count plus equals one is gonna do obviously is it's gonna keep track of how many people are connected to the the server at once because obviously right like once this happened we can except then we go down the while loop we start a new thread and then we wait for another connection so we're just gonna keep track of that what we're gonna do is we're gonna say P equals zero just standing for the current player we're going to say game ID equals and this is gonna be weird but just follow along with me we could ID count - one integer division - now what this is gonna do is essentially every two people that connect to the server we're gonna increment game ID by one and what game ID is gonna be or every we'll say it's yeah well add it by one what game ID is gonna do is keep track of what ID our game is gonna be so like for example if we have ten people connected to the server we're gonna have five games right so that's what this line of code is doing for us it's keeping track of how many games or if we need to add a new game because obviously if we have like six people connected all of them are gonna be playing each other a seventh person connects well it doesn't have a game to join we have to create a new game for it to join hopefully that makes sense okay so what we'll do next and say if I D count modulus two equals equals one and what this is gonna stand for is if you're gonna be player one or player two and if this happens is actually we need to create a new game because this means that we don't have a pair for our new player so for example like say this numbers three that means two people are already playing so though it's one person just connected so we need to create a new game that's what this modulus two is getting okay so to do that we're gonna say games game ID equals game game ID okay and I believe this X J's to be capital so obviously I saw at the beginning of this I forgot to mention I import a game so from game import game that is important and yes actually what we're doing is we're just going to say that game ID which is that key in our dictionary is now equal to a new game so we can access that and add players to it and whatnot sweet so that works let's actually print out a message here and just say creating a new game dot dot just so that in our server we get some kind of output and we can have a look at that if something's going wrong so otherwise if there we don't need to create new game meaning we have let's say three people are connected so that second game already exists and another person connects well that person has to be a part of this new game so what we're gonna do then is we're inside game game ID dot ready equals true now what this means is that the second player connected so there's two players now connected to our game so now we can say that that game is ready to start playing because both the players are connected so that means that they can will obviously play against each other right so that's what we'll do we'll set that dot ready equal to true and obviously we're storing all the games on the server side as opposed to on the client side and then we're going to say is going to say P equals one and what this means is player equals one and you'll see why we need to do this in a second okay so now we're gonna do start new thread [Music]
Original Description
In this python online game tutorial I show how to create an online rock paper scissors game using python sockets, networking and pygame.
Source Code: https://techwithtim.net/tutorials/python-online-game-tutorial/online-rock-paper-scissors-p1/
⭐ Kite is a free AI-powered coding assistant for Python that will help you code smarter and faster. Integrates with Atom, PyCharm, VS Code, Sublime, Vim, and Spyder. I've been using Kite for 6 months and I love it! https://kite.com/download/?utm_medium=referral&utm_source=youtube&utm_campaign=techwithtim&utm_content=description-only
◾◾◾◾◾
💻 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
- Online Game Tutorial Python
- Python Tutorials
- Sockets Python
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: LLM Foundations
View skill →Related Reads
📰
📰
📰
📰
OpenAI makes ChatGPT Health available to all U.S. users
TechCrunch AI
I built an AI-first bank statement converter. The one rule that stopped it lying to me.
Dev.to AI
Building trade assistant: How Jefferies optimized front office trading operations with AI
AWS Machine Learning
Changes to LLM pricing: Novita and StreamLake
Dev.to · Narev Bot
🎓
Tutor Explanation
DeepCamp AI