Playing the game

The last example in this chapter is to design a simple gameplay. We now have the interaction driven by Kinect, and fruit objects that intersect with handtrails, but we don't have any rules yet. The gameplay will specify how to win or lose the game and how to set challenges for the player to overcome. A good gameplay is the base of the playability of a game.

We have four rules for the Fruit Ninja game here to follow:

  • Our goal is to cut as many fruits as possible to earn higher scores.
  • The higher the points we earn, the faster and more in number the fruits appear.
  • The player has a life value (initially 100). The game finishes when it becomes zero.
  • If a fruit is not sliced, the life will be decreased by 5 points. If a bomb is sliced, the life will be decreased by 20 points.

And these rules will be implemented in the following section.

Adding simple game logic

Let's start now.

  1. We will declare the score and life variables, which will act as the very basic elements of the gameplay. And we add a gameOverTexID variable for displaying a different image when the game is over (when the life is down to 0).
    const unsigned int gameOverTexID = 6;
    int score = 0, life = 100;
  2. In guessGesture(), we will reset the score and life variables if the player holds his hand on the restart button (top-left), and quit the program when holding on the close button (top-right).
    if ( holdGestureCount[index]>30 )
    {
        if ( currentY>0.9f && currentX<0.1f )  // Restart
        { score = 0; life = 100; }
        else if ( currentY>0.9f && currentX>0.9f )  // Exit
            glutLeaveMainLoop();
    }
  3. In the update() function, we have to check the objectID object of every fruit object intersected with the hands. If it is a bomb (ID = 3), decrease the life; otherwise increase the score.
    if ( isSliced )
    {
        ...
        if ( fruit.objectID<3 ) score += 10;
        else life -= 20;
    }
    else if ( fruit.position.y<0.0f )
    {
        // If the fruit is sliceable but not sliced
        // we also decrease the life as a punishment
        if ( fruit.canSlice ) life -= 5;
        itr = _fruitObjects.erase( itr );
    }
    else
    {
        fruit.update();
        ++itr;
    }
  4. To make the game more difficult, when the player earns a high enough score, we can increase the chance of generating new fruits in every frame gradually. This is done by simply altering the following code:
    bool createNew = randomValue(0.0f, 1.0f) < 0.01f * (1.0f + (float)score / 100.0f);
    if ( createNew )
    {
        ..
    }
  5. In the render() function, we check if the life equals to 0 and display the "game over" texture instead of the normal one.
    if ( life<=0 )
        TextureManager::Inst()->BindTexture( gameOverTexID );
    else
        TextureManager::Inst()->BindTexture( backgroundTexID );
    drawSimpleMesh( WITH_POSITION|WITH_TEXCOORD, 4, meshData, GL_QUADS );
  6. We can also write the current score and life values on the screen in the render() function.
    std::stringstream ss;
    ss << "Score: " << score << "  Life: " << (life<0? 0: life);
    
    glRasterPos2f( 0.01f, 0.01f );
    glColor4f( 1.0f, 1.0f, 1.0f, 1.0f );
    glutBitmapString( GLUT_BITMAP_TIMES_ROMAN_24, (const unsigned char*)ss.str().c_str() );
  7. Last but not least, read the "game over" image from the disk. You may design this image by adding some texts and symbols on the normal background.
    if ( TextureManager::Inst()->LoadTexture("FruitNinja6.jpg", gameOverTexID, GL_BGR_EXT) )
    {
        glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
        glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
    }
  8. Now, compile and run our game. You will see the score and life values changing when you struggle to cut the fruits thrown suddenly.
    Adding simple game logic

    Playing the Kinect-based Fruit Ninja game

  9. When the game is over, you will see a different image on the screen, and you can hold on to the restart button at the top-left corner to restart the game, or exit by holding on to the exit button at the top-right corner.
    Adding simple game logic

    The game is over if the value of the player's life reaches zero

Understanding the code

A perfect gameplay system will be more complex than the current one. We can have several levels for the player to reach when his/her score is high enough. One could also get some new abilities when playing for a long time or reach a specific level, for example, clearing all the fruits on the screen with a special gesture, or destroy the bombs without getting hurt, in a specific timespan.

However, since the game system design is not the key point of this book, we will only use the two simplest concepts of a game here, that is, score and life. They are enough for interacting with the existing elements in the scene. If slicing a fruit is detected, the score increases. The life decreases if you slice a bomb. If the life is zero, the game is over and you can only restart it. The score and life values will be reset.

Additional information

Now it depends on you to improve this game as much as possible. You can change the images it currently uses. You can also add new game concepts such as levels and special props. As well as this, you can add audio and networking supports as you wish. After all, it's a Kinect-based game, which we have developed from zero!

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

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