Author Topic: Making multiplayer games using pygame  (Read 5953 times)

Quacksol (OP)

  • *
  • Posts: 6
Making multiplayer games using pygame
« on: August 11, 2014, 02:02:12 pm »
Hello everyone, I have some questions to ask regarding whether or not it's possible to make multiplayer games using pygame for the GCW, using wireless connections like that delicious chocolate doom. I'm quite new to python and even programming, but I'm certainly eager to see what I can do, I've got a few things working on the gadget already.
To keep things simple, lets say I wanted to make a 2-player Pong where the ball goes between the screens. I'd like to ask firstly if this is possible with pygame, and if so how the whole thing works. I had a quick look on external sites but didn't find anything for development of multiplayer games, but a few videos showing multiplayer is possible. Thanks in advance.

pcercuei

  • ***
  • Posts: 1429
    • My devblog
Re: Making multiplayer games using pygame
« Reply #1 on: August 11, 2014, 02:26:00 pm »
Network communications are done using sockets: https://docs.python.org/2/howto/sockets.html
That's the same in every other language. Basically, you create a socket, bind it to an IP/port, and then it's like a tube. You write data on one side, read it from the other side.

Ziz

  • *
  • Posts: 284
    • http://ziz.gp2x.de
Re: Making multiplayer games using pygame
« Reply #2 on: August 12, 2014, 09:21:25 am »
and then it's like a tube. You write data on one side, read it from the other side.
In my imagination it is no tube, it is... an open Star Gate! :D
I am a leaf on the wind - watch how I soar. Wash

Quacksol (OP)

  • *
  • Posts: 6
Re: Making multiplayer games using pygame
« Reply #3 on: August 12, 2014, 01:14:27 pm »
Network communications are done using sockets: https://docs.python.org/2/howto/sockets.html
That's the same in every other language. Basically, you create a socket, bind it to an IP/port, and then it's like a tube. You write data on one side, read it from the other side.

Thanks for this, although it raises more questions. Firstly, do I need to use that GCW connect, or could it make things easier? And secondly, after a bit of reading up on sockets, I found a video explaining how python can send data over wifi using UDP something; am I looking in the wrong place? Lastly, if I'm not, I had a problem converting strings into binary to send and then converting that back into ascii, how the heck do I do this? Thanks again.

Ziz

  • *
  • Posts: 284
    • http://ziz.gp2x.de
Re: Making multiplayer games using pygame
« Reply #4 on: August 12, 2014, 02:04:48 pm »
Maybe have a look at the OSI Model: http://en.wikipedia.org/wiki/OSI_model

Short: You have seven layers of abstraction. The highest layer is your application and with going deeper you go nearer to the hardware and physical background of the data transmission. Layer one is the wifi itself. Gladly you don't have to interact with the Wifi hardware, but you need GCW Connect to make a connection with a Wifi Network first. Of course your application could do this on it's one, too, but why would you like reinvent the wheel?

Now have a look at layer three of the OSI model. Sockets are based on the IP protocol. Every device in the network needs an IP. GCW Connect made already, that you got one, lucky you. ;)

Now you need to choose, which protocol you want in layer four, the transport layer. You already mentioned UDP. UDP is like sending a letter. You give it to the post and that's it. You will never know, whether this letter will arrive. Maybe the post car explodes or your destination address doesn't exist (anymore). You don't care. You send your message, put your fingers in your ears and sing. ;)

TCP is like a letter with receipt. You will know, if your message transfer fail. However this takes longer, because after sending your message you wait a couple of times until the recipient (hopefully) answers.

Furthermore UDP is "connectionless", TCP works with connections. With TCP you open a connection and afterwards you can send and receive message and (the important part): The router of yours sees "Ah, a computer of the internal network opened the connection, so it is fine to send the answers of the other part oft the connection to him". With UDP you may get trouble, because there is no connection, from which the routers knows, that a computer from outside has the right to speak to you. Of course the router may have noticed, that you spoke to the server recently, but you cannot trust this unfortunately.

It also depends, what you want to make and if it is bad if message may not be sent. This can be the case for voice over IP. It doesn't matter if some packages didn't arrive. And even if they arrive after a couple of time, they are not useful anymore.

So, how to start?
  • You need a working Wifi connection, established e.g. with GCW Connect
  • Decide, whether you want to use UDP or TCP. If in doubt, take TCP.
  • Have a look at TCP or UDP sockets in Python and send data!

I hope this helped a bit. Networking can be a bitch. Don't give up!
I am a leaf on the wind - watch how I soar. Wash

Quacksol (OP)

  • *
  • Posts: 6
Re: Making multiplayer games using pygame
« Reply #5 on: August 12, 2014, 04:09:06 pm »
Thanks, I think I'm getting the hang of the whole sockets biz now. I've got 2 programs running on the pc that communicate with eachother, using some IP address and not localhost. Is it possible to have 2 GCWs connect without WiFi though?

Ziz

  • *
  • Posts: 284
    • http://ziz.gp2x.de
Re: Making multiplayer games using pygame
« Reply #6 on: August 12, 2014, 04:34:52 pm »
Somehow they need to be connected. I don't know, whether the GCW is capable of ad hoc wifi (Wifi without basis station like a router), but if you don't want to use Wifi at all you would need something else.

One possibility would be USB network, but I have no idea, whether the USB driver is good enough atm for this and whether the needed kernel modules are available. But in theory that should be doable. However I would suggest to concentrate on Wifi for now. Or to not concentrate on the kind of Network anyway. For your application it should not matter, whether you use network over wifi, usb or ethernet. :)
I am a leaf on the wind - watch how I soar. Wash

Quacksol (OP)

  • *
  • Posts: 6
Re: Making multiplayer games using pygame
« Reply #7 on: August 13, 2014, 03:16:30 pm »
Right, things are looking quite good now, but I have ONE LAST question. At the moment I have the same program that on startup lets you choose whether you're the client or server, and sets sockets up accordingly. When I press a button from the client, it sends a five to the server, prints it out, all fine. However, trying to press the button from the server tells : "[WinError 10057] A request to send or receive data was disallowed because the socket is not connected and (when sending on a datagram socket using a sendto call) no address was supplied" After a bit of searching I found quite a few 'echo' programs, which talked a lot about servers only binding and listening, and responding only immediately after receiving something. Is this the case, or have I just messed up?

pcercuei

  • ***
  • Posts: 1429
    • My devblog
Re: Making multiplayer games using pygame
« Reply #8 on: August 13, 2014, 03:46:25 pm »
At some point in your server's code, you use the "accept" method:
(clientsocket, address) = serversocket.accept()

This gives you a "clientsocket" which can be used to send and receive data. Are you maybe using "serversocket" instead to answer to the client? You have to use "clientsocket" for both sending and receiving.

Quacksol (OP)

  • *
  • Posts: 6
Re: Making multiplayer games using pygame
« Reply #9 on: August 13, 2014, 06:06:32 pm »
Thanks again, that did the trick. However, I've noticed some strange behaviour. The server sends data fine, and the client receives it all ok, but when the client sends data it freezes until the server sends some more data, and when the server sends a few more pulses only then does it receive the data sent a few seconds ago. I'm guessing this is a problem with my coding, are there any special techniques I should be aware of?

Quacksol (OP)

  • *
  • Posts: 6
Re: Making multiplayer games using pygame
« Reply #10 on: August 16, 2014, 12:41:40 pm »
I don't know if anyone is still willing to answer my many questions any more, but if you're feeling like it I have yet another. Can I send multiple things through a socket in a single frame? My server doesn't want to receive 2 things, it just gives up.

Ziz

  • *
  • Posts: 284
    • http://ziz.gp2x.de
Re: Making multiplayer games using pygame
« Reply #11 on: August 16, 2014, 01:56:57 pm »
Sure, you can send as much as you want.
I am a leaf on the wind - watch how I soar. Wash