Game Development Reference
In-Depth Information
Putting it all into practice
We are going to build a very simple client-to-client simulation of two players who both
control a character that can move in four directions: up, down, left, and right. This is not a
fluid simulation, but the player's movement is limited by one tile per frame. In this case, we
can use TCP to achieve a decent networking experience.
We start with the world. It has to be split into tiles and each tile has to have a certain size
(defined in pixels). Here is our #include and #define list:
The graphics module is used for the RenderWindow and RectangleShape represent-
ations of the players and the networking module is for the TCP sockets. We use the console
as a way of supplying the destination IP address. This can be done in many other ways, but
the console seems appropriate for this example. Finally, we specify that a board tile is ex-
actly 40 pixels.
Moving on to the first part of the program (the connection), we let the players type host to
start waiting for a connection (acting as the listener) or type in an IP address to connect to
a waiting host. This is the code that produces the behavior:
Search WWH ::




Custom Search