/* Callback handler for window re-paint event */ void display() { glClear(GL_COLOR_BUFFER_BIT); // Clear the color buffer glMatrixMode(GL_MODELVIEW); // To operate on the model-view matrix glLoadIdentity(); // Reset model-view matrix paddleRight.move(); ball.draw(); paddleRight.draw(); paddleLeft.draw(); glutSwapBuffers(); // Swap front and back buffers (of double buffered mode) // Animation Control - compute the location for the next refresh ball.contactLeft(paddleLeft); ball.contactRight(paddleRight); ball.move(); ai.AIPaddleMove(ball); //ai2.AIPaddleMove(ball); if (ball.didRoundEnd() == 'R') { ++scoreRight; ball.resetBallPos(); } else if (ball.didRoundEnd() == 'L') { ++scoreLeft; ball.resetBallPos(); } else return; }
// Il metodo `run` farà partire il game loop. void run() { while(true) { window.clear(sf::Color::Black); if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Escape)) break; // Il tasto `P` gestirà la pausa. if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::P)) { // Prima di mettere/togliere la pausa, controlliamo // se il tasto era già stato pressato. if(!pausePressedLastFrame) { if(state == State::Paused) state = State::InProgress; else if(state == State::InProgress) state = State::Paused; } pausePressedLastFrame = true; } else pausePressedLastFrame = false; // Il tasto `R` farà ricominciare il gioco. if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::R)) restart(); // Se il gioco è in pausa, non aggiorneremo i game // object. if(state != State::Paused) { ball.update(); paddle.update(); for(auto& brick : bricks) { brick.update(); solveBrickBallCollision(brick, ball); } bricks.erase( std::remove_if(std::begin(bricks), std::end(bricks), [](const auto& mBrick) { return mBrick.destroyed; }), std::end(bricks)); solvePaddleBallCollision(paddle, ball); } ball.draw(window); paddle.draw(window); for(auto& brick : bricks) brick.draw(window); window.display(); } }
// The `run` method will start the game loop. void run() { while(true) { window.clear(sf::Color::Black); if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Escape)) break; // The `P` key will toggle the pause. To prevent continuous // use of the pause button, we need to check if the input // was pressed last frame. if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::P)) { // If `P` was not pressed last frame, we can toggle // the state. if(!pausePressedLastFrame) { if(state == State::Paused) state = State::InProgress; else if(state == State::InProgress) state = State::Paused; } pausePressedLastFrame = true; } else pausePressedLastFrame = false; // Let's also use the `R` key to restart the game. if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::R)) restart(); // If the game is paused, we'll only draw game elements, // without updating them. if(state != State::Paused) { // The rest of the game loop code is exactly the same. ball.update(); paddle.update(); for(auto& brick : bricks) { brick.update(); solveBrickBallCollision(brick, ball); } bricks.erase( std::remove_if(std::begin(bricks), std::end(bricks), [](const auto& mBrick) { return mBrick.destroyed; }), std::end(bricks)); solvePaddleBallCollision(paddle, ball); } ball.draw(window); paddle.draw(window); for(auto& brick : bricks) brick.draw(window); window.display(); } }