void PluginPong::update(unsigned long timeDiff, boolean realtimeSync) { if (!gameOver) { // Update the game movePaddles(); moveBall(); gameOver = isGameOver(); } // Draw it! matrix.clear(); matrix.drawLine(paddleTop.x, paddleTop.y, paddleTop.x + PADDLE_WIDTH - 1, paddleTop.y); matrix.drawLine(paddleBottom.x, paddleBottom.y, paddleBottom.x + PADDLE_WIDTH - 1, paddleBottom.y); matrix.setPixel(ball.x, ball.y); }
bool TutorialApplication::frameRenderingQueued(const Ogre::FrameEvent& evt) { float deltaTime = mTimer.getMicroseconds() / 1000.0f; deltaTime /= 4; mTimer.reset(); if(BaseApplication::frameRenderingQueued(evt)){ movePaddles(deltaTime); moveBall(deltaTime); mSceneMgr->getLight("spotLight")->setDirection(mBall->getPosition()-mSceneMgr->getLight("spotLight")->getPosition()); mSceneMgr->getLight("spotLight2")->setDirection(mBall->getPosition()-mSceneMgr->getLight("spotLight2")->getPosition()); return true; } else { return false; } }
int main(int argc, char *argv[]) { SDL_Init(SDL_INIT_VIDEO); displayWindow = SDL_CreateWindow("My Game", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1280, 720, SDL_WINDOW_OPENGL); SDL_GLContext context = SDL_GL_CreateContext(displayWindow); SDL_GL_MakeCurrent(displayWindow, context); #ifdef _WINDOWS glewInit(); #endif glViewport(0, 0, 1280, 720); ShaderProgram program(RESOURCE_FOLDER"vertex.glsl", RESOURCE_FOLDER"fragment.glsl"); Entity rightpaddle(program); rightpaddle.moveRight(1.50); Entity ball(program); Entity leftpaddle(program); leftpaddle.moveLeft(1.50); Time counter; float elapsed; float paddleVertex[] = { -0.025, -0.175, 0.025, -0.175, 0.025, 0.175, -0.025, -0.175, 0.025, 0.175, -0.025, 0.175 }; float ballVertex[] = { -0.035, -0.035, 0.035, -0.035, 0.035, 0.035, -0.035, -0.035, 0.035, 0.035, -0.035, 0.035 }; SDL_Event event; bool done = false; while (!done) { while (SDL_PollEvent(&event)) { if (event.type == SDL_QUIT || event.type == SDL_WINDOWEVENT_CLOSE) { done = true; } } glClear(GL_COLOR_BUFFER_BIT); elapsed = counter.getTime(); rightpaddle.setMatrix(); rightpaddle.Draw(paddleVertex); ball.setMatrix(); ball.Draw(ballVertex); leftpaddle.setMatrix(); leftpaddle.Draw(paddleVertex); detectCollision(ball, leftpaddle, rightpaddle, elapsed); movePaddles(leftpaddle, rightpaddle, elapsed); moveBall(ball, elapsed); detectWin(ball, elapsed); SDL_GL_SwapWindow(displayWindow);// keep at bottom } SDL_Quit(); return 0; }
void runGameplay(GameVars* gameVars) { //GAME //Draw the court drawCourt(); //Draw the scores drawScores(gameVars); //Game play states switch (gameVars->gameState) { case GSGameStart: { #ifdef DEBUG debugPrint("START\x80"); #endif //Draw Ready message drawReady(0x80); //Wait for a number of frames waitForFramesThenMoveOnToNextState(60, gameVars); } break; case GSWaitForServe: { #ifdef DEBUG debugPrint("SERVE\x80"); #endif //Score max? if (gameVars->player1.score >= maxScoreForPlayer || gameVars->player2.score >= maxScoreForPlayer) { //Served - move to next state moveToNextGameState(gameVars, GEWinner); return; } //Draw player paddles movePaddles(gameVars); //Inform who is about to serve drawServe(gameVars->player1ServeNext); //wait for user to start game with button 4 readButton(); //Player 1 Button 4 or P2 Button 4 if ( (gameVars->player1ServeNext && (_BTN_CUR_MASK & _JOY1_B4)) || (!gameVars->player1ServeNext && (_BTN_CUR_MASK & _JOY2_B4)) ) { //Position the ball and make visible serve(gameVars); //Served - move to next state moveToNextGameState(gameVars, GENone); } } break; case GSPlay: { #ifdef DEBUG debugPrint("PLAY\x80"); #endif //GAME PLAY ACTION drawCenterLine(gameVars); //Draw player paddles movePaddles(gameVars); //Only deal with visible balls if(gameVars->ball.visibility == false) return; //Move ball - check for goal moveBallSprite(gameVars); } break; case GSScored: { #ifdef DEBUG debugPrint("SCORED\x80"); #endif //Goal! drawPoint(!gameVars->player1ServeNext); waitForFramesThenMoveOnToNextState(60, gameVars); } break; case GSGameOver: { #ifdef DEBUG debugPrint("GAMEOVER\x80"); #endif //Display 'Game Over' drawGameOver(gameVars->player1.score > gameVars->player2.score); //wait for user to select replay or menu readButton(); //Player 1 Button 1 if (_BTN_CUR_MASK & _JOY1_B1) { //Reset scores resetPlayerScores(gameVars); //Replay - move to next state moveToNextGameState(gameVars, GEPlayAgain); } else if (_BTN_CUR_MASK & _JOY1_B2) { //Reset scores resetPlayerScores(gameVars); //Menu - move to next state moveToNextGameState(gameVars, GEBackToMenu); } } break; default: break; } }