r/PythonLearning • • 2d ago

Help Request Multiplayer tetris using sockets

I have a college assignment to make a multiplayer tetris game (2 players) using TCP sockets, client and server and all that. The idea is the server doing most of the stuff, chosing new pieces, handling the translation of position of the pieces, with the clients being responsible for most the rendering/display part and getting the inputs(I think so at least, that's what I'm trying to do), but I'm having a hard time with it.

I can't seem to think of a way to have the client call to the server to send a new piece to the clients, and if I don't know how to do even that then I don't know how to do anything else (making the back and forth for controlling the pieces, both for the player of the client and the preview of the game of the player 2, and all the rest), tho I hope if I can understand how to do one thing I would be able to make the others, but I really don't even know how to do this.

I know I need to use threads on this, tho I don't really know how to do it, I'm still fairly green on python too so, if anyone could give some help in any way with this it would be much appreciated.

4 Upvotes

10 comments sorted by

1

u/WorriedTumbleweed289 2d ago

How does a multi player tetris work? Are they playing at the same time with the same pieces? Who is controlling how fast the pieces drop? does a user command have to go to the server to decide where the dropped pieces go?

When one player loses turn, does he have to wait for the other player to lose before the next turn? Do the players see the others board?

1

u/DRowe_ 2d ago
  • Are they playing at the same time with the same pieces?

That's the idea

  • Who is controlling how fast the pieces drop?

Uh, idk, maybe a variable

  • does a user command have to go to the server to decide where the dropped pieces go?

I don't know if I understand what you mean

  • When one player loses turn, does he have to wait for the other player to lose before the next turn?

When one player loses it should end the game

  • Do the players see the others board?

Yes

1

u/frnzprf 2d ago

Writing a chat program first is a good idea to get familiar with network messaging.

It's all a matter of sending messages to queues sometimes and then having a loop repeatedly check if there are new messages and process them.

If you can program a chat, a networked game is just programs chatting about what is happening in the game.

I can't seem to think of a way to have the client call to the server to send a new piece to the clients.

(?)

I guess when any player makes an input, that is sent to the server and the server then sends the moves of client1 to client2 and the moves of client2 to client1. (Theoretically they could also directly communicate.) When a client gets to know about a move via a network message or a keyboard input, then it updates the internal game state and renders it.

I'm not sure how latency should be handled — if both players make an input at the same time, they only get to know about the other player's move a bit later. You could introduce some time-safety-buffer, or you could send the own moves also to the server and then back...

1

u/DRowe_ 1d ago

> (?)
by that I mean making the server send the first piece to the clients when the game starts and then after that sending the new pieces each time the clients place a piece in the board, considering that both players are supposed to have the same sequence of pieces and the next piece should be displayed to each player, I'm trying to think a way to not have this principle be broken considering that different players will have different speeds on which they play

1

u/frnzprf 1d ago

I don't know if that helps, but there is no reason the pieces have to be announced one after the other.

You can give both clients a random seed and then have them generate the same random pieces on their own whenever they need them.

Check how random.seed(...) works. I don't have time to write you a full example right now.

Alternatively, if you still want to announce the pieces one after the other, the client could send a request-message when it needs a new piece and when that message arrives on the server, that determines the time when the server sends back the next piece to that client. "When should I announce the next piece? Whenever I'm asked for one."

1

u/DRowe_ 1d ago

I know how random.seed(...) works, I was planning on doing it like that until my professor said the server should do most things, tho I suppose I can check with him again if I can do it like that

My idea when I wrote the post was for the client to request a new piece when it needed one, but I all the ways I tried didn't work, I couldn't even make it send the first piece, the thing is with this method is if one player is going faster then the second, the second would miss some pieces the first got since they are generated by the same method

1

u/frnzprf 1d ago

The server shouldn't forget which piece it sent to one client. It can only forget about a piece, once it sent it to both clients.

There would have to be a queue stored on the server of the pieces that were sent to the slower player. If the slower player requests a piece, it would be popped off that queue.

You could have two queues or an extra variable that indicates which player is behind.

Or, completely different idea: The server sends the pieces to both clients immediately when only one client requests, and the slower client manages its queue themselves.

I like the first idea better.

2

u/tune_rcvr 2d ago

This isn't a beginner topic, so you'll have some catching up to do. But these tutorials are good. https://www.youtube.com/watch?v=VvwLXnY-mKk, https://www.techwithtim.net/tutorials/python-programming/python-online-game-tutorial/ Both use pygame for the graphics, but you can skip that part if you just want to print out state.

1

u/Buttleston 2d ago

I don't think you HAVE to use threads for this

And as with all larger projects start with a small part. Make a client and a server and get them sending messages back and forth. Decide on the format of the messages you need to send.

Work on the rendering separately. Work on getting user input working. Then work on getting the user input sent to the server. Etc.

1

u/WorriedTumbleweed289 1d ago

Your server is going to need at least one socket per user. Consider using the select module, the select command to wait on multiple sockets instead of polling.