Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length

 
Advanced search

1403811 Posts in 68294 Topics- by 61958 Members - Latest Member: Kvrochi

December 09, 2022, 06:46:57 PM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsCommunityTownhallForum IssuesArchived subforums (read only)TutorialsRetrofitting Tetris (or similar) into Multiplayer-Tetris (Java)
Pages: [1]
Print
Author Topic: Retrofitting Tetris (or similar) into Multiplayer-Tetris (Java)  (Read 1801 times)
SteveSmith
Level 0
*


View Profile
« on: November 08, 2016, 07:21:48 AM »

Turning Tetris into Multiplayer-Tetris

This post will describe how to take an existing codebase of the classic game Tetris and make it multiplayer using the Generic Multiplayer Connector library ( https://bitbucket.org/SteveSmith16384/genericmultiplayerconnector ).  Although I use that specific game for this tutorial, it should serve as an example of how to do it for any simple game.

The first thing to do is add the GMCClient.jar library to your code and add it to your project using your favourite IDE.  This can be downloaded from the respository, be selecting it and choosing View Raw.

Next, add a reference to ConnectorMain in the main class of your game:-


    public ConnectorMain connector;


Next, create an instance of it at the start of your game.  The constructor takes all the parameters it needs to know which server to connect to, the players name etc...  The easiest way to get these from the player is to use the built-in StartGameOptions.ShowOptionsAndConnect() method.  This brings up a form for the user to enter the details, and then connects to the server.  It returns an instance of ConnectorMain.


    connector = StartGameOptions.ShowSimpleOptionsAndConnect(this, "Multiplayer Tetris ", 2, 99);


We'll choose a minimum of 2 players and a maximum of 99, because we're that popular.  You should find that you'll get compile errors after adding the above line, since the this needs to be a class that implements the IGameClient interface.  So make your class implement this interface as so:-


    public class MultiplayerTetris extends JFrame implements KeyListener, IGameClient {


...and then get your favourite IDE to add all the relevant implementations.  They don't need to do anything yet.

Once you've connected to the server, you can join the game.  It's recommended do this immediately, since that's why the player started playing this thing.


    connector.joinGame();


Once you've done this, it's just a case of waiting for the minimum number of players to join before the game can actually start.  You can either have some kind of game loop that checks for connector.getGameStage()  == GameStage.IN_PROGRESS, or call the following which will simply halt your code until the game starts:-


    connector.waitForStage(GameStage.IN_PROGRESS);


Once the game stage has changed to GameStage.IN_PROGRESS, you can start the game proper and drop some shapes.

In our Multiplayer Tetris, we'll design it so that when a player completes a row, it speeds up all the other player's games.  So, at the place where your game identifies that a player has completed a row, add the following code:-


    game.connector.sendKeyValueDataByTCP(1, 1);


This function will send the key/value "1/1" to all the other clients when this player completes a row.  The first "1" is our code for completing a row, and the second "1" is the number of rows completed.  If I was a better programmer I'd make the first "1" a constant or something to aid readability.  You can design your own protocol as you see fit.

Next, find the implementation of the "void dataReceivedByTCP(int fromplayerid, int code, int value)" function in your code as required by the IGameClient interface, and change it to something like the following:-


  @Override
  public void dataReceivedByTCP(int fromplayerid, int code, int value) {
       if (code == 1) { // 1 is the code for completing a row
              this.game_speed += 0.1f * value;
        }
 }
 

This function will be called whenever the other clients receive the code we sent earlier, but sent by another player.  This will then increase the value of game_speed, which will speed up the game (in my Tetris codebase anyway - yours will probably be different).

Next we need to add some code to check for a winner.  The easiest way to do this is override the IGameClient.gameEnded() interface implementation:-


@Override
public void gameEnded(String winner) {
       if (connector.areWeTheWinner()) {
          JOptionPane.showMessageDialog(this, "You won!");
       } else {
           JOptionPane.showMessageDialog(this, "The winner was " + connector.getWinnersName());
       }
}


If this method is called, it means the game has finished, so you should stop your game here and show a message to the player.  I've just used JOptionPane to do this to keep things simple.

The only thing to do now is remember to call connector.joinGame() again if the players wants to play another game.

And that's it!  Now run your game.  It should first come up with the options screen asking for the server and game details.  Once these are accepted, and at least 2 players have joined the server, the game should start and the battle between players can commence!

If you have any problems with this, there is a Multiplayer Tetris game included with the GMC library for you to check out.
Logged
Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic