Alex Rivera | Logout

Basic networking with Pygame

Asked 2012-04-01T03:51:51.103
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?

Edit
Report

1 Answer

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.

answered 2012-04-01T23:09:01.993

Your Answer