Written by: Andy Odle
Date: Mar 3, 2011
This tutorial utilizes v1.03 please download and use this version to prevent
problems that are present in previous versions. This tutorial also builds upon skills learned in the Organizing Code tutorial.
This tutorial covers:
Doing things when a timer goes off.
Creating New Balls Automatically:
1.) Open GOC.h in CodeLite.
2.) Add a TimerGOC pointer, called timer_m to the GOC class's private variables section.
3.) Open main.cpp.
4.) Set timer_m equal to a new TimerGOC() in the GOC::Initialize().
Initialize the Timer
void GOC::Initialize() { /* Initialize your game scene, and game variables here. */ // Initialize all the game balls. ball_m = new Ball(6000, GetWindowWidth(), GetWindowHeight()); // Has the game started? gameStarted_m = false; // Initialize timer. timer_m = new TimerGOC(); }
5.) Call ball_m's CreateBall() if the game has started and if the timer_m's
SecTimerTriggered(...) has triggered in the GOC::IndependentUpdate(...).
Timer Triggered Ball Creation
void GOC::IndependentUpdate( TimeGOC* timeGOC_p, InputEngine* input_p ) { /* Update your game scenes, check for collisions, check for input and play sounds here. */ // Add a new ball to the screen every 5 seconds. if( gameStarted_m == true && timer_m->SecTimerTriggered( 5, timeGOC_p->TotalElapsedSeconds() - timeGOC_p->ElapsedProgramSeconds(), true ) ) { ball_m->CreateBall(); } }
6.) Destroy the timer_m instance in the GOC::Destroy().
Destroy the Timer
void GOC::Destroy() { /* Clean up after your game here. */ delete ball_m; delete timer_m; }
7.) Click Build -> Build and Run Project in CodeLite.
Figure 1: Timer Example.
(Back to "Windows® " or "Ubuntu® " Tutorials)
Timers Complete Example:
You can get the complete example source code here.
/* File Name: GOC.h Created By: Andy Odle Date: Mar 2, 2011 */ #ifndef GOC_h #define GOC_h #ifdef _WIN32 #include "./source/PlatformSpecific/inc/WindowManager_WIN.h" #else #include "./source/PlatformSpecific/inc/WindowManager_UBUNTU.h" #endif #include "./source/PlatformSpecific/inc/ErrorReport.h" #include "./source/PlatformSpecific/inc/DeviceInput.h" #include "./source/PlatformSpecific/inc/Sprite.h" #include "./source/inc/TimeGOC.h" #include "./source/inc/TimerGOC.h" #include "./source/inc/InputEngine.h" #include "./source/inc/Render2D.h" #include "./source/inc/UniqueNumber.h" #include "./source/inc/Sound.h" #include "./source/inc/AnimationGOC.h" #include "./source/inc/MathGOC.h" /* Add new include files here */ class Ball { public: // Create the game balls // // Input: // Balls to create, game window width, and game window height Ball(int numberOfBalls_p, int windowWidth_p, int windowHeight_p); ~Ball(); // Add a new ball to the game. void CreateBall(); void UpdateBalls(Sprite *playerOne_p, Sprite *playerTwo_p); private: int ballCount_m; // Current number of balls in play. int numberOfBalls_m; // Total number of balls to play. int maxBallX_m; int maxBallY_m; float *ballVelocityX_m; float *ballVelocityY_m; int ballSpeed_m; float ballAngle_m; Sprite *balls_m; // Game balls. }; class GOC { public: // Initialize the developer's game. virtual void Initialize(); // Updates the developer's game state. virtual void Update(TimeGOC *timeGOC_p, InputEngine *input_p); virtual void IndependentUpdate(TimeGOC *timeGOC_p, InputEngine *input_p); // Ceans up the developer's game. virtual void Destroy(); // This is how you interact with the GOC. // // Output: // Returns the Game Object Creator instance pointer. static GOC *goc_i(); // Get the window's width and height. int GetWindowWidth(); int GetWindowHeight(); // Start the setup of the Game Object Creator. #ifdef _WIN32 void LoadGOC(HINSTANCE &hinstance_p); #else void LoadGOC(); #endif // Clean up the Game Object Creator. void UnloadGOC(); ErrorReport *GetErrorReport(); private: GOC(); ~GOC(); GOC(GOC const&){}; GOC &operator=(GOC const&){}; private: ErrorReport *errorReport_m; static GOC *goc_m; /* Add private variables here. */ TimerGOC *timer_m; Ball *ball_m; Sprite *playerOne_m; Sprite *playerTwo_m; int maxPlayerPosition_m; bool gameStarted_m; }; #endif
/* File Name: main.cpp Created By: Andy Odle Date: Mar 2, 2011 */ #include "./source/inc/GOC.h" Ball::Ball(int numberOfBalls_p, int windowWidth_p, int windowHeight_p) { numberOfBalls_m = numberOfBalls_p; ballCount_m = 0; // Max ball x and y cordinate. maxBallX_m = windowWidth_p - 24; maxBallY_m = windowHeight_p - 24; ballSpeed_m = 10; ballAngle_m = 60; ballVelocityX_m = new float[numberOfBalls_m]; ballVelocityY_m = new float[numberOfBalls_m]; // Create all the balls for the game. balls_m = new Sprite[numberOfBalls_m]; for(int count = 0; count < numberOfBalls_m; count++) { // Give each ball a different direction. ballVelocityX_m[count] = ballSpeed_m * cos(ballAngle_m + count); ballVelocityY_m[count] = ballSpeed_m * sin(ballAngle_m + count); // New balls start center screen. PointGOC ballPosition_l = { windowWidth_p / 2, windowHeight_p / 2, 0.0f}; balls_m[count].Set2DPosition(ballPosition_l, 24, 24); balls_m[count].GetTexture()->LoadTexture( "./images/Ball.png" ); balls_m[count].SetVisible(false); } } void Ball::CreateBall() { // Add a new ball into the game. if(ballCount_m < numberOfBalls_m) { balls_m[ballCount_m].SetVisible(true); ballCount_m++; } } void Ball::UpdateBalls(Sprite *playerOne_p, Sprite *playerTwo_p) { for(int count = 0 ; count < ballCount_m; count++) { if(balls_m[count].IsVisible()) { // Move the ball a little bit each frame. balls_m[count].SetXCor( balls_m[count].GetXcor() + ballVelocityX_m[count] ); balls_m[count].SetYCor( balls_m[count].GetYcor() + ballVelocityY_m[count] ); // Top Screen Edge Has been reached. if(balls_m[count].GetYcor() <= 0) { ballVelocityY_m[count] = -ballVelocityY_m[count]; balls_m[count].SetYCor(0); } // Bottom Screen Edge Has been reached. if(balls_m[count].GetYcor() >= maxBallY_m) { ballVelocityY_m[count] = -ballVelocityY_m[count]; balls_m[count].SetYCor(maxBallY_m); } // Right Screen Edge Has been reached. if(balls_m[count].GetXcor() >= maxBallX_m) { // Player One Scored remove ball from game. balls_m[count].SetVisible(false); } // Left Screen Edge Has been reached. if(balls_m[count].GetXcor() <= 0) { // Player Two Scored remove ball from game. balls_m[count].SetVisible(false); } // Ball collied with PlayerOne's paddle. if(playerOne_p->HasCollidedRight( &balls_m[count]) == CollidedSide::RIGHT_COLLISION ) { ballVelocityX_m[count] = -ballVelocityX_m[count]; balls_m[count].SetXCor(31); } // Ball collied with PlayerTwo's paddle. if(playerTwo_p->HasCollidedLeft( &balls_m[count]) == CollidedSide::LEFT_COLLISIOIN ) { ballVelocityX_m[count] = -ballVelocityX_m[count]; balls_m[count].SetXCor(maxBallX_m - 31); } } } } Ball::~Ball() { delete[] ballVelocityX_m; delete[] ballVelocityY_m; delete[] balls_m; } void GOC::Initialize() { /* Initialize your game scene, and game variables here. */ // Initialize all the game balls. ball_m = new Ball(6000, GetWindowWidth(), GetWindowHeight()); // Has the game started? gameStarted_m = false; // Initialize timer. timer_m = new TimerGOC(); // Initialize player one. PointGOC playerOnePosition_l = { 15, 15, 0.0f}; playerOne_m = new Sprite(); playerOne_m->Set2DPosition( playerOnePosition_l, 15, 200 ); playerOne_m->GetTexture()->LoadTexture( "./images/Padle.png" ); playerOne_m->SetVisible(true); // Initialize player two. PointGOC playerTwoPosition_l = { GetWindowWidth() - 30, 15, 0.0f }; playerTwo_m = new Sprite(); playerTwo_m->Set2DPosition( playerTwoPosition_l, 15, 200 ); playerTwo_m->GetTexture()->LoadTexture( "./images/Padle.png" ); playerTwo_m->SetVisible(true); maxPlayerPosition_m = GetWindowHeight() - playerOne_m->GetHeight(); } void GOC::Update(TimeGOC *timeGOC_p, InputEngine *input_p) { /* Update your game scenes, check for collisions, check for input and play sounds here. */ // Only update the balls if the game has begun. if(gameStarted_m) { ball_m->UpdateBalls(playerOne_m, playerTwo_m); } // If enter has been pressed start the game. if(input_p->IsKeyOrButtonPressed( KeysAndButtons::ENTER_KEYBOARD, false )) { gameStarted_m = true; } // Update PlayerTwo's position using the mouse. if(input_p->YCordinate() < maxPlayerPosition_m) { playerTwo_m->SetYCor( input_p->YCordinate() ); } else { playerTwo_m->SetYCor(maxPlayerPosition_m); } // Move PlayerOne up. if(input_p->IsKeyOrButtonPressed( KeysAndButtons::UP_KEYBOARD ) && playerOne_m->GetYcor() > 0) { playerOne_m->SetYCor(playerOne_m->GetYcor() - 20); } // Move PlayerOne down. if(input_p->IsKeyOrButtonPressed( KeysAndButtons::DOWN_KEYBOARD ) && playerOne_m->GetYcor() < maxPlayerPosition_m) { playerOne_m->SetYCor(playerOne_m->GetYcor() + 20); } } void GOC::IndependentUpdate(TimeGOC* timeGOC_p, InputEngine* input_p) { /* Update your game scenes, check for collisions, check for input and play sounds here. */ // Add a new ball to the screen every 5 seconds. if(gameStarted_m == true && timer_m->SecTimerTriggered(5, timeGOC_p->TotalElapsedSeconds() - timeGOC_p->ElapsedProgramSeconds(), true)) { ball_m->CreateBall(); } } void GOC::Destroy() { /* Clean up after your game here. */ delete ball_m; delete timer_m; 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 return 0; }
Legal Info  About GOC  Contact Us