Ubuntu Tutorials  Windows Tutorials  Download  |
Input Handling |
|
Written by: Andy Odle
      This tutorial builds upon the Add a 2D Sprite tutorial. Please complete the 1.) Download Padle.zip from here. 2.) Extract Padle.png into the "./Debug/images/" folder. 3.) Include DeviceInput.h in main.cpp (See Code Example 1).
Example Code 1:// Add the keyboard and mouse virtual key codes. #include "./source/PlatformSpecific/inc/DeviceInput.h" 4.) Change ball_m's starting visibitly to false. 5.) Create a new Sprite called playerOne_m.
6.) Set playerOne_m's postion, width and height to: 7.) Assign the Padle.png texture to playerOne_m. 8.) Set playerOne_m's visibilty to true. 9.) Create a new Sprite called playerTwo_m.
10.) Set playerTwo_m's postion, width and height to: 11.) Assign the Padle.png texture to playerTwo_m. 12.) Set playerTwo_m's visibilty to true.        Add the following code to the inside of GOC::Update() function.
A.) Set playerTwo_m's y-cordinate to the mouse's y-cordinate
Example Code 2:/* Update your game scenes, check for collisions, check for input and play sounds here. */ // Move player two up and down with the mouse. playerTwo_m->SetYCor(input_p->YCordinate());
B.) Decrement playerOne_m's y-cordinate by 20, when up is pressed on the
C.) Increment playerOne_m's y-cordinate by 20, when down is pressed on the
Example Code 3:// Move player one up 20 units when up is pressed. if(input_p->IsKeyOrButtonPressed(KeysAndButtons::UP_KEYBOARD)) { playerOne_m->SetYCor(playerOne_m->GetYcor() - 20); } // Move player one down 20 units when down is pressed. if(input_p->IsKeyOrButtonPressed(KeysAndButtons::DOWN_KEYBOARD)) { playerOne_m->SetYCor(playerOne_m->GetYcor() + 20); }        Add the following code to the inside of GOC::Destroy() function. A.) Delete playerOne_m's instance. B.) Delete playerTwo_m's instance.
13.) Click Build > Build and Run Project if there were no errors you should see
Figure 1: Input Handling.
Input Handling Complete Example:       You can get the complete example source code here. /* Date Created: May 06, 2010 FileName: main.cpp First Sprite Example */ #include "./source/PlatformSpecific/inc/GOC.h" #include "./source/PlatformSpecific/inc/DeviceInput.h" void GOC::Initialize() { /* Initialize your game scene, and game variables here. */ // Ball's starting postion. PointGOC ballPosition_l = {GetWindowWidth() / 2, GetWindowHeight() / 2, 0.0f}; // Initialize the ball. ball_m = new Sprite(); ball_m->Set2DPosition(ballPosition_l, 24, 24); ball_m->GetTexture()->LoadTexture("./images/Ball.png"); ball_m->SetVisible(false); //------------------------ // Player one's starting postion. PointGOC playerOnePosition_l = {15, 15, 0}; // Initialize player one. playerOne_m = new Sprite(); playerOne_m->Set2DPosition(playerOnePosition_l, 15, 200); playerOne_m->GetTexture()->LoadTexture("./images/Padle.png"); playerOne_m->SetVisible(true); //---------------------------- // Player one's starting postion. PointGOC playerTwoPosition_l = {GetWindowWidth() - 30, 15, 0}; // Initialize player two. playerTwo_m = new Sprite(); playerTwo_m->Set2DPosition(playerTwoPosition_l, 15, 200); playerTwo_m->GetTexture()->LoadTexture("./images/Padle.png"); playerTwo_m->SetVisible(true); //---------------------------- } void GOC::Update(TimeGOC *timeGOC_p, InputEngine *input_p) { /* Update your game scenes, check for collisions, check for input and play sounds here. */ // Move player two up and down with the mouse. playerTwo_m->SetYCor(input_p->YCordinate()); // Move player one up 5 units when up is pressed. if(input_p->IsKeyOrButtonPressed( KeysAndButtons::UP_KEYBOARD)) { playerOne_m->SetYCor(playerOne_m->GetYcor() - 20); } // Move player one down 5 units when down is pressed. if(input_p->IsKeyOrButtonPressed( KeysAndButtons::DOWN_KEYBOARD)) { playerOne_m->SetYCor(playerOne_m->GetYcor() + 20); } } void GOC::IndepedentUpdate(TimeGOC* timeGOC_p, InputEngine* input_p) { /* Update your game scenes, check for collisions, check for input and play sounds here. */ } void GOC::Destroy() { /* Clean up after your game here. */ // Destory your sprite when you are done with it. delete ball_m; // Destroy player One and player two. delete playerOne_m; delete playerTwo_m; } #ifdef _WIN32 // Define the windows main funciton. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) #else // Define the ubutnu main function. int main(int argc, char **argv) #endif { #ifdef _WIN32 // Start the GOC on windows. GOC::goc_i()->LoadGOC(hInstance); #else // Start the GOC on ubuntu. GOC::goc_i()->LoadGOC(); #endif // Destroy and cleanup GOC. GOC::goc_i()->UnloadGOC(); return 0; } Legal Info  About GOC  Contact Us  |
|