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:
And these rules will be implemented in the following section.
Let's start now.
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;
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(); }
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; }
bool createNew = randomValue(0.0f, 1.0f) < 0.01f * (1.0f + (float)score / 100.0f); if ( createNew ) { .. }
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 );
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() );
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 ); }
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.
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!