Integrating Sockets with Battle Tank

Now that you have all this cool code that deals with sockets, you can integrate it into the Battle Tank game you created in the previous chapter. To do this is very simple. Obviously, the first thing you will want to do is connect to the socket every time you click the Fire button or start a new game. If you click the Fire button you will want to send your data to the server.

Once your data has reached the server you will want to query the server for the next coordinates of the bullet, and then render the game with the new coordinates. Here is the new Render() function for Battle Tank:

function Render()
{
    // Create the socket and connect
    $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
    $connection = socket_connect($socket,’localhost’, 1337);

    global $gGameState;
    global $gDifficulty;
    global $gLeftTankLocation;
    global $gRightTankLocation;

    // Get locations
    $gLeftTankLocation = $_SESSION[‘gLeftTankLocation’];
    $gRightTankLocation = $_SESSION[‘gRightTankLocation’];

    switch($gGameState)
    {
        case GAME_MENU:
        {
            // Display the menu
            RenderMenu();
            break;
        }
        case GAME_INIT:
        {
            // Init the Game
            GameInit();

            // Update Screen
            Render();

            break;
        }
        case GAME_PLAY:
        {
            //printf(session_id());

            // Get the input and calculate the hit point
            if($_POST[‘btnFire’] != “”)
            {
                $explosionCoords = CalculateFire($_POST[‘angle’], $_POST[‘velocity’]);

                if(!socket_write($socket, $explosionCoord[0] . “,“ .
$explosionCoords[1] . ”
”))
                {
                    echo(“<p>Write failed</p>”);
                }

                // Check to see if there was a hit
                if($explosionCoords[“x”] >= $gRightTankLocation[“x”] &&
$explosionCoords[“x”] <= $gRightTankLocation[“x”] + 47 && $explosionCoords[“x”] >=
$gRightTankLocation[“y”] && $explosionCoords[“y”] <= $gRightTankLocation[“x”] + 24)
                {
                    // Hit the other tank
                    $gGameState = GAME_WIN;
                    Render();
                    return;
                }

                RenderExplosion($explosionCoords);
            }

            while($buffer = socket_read($socket, 1024, PHP_NORMAL_READ))
            {
                if($buffer == “NO DATA”)
                {
                    echo(“<p>NO DATA</p>”);
                    break;
                }
                else
                {
                    // Do something with the data in the buffer
                    $explosionCoords = split($buffer, “,”);
                }
            }
            // Make the other players shot
            if($explosionCoords[0] >= $gLeftTankLocation[“x”] && $explosionCoords[0]
<= $gRightTankLocation[“x”] + 47 && $explosionCoords[1] >= $gLeftTankLocation[“y”] &&
$explosionCoords[1] <= $gLeftTankLocation[“x”] + 24)
            {
                // Hit the other tank
                $gGameState = GAME_OVER;
                Render();
                return;
            }
            RenderExplosion($explosionCoords);

            // Render the terrain
            RenderTerrain();

            // Render the tanks
            RenderTanks();

            // Render the interface
            RenderInterface();

            break;
        }
        case GAME_WIN:
        {
            // Let the player know he won
            printf(“WIN!!!”);
            break;
        }
        case GAME_OVER:
        {
            // Let the player know the game is over
            printf(“GAME OVER”);
            break;
        }
    }

    // Update the game state
    $_SESSION[‘gGameState’] = $gGameState;
}

That’s it! As I said before, this is not the best way to do this, but it works for demonstrating PHP’s socket capabilities. There are some cool things you could do by creating sock-ets that talk to Flash pieces through your own PHP server, which is a more common way to use sockets.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset