22

I need to do some basic networking for a Pygame project.

Basically, it's a 2D single player or cooperative game. The networking only needs to support two players, with one as a host.

The only information that needs to be sent is the positions of players, creeps and bullets.

I've been reading around and Twisted keeps coming up, but I haven't done networking before, and I'm not sure if that might be an overkill.

So, is it possible for a relative newbie to implement networking in Pygame?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Lightbreeze
  • 4,624
  • 3
  • 15
  • 10

5 Answers5

16

This was asked recently on Reddit, so I'll more or less just copy my answer over from there. I apologize for not being able to provide more links, I have <10 rep so I can only post two at a time.

Twisted might work, but I don't have a whole lot of experience with it. I'd recommend going with sockets, as that's what Twisted uses in the background anyway. Beej's guide (google it) is pretty much the Holy Bible of sockets if you want to learn how they work (in C++, but the concepts extend everywhere). Python does abstract some of the complexity away, but it's still a good idea to know what's going on in the background.

For Python specific sockets, you can go ahead and just use the howto (user745294 posted a link above). Here's a nice article titled "What every programmer needs to know about Game Networking". It goes into the different types of major networking styles (client-server, p2p, udp v. tcp, etc.) and the history behind what some major games used for their networking.

Below is a link to a demo I did on making a networked "game" in Python 2.6/Pygame. It's not actually a game, but each client you create connects to the server and controls a character. You can move your character with the arrow keys and the character will move on all connected clients. I tried commenting the source code with some indication of what I'm sending back and forth, but you may need a little knowledge about sockets to understand it.

The source code is provided in the codepad links in the comment below this post. You will need to provide two images in the same directory as the scripts:

  1. bg.png is the background sprite. It should be an image 400px wide and 300px tall (this can be changed in the GameClient class if needed)
  2. sprite.png is the player character. It should be smaller than the background so that you can see it moving around.
nemec
  • 1,044
  • 9
  • 35
  • Here are a couple of codepad links to the Client/Server in case you don't want to visit mediafire (you'll have to provide your own bg.png and sprite.png). Server: http://codepad.org/p9lVrmqn Client: http://codepad.org/e6pwGj24 – nemec Apr 01 '12 at 19:30
  • 1
    The mediafire link is dead anyway, it would seem. The codepad links are great though, thanks! – John T Mar 08 '14 at 10:23
  • Hi I know this is old, but the mediafire link is dead. If you still happen to have the source code Im really interested! – Jackpy Nov 08 '18 at 21:46
  • Source code is provided in the codepad links in the comment above. The only additional content that was included in the mediafire are two images: bg.png (the background) and sprite.png (the player character). Provide two images of your own (I no longer have them) and the code will work as expected. – nemec Nov 10 '18 at 20:32
  • @nemec When I tried to copy the code from codepad links and create some images, I received several python1/3 conflict errors and when I fixed those, I received another error: `ConnectionResetError: [WinError 10054] An existing connection was forcibly closed by the remote host`. Am I doing anything wrong here? – Patlatus Apr 01 '23 at 11:46
  • Assuming you run the server first, port 9009 isn't taken, and both server and client are run from the same server, I'm not sure what's wrong. – nemec Apr 09 '23 at 10:01
7

You can use Twisted for networking with Pygame. The "game" project on Launchpad has some examples of how one might integrate the main loops together; basically, use twisted.internet.task.LoopingCall to draw Pygame frames and handle input, while letting the Twisted reactor of your choice run normally.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Glyph
  • 31,152
  • 11
  • 87
  • 129
2

Since you are already using Pygame, I think this light networking library made for Pygame will do what you need and teach you, but not overwhelm you.

"Mastermind Networking Lib" via pygame.org

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • 1
    Mastermind uses [pickle](https://docs.python.org/3/library/pickle.html) which is not secure. *" Warning: The pickle module is not secure against erroneous or maliciously constructed data. Never unpickle data received from an untrusted or unauthenticated source."* – skrx Oct 13 '18 at 16:07
  • The author changed the lib so that it uses json by default now which is safe. – skrx Oct 27 '18 at 09:38
0

There is Pyro (Python remote objects) as another solution for networking in Python.

http://irmen.home.xs4all.nl/pyro/

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Tshirtman
  • 5,859
  • 1
  • 19
  • 26
  • 1
    Do not use Pyro for any untrusted communications. It definitely is not suitable for a game played over the internet. http://packages.python.org/Pyro4/security.html#pickle-as-serialization-format – Glyph Apr 02 '12 at 21:34
  • I admit that's not the best use case for it, thanks for pointing out. – Tshirtman Apr 03 '12 at 18:36
  • The link is broken: *"404. Not Found. The requested URL was not found on this server."* – Peter Mortensen Dec 05 '22 at 00:08
0

Using raw sockets is low-level and full of danger. As said before, Twisted is complex and takes to time get up and running. To save yourself some headaches I'd try something like zerorpc.

You need the following solutions:

  • discovering other player(s) on the (local) network, you don't want player to enter some IP address
  • handle network errors
  • serialize messages containing your data (positions, player name, etc.)
  • handle threading as networking is asynchronous I/O

Above should still be called 'basic', you should really use some fancy networking library with idiomatic API.

Essentially you need to expose the network service (in its own thread) that will push messages to Python's Queue, and then access this same queue from your Pygame code, and if there is a message then you update whatever structures you use to store player's position and draw it on screen.

You shouldn't send stuff like bullet positions over the network as they can be easily (and faster) calculated locally. You just send an event like bullet_shot over the network with a source position and velocity vector.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
skrat
  • 5,518
  • 3
  • 32
  • 48
  • 2
    Twisted isn't all that complex. An echo server is 3 lines of code. A 2-way RPC server isn't much more than that. Twisted's a general networking solution though, suitable for a game client - zerorpc relies on zeromq and is therefore not suitable for untrusted-client communications; it's more for coordinating back-end services with each other. – Glyph Apr 02 '12 at 20:23