// 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 () { Ball ball{windowWidth / 2, windowHeight /2 }; Paddle paddle {windowWidth / 2, windowHeight - 50}; std::vector<Brick> bricks; for(int iX{0}; iX < countBlocksX; ++iX) for(int iY{0}; iY < countBlocksY; ++iY) bricks.emplace_back((iX + 1) * (blockWidth + 3) +22, (iY + 2) * (blockHeight +3)); // { // Equivalente a emplace_back : C++11 // Brick umBrick{(iX + 1) * (blockWidth + 3) +22, // (iY + 2) * (blockHeight +3)}; // bricks.push_back(umBrick); // } sf::RenderWindow window({windowWidth,windowHeight}, "Arkanoid - 5"); window.setFramerateLimit(60); // Loop do jogo while (window.isOpen()) { // Monitora os eventos e reage de acordo sf::Event event; while(window.pollEvent(event)) { if(event.type == sf::Event::Closed || (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Key::Escape)) { window.close(); } } // Limpa a tela window.clear(sf::Color::Black); // Renderiza os objetos na tela ball.update(); paddle.update(); // testa colisão a cada ciclo testCollision(paddle, ball); // remove blocos colididos bricks.erase( remove_if( begin(bricks), end(bricks), [](const Brick& mBrick){ return mBrick.destroyed; }), end(bricks) ); // testa colisões entre bola e blocos for(auto& brick : bricks) testCollision(brick,ball); window.draw(ball.shape); window.draw(paddle.shape); // for(int i{0}; i < bricks.size(); ++i) // Equivalente for(auto& brick : bricks) // C++11 window.draw(brick.shape); // Exibe os conteúdos da tela window.display(); } return EXIT_SUCCESS; }
// 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(); } }
int main( void ) { Setup setup; setup.init(); window = setup.get_window(); draw_paddle(paddle.get_x()-paddle_size_x/2, paddle.get_y()+paddle_size_y/2, paddle_size_x, paddle_size_y); draw_ball(); vector<Brick> bricks = get_bricks(); draw_bricks(bricks); init_hexagon(); draw_hexagon(); // Ensure we can capture the escape key being pressed below glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE); // Dark blue background glClearColor(0.0f, 0.0f, 0.4f, 0.0f); glfwSetKeyCallback(window, keyCalback); GLuint VertexArrayID; glGenVertexArrays(1, &VertexArrayID); glBindVertexArray(VertexArrayID); // Create and compile our GLSL program from the shaders GLuint programID = LoadShaders( "SimpleVertexShader.vertexshader", "SimpleFragmentShader.fragmentshader" ); OpenGl opengl(programID); GLuint vertexbuffer; glGenBuffers(1, &vertexbuffer); glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer); glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat)*g_vertex_buffer_data.size(), &g_vertex_buffer_data[0], GL_DYNAMIC_DRAW); do{ glClear(GL_COLOR_BUFFER_BIT); if(ball.get_y() < -1.0){ ball.set_y(0.0); } // Use our shader glUseProgram(programID); // Draw nothing, see you in tutorial 2 ! // 1rst attribute buffer : vertices glEnableVertexAttribArray(0); glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer); glVertexAttribPointer( 0, // attribute 0. No particular reason for 0, but must match the layout in the shader. 2, // size GL_FLOAT, // type GL_FALSE, // normalized? 0, // stride (void*)0 // array buffer offset ); opengl.draw_hexagon(g_hexagonOffset); ball.update(); paddle.update(666); collision.test_collision(paddle, ball, ball_velocity); for(auto& brick : bricks) test_collision(brick, ball); // Ball opengl.draw_ball(ball.get_x(), ball.get_y()); // Paddle opengl.draw_paddle(paddle.get_x()); opengl.draw_bricks(bricks); // Swap buffers glDisableVertexAttribArray(0); glfwSwapBuffers(window); glfwPollEvents(); } // Check if the ESC key was pressed or the window was closed while( glfwGetKey(window, GLFW_KEY_ESCAPE ) != GLFW_PRESS && glfwWindowShouldClose(window) == 0 ); // Close OpenGL window and terminate GLFW glfwTerminate(); return 0; }