voorloopnul

About Me

My name is Ricardo Pascal, I'm a developer and sysadmin who lives in Florianopolis/Brazil. I enjoy work with projects related to Linux, Python, Science, Web, Django and Network.

g+ For the first time since I became a Ubuntu user, I'm considering not update my system to a new release... | |

How (not) to create a network game

One of the most important thing that you should keep in mind while developing a real time network game is that latency is a bitch.

Considerations

  • For the sake of simplicity the code will be focused on latency without any attempt to fix that.
  • Our sample code is constructed based on a previous post about proxy, since the server code is 99% the same, a explanation about how the server works can be retrieved from that post.

Overall architecture

The architecture of our game is really really simple, in a raw way it will emulate one of the most common conception of how a network communication in a game can occur:

1) there is a server; 2) each client connect to this server and send his current position; 3) as response, the server send back a dictionary that holds the position of all objects; 4) with this data the client draw all the objects at their respective positions.

spaceship.png

spaceship.png

spaceship-server.py

spaceship.py

Client explanation

function forward()

returns a communication channel with the server, using this socket the client will send the spaceship position, and retrieve the global position map.

function create_ship()

returns a spaceship object (a pyglet sprite); set the initial position, if that is not your ship define a _id and put a little of opacity;

class MainWindow() init()

Start the pyglet screen; bind the keyboard as input; create your ship; start the communication channel

class MainWindow() on_draw()

(1) Clear screen and draw your spaceship. (2) Stores your spaceship position/orientation, them send this data encoded as json to the server. (3) Receive and decode the global position data. (4) Iterate over the returned data to create a list of ships id and ships objects. (5) Iterate over the list of objects; define their new position and draw them all. (6) Check for keyboard input and move your ship.

What happens when you run the code?

If you use 3 similar computers on a local network, one as server and two as clients, you will have a close to smooth experience. But if you run this over Internet things will go wrong. Do you realize why?

The bitch latency! Over a LAN you will have less than 1ms of latency, in one second close to 500 interchanges between the server and client can occur. (Since we need a response from server, each action will take twice the latency time: 1000ms / (1ms + 1ms) ) But between my home broadband and my vps only 4 happens.

Let's imagine a scenario where 3 people from different parts of the world connect to my game server running at this VPS(in Germany). How the spaceship speed will be seen from the watcher perspective?

  • People A Joe from Haarlem (NL) with 100ms of latency
  • People B Mika from Hamburg (DE) with 50ms of latency
  • People C Me from Florian√≥polis (BR) with 250ms of latency

That's what will be seen:

Spaceship speed

Each spaceship will have a different speed, and each client will see a different pattern of lag which will warp all the others spaceships. At this point you probably realize that writing a network game is not so simple.

That's all folks!


by Ricardo Pascal on Sep 10, 2012

comments powered by Disqus