Skip to content

Commit

Permalink
Fixed minor formatting for easier reading.
Browse files Browse the repository at this point in the history
No core changes , directly or indirectly affecting the file were made.
  • Loading branch information
electrify-7 authored Oct 1, 2023
1 parent 7f1c6f2 commit 3337ff0
Showing 1 changed file with 18 additions and 13 deletions.
31 changes: 18 additions & 13 deletions src/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -44,36 +45,40 @@ 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;
}
}
}

void Game::Update() {
if (!snake.alive) return;
void Game::Update()
{
if (!snake.alive)
return;

snake.Update();

int new_x = static_cast<int>(snake.head_x);
int new_y = static_cast<int>(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();
Expand All @@ -84,4 +89,4 @@ void Game::Update() {
}

int Game::GetScore() const { return score; }
int Game::GetSize() const { return snake.size; }
int Game::GetSize() const { return snake.size; }

0 comments on commit 3337ff0

Please sign in to comment.