diff --git a/src/game.cpp b/src/game.cpp index cc7d60f9..3f85e55a 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -11,7 +11,8 @@ Game::Game(std::size_t grid_width, std::size_t grid_height) } void Game::Run(Controller const &controller, Renderer &renderer, - std::size_t target_frame_duration) { + std::size_t target_frame_duration) +{ Uint32 title_timestamp = SDL_GetTicks(); Uint32 frame_start; Uint32 frame_end; @@ -29,13 +30,13 @@ void Game::Run(Controller const &controller, Renderer &renderer, frame_end = SDL_GetTicks(); - // Keep track of how long each loop through the input/update/render cycle - // takes. + // Keep track of how long each loop through the input/update/render cycle takes. frame_count++; frame_duration = frame_end - frame_start; // After every second, update the window title. - if (frame_end - title_timestamp >= 1000) { + if (frame_end - title_timestamp >= 1000) + { renderer.UpdateWindowTitle(score, frame_count); frame_count = 0; title_timestamp = frame_end; @@ -44,20 +45,22 @@ void Game::Run(Controller const &controller, Renderer &renderer, // If the time for this frame is too small (i.e. frame_duration is // smaller than the target ms_per_frame), delay the loop to // achieve the correct frame rate. - if (frame_duration < target_frame_duration) { + if (frame_duration < target_frame_duration) + { SDL_Delay(target_frame_duration - frame_duration); } } } -void Game::PlaceFood() { +void Game::PlaceFood() +{ int x, y; while (true) { x = random_w(engine); y = random_h(engine); - // Check that the location is not occupied by a snake item before placing - // food. - if (!snake.SnakeCell(x, y)) { + // Check that the location is not occupied by a snake item before placing food. + if (!snake.SnakeCell(x, y)) + { food.x = x; food.y = y; return; @@ -65,15 +68,17 @@ void Game::PlaceFood() { } } -void Game::Update() { - if (!snake.alive) return; +void Game::Update() +{ + if (!snake.alive) + return; snake.Update(); int new_x = static_cast(snake.head_x); int new_y = static_cast(snake.head_y); - // Check if there's food over here + // To Check if there's food over here if (food.x == new_x && food.y == new_y) { score++; PlaceFood(); @@ -84,4 +89,4 @@ void Game::Update() { } int Game::GetScore() const { return score; } -int Game::GetSize() const { return snake.size; } \ No newline at end of file +int Game::GetSize() const { return snake.size; }