Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed minor formatting for easier reading. #25

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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; }