[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [pygame] Network



On Tue, Dec 11, 2001 at 12:12:57AM -0500, Nat Budin wrote:
> On Monday 10 December 2001 07:21 pm, you wrote:
> > I'm going to make it simple, how to do a networked game using python and
> > pygame? Does I have to use sockets? Isn't there any easier(like pygame
> > is to SDL) way to do it?
> 
> Well, I'm no networks expert, but one idea does occur to me.  There is 
> probably no easier way to make the network connections themselves than using 
> sockets, but there is most likely a much easier way to deal with the data 
> sent between clients.  Namely, you could use some of Python's built-in 
> encoding and decoding facilities like Pickle or maybe some XML stuff to 
> simplify encoding and parsing messages from client to server and vice versa.

Coming from a C background I may be able to help.

Definately learn sockets. Its the way its done, its easy (especially with python), its just like files and youll be a better programmer for learning it.

Use TCP or UDP. UDP is probably better if you can fit the biggest comm. packet into 512 bytes. TCP would be easier, albeit slightly heavier on the bandwidth.

Develop a 'protocol' for the game packets. Put a version number in the protocol for backward compatability. Lay it out as bytes. eg.

+----+----+----+----+----
|ver |type|  length | begin...
+----+----+---------+----

so this could be your packet header. Ver=protocol version (1 byte), type=the message type (1 byte), length=length of the following message (2 bytes) then the message.

Make lots of different message types. That get inserted where begin is. 

Perhaps put a timestamp with the packets. What about clock skew between hosts?


This is the basic idea. You develop a protocol spec that the game adhears to, that it uses to communicate the game state between hosts. Use the struct.pack() method to pack the data into a compressed sequence and then send it. Use struct.unpack to expand. Read the man for info on struct.

This would be much more efficient than Pickle or XML, because your protocol only sends what is neccessary in the most compact form possible.

Oh. And document the protocol in the game source tree (Dont want to forget it :)


For the actual game, their are a number of ways to approach the problem

The general problem is keeping two instances (or more) of the game in syncronisation with each other. Its not an easy task, as their are many factors that may change (errors, bandwidth, crashes?). There are lot of techniques to keep games in synchronisation, each with pros and cons. Heres some basic ones.

Input Device Reflection
-----------------------
Treat the network connection as a really long, lagging joystick and keyboard cable! Send the input to the other computers. Consider their data as the other players input. What if one machine is faster than the other? PC 1 might be on frame 500 while PC 2 is on 550!

Universe Reflection
-------------------
Remove complete randomness from the game. Any randomness must be communicated in the protocol. The more deterministic the events are, the less data that needs to be sent, as each machine can deduce whats happening on the other. When things 'begin' in the universe on one computer, a signal is send to the other where this 'begining' is mirrored. If the behavior from the t=0 point is deterministic, the conputers can go on from their.
Regular syncronisation packets can help. Where things are checked to be correct. (This is what creates that jumping in quake2).

Token Locking
-------------
Slowing the fast running games to the slow running ones through tokens that are send (Or broadcast on a LAN with UDP to the broadcast address. Actually on a LAN, broadcast packets can help all of these syncronisation techniques. Doesn't work with TCP however)

Master Slave
------------
One game is the master and handles everything. The slave mimics the information the master is teling it. This is the best approach for more than two gamers at once. A dedicated server can act as the master if need be, and the clients connect. The master calculates all the game logic and communicates it to the slaves. 

Im sure there are other ways aswell
 


Good luck!

Crispin

____________________________________
pygame mailing list
pygame-users@seul.org
http://pygame.seul.org