Training
Turn-based games
This article will teach you how to create a two-player, turn-based multiplayer game. And if you think that these types of games aren't fun, you should definitely check out this extensive list of turn-based game hits. In our project, one of the computers will run as a server and client at the same time, while the second computer will only run as a client.
The code will be a bit more complex, because we are actually building a game this time. Don't worry, though; a huge chunk of the code includes function prototypes, various panel definitions, and so on – see for yourself.
In this game, players throw a dice, trying to reach the destination first. First, let's take a look at function main().
As you can see, this function doesn't do anything extraordinary; it is very well commented, so let's move on. The next image shows the server's and client's event functions.
The server-side function is triggered as soon as a client joins the game. Then, it generates a random number (can be 1 or 2) to determine which computer (the server or the client) should throw the dice first. The same function runs when a client leaves the games or gets disconnected from it. The final "if" branch determines if the server event was triggered by a current_player variable change, which was sent by one of the clients over the network. If this happens, the dice panel will be displayed on the server; otherwise, it will be hidden. The last few lines of code move the player panels on the screen, towards the right side, by increasing their pos_x coordinates.
The client even function is even easier to understand; it will monitor the server, alerting the player if the connection with it is interrupted, and then (if this happens) it stops the game. The rest of the code is similar with the one that was used for the server: it hides or displays the dice panel on the client's monitor, and it moves the client towards the right side of the screen.
The last code section is used to move the mouse pointer around, and to run the dice_clicked() function. Since this piece of code is a bit more complex and is run by both the server and the clients, let's take a good look at it.
The first few lines of code don't allow the player to cheat by clicking the dice panel several times in a row, with the goal of getting a higher score. Those lines ensure that only the first mouse click on the dice panel will be taken into account. The next lines generate a random integer that has a value which can range from 1 to 6, and then display the appropriate dice face.
If the game is run on the server, the if(im_server) branch will be executed. The code moves the player towards the right side of the screen, and then checks its final position, with the goal of determining if the server has won the game or not. If the answer is affirmative, a message will be displayed and a sound will be played on both the server and the client. The if(im_client) branch is very similar, so we won't discuss it here. The last few lines of code send the needed variables over the network.
As you can see, we have managed to build a turn-based multiplayer game that actually works without using too much code. The next tutorial will show the code for a real-time multiplayer game example.
Need help with code for your MORPG? I can help!