Basic networking with Pygame

2019-03-08 20:50发布

问题:

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 2 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? Can anyone point me in the right direction?

回答1:

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.


回答2:

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.



回答3:

Since you are already using Pygame I think this light networking lib made for Pygame will do what you need and teach you but not overwhelm you. "Mastermind Networking Lib" via pygame.org



回答4:

There is pyro (python remote object) as another solution for networking in python.

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



回答5:

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 https://github.com/dotcloud/zerorpc-python

You need following solutions:

  • discovering other player(s) on (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 IO

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

UPDATE:

Essentially you need to expose network service (in it's own thread) that will push messages to Python's Queue, 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 sent 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 source position and velocity vector.