// 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(); } }
int main() { constexpr int brkCountX{11}, brkCountY{4}; constexpr int brkStartColumn{1}, brkStartRow{2}; constexpr float brkSpacing{3.f}, brkOffsetX{22.f}; Ball ball{wndWidth / 2.f, wndHeight / 2.f}; Paddle paddle{wndWidth / 2, wndHeight - 50}; std::vector<Brick> bricks; for(int iX{0}; iX < brkCountX; ++iX) for(int iY{0}; iY < brkCountY; ++iY) { float x{(iX + brkStartColumn) * (Brick::defWidth + brkSpacing)}; float y{(iY + brkStartRow) * (Brick::defHeight + brkSpacing)}; bricks.emplace_back(brkOffsetX + x, y); } sf::RenderWindow window{{wndWidth, wndHeight}, "Arkanoid - 8"}; window.setFramerateLimit(60); while(true) { window.clear(sf::Color::Black); if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Escape)) break; 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(); } return 0; }
// 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(); } }