Skip to content

Commit

Permalink
Reformatted and refactored code.
Browse files Browse the repository at this point in the history
  • Loading branch information
swwelch committed Jul 13, 2019
1 parent c180c89 commit a4457ef
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 85 deletions.
25 changes: 16 additions & 9 deletions src/controller.cpp
Original file line number Diff line number Diff line change
@@ -1,32 +1,39 @@
#include "controller.h"
#include <iostream>
#include "SDL.h"
#include "snake.h"

void Controller::HandleInput(bool &running, Snake &snake) {
void Controller::ChangeDirection(Snake &snake, Snake::Direction input,
Snake::Direction opposite) const {
if (snake.direction != opposite || snake.size == 1) snake.direction = input;
return;
}

void Controller::HandleInput(bool &running, Snake &snake) const {
SDL_Event e;
while (SDL_PollEvent(&e)) {
if (e.type == SDL_QUIT) {
running = false;
} else if (e.type == SDL_KEYDOWN) {
switch (e.key.keysym.sym) {
case SDLK_UP:
if (snake.last_dir != Snake::Direction::kDown || snake.size == 1)
snake.dir = Snake::Direction::kUp;
ChangeDirection(snake, Snake::Direction::kUp,
Snake::Direction::kDown);
break;

case SDLK_DOWN:
if (snake.last_dir != Snake::Direction::kUp || snake.size == 1)
snake.dir = Snake::Direction::kDown;
ChangeDirection(snake, Snake::Direction::kDown,
Snake::Direction::kUp);
break;

case SDLK_LEFT:
if (snake.last_dir != Snake::Direction::kRight || snake.size == 1)
snake.dir = Snake::Direction::kLeft;
ChangeDirection(snake, Snake::Direction::kLeft,
Snake::Direction::kRight);
break;

case SDLK_RIGHT:
if (snake.last_dir != Snake::Direction::kLeft || snake.size == 1)
snake.dir = Snake::Direction::kRight;
ChangeDirection(snake, Snake::Direction::kRight,
Snake::Direction::kLeft);
break;
}
}
Expand Down
6 changes: 5 additions & 1 deletion src/controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@

class Controller {
public:
void HandleInput(bool &running, Snake &snake);
void HandleInput(bool &running, Snake &snake) const;

private:
void ChangeDirection(Snake &snake, Snake::Direction input,
Snake::Direction opposite) const;
};

#endif
42 changes: 25 additions & 17 deletions src/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,50 @@
#include <iostream>
#include "SDL.h"

Game::Game(const std::size_t &grid_width, const std::size_t &grid_height)
: kGridWidth(grid_width), kGridHeight(grid_height), engine(dev()) {
snake = Snake(grid_width, grid_height);
Game::Game(std::size_t grid_width, std::size_t grid_height)
: snake(grid_width, grid_height),
engine(dev()),
random_w(0, static_cast<int>(grid_width)),
random_h(0, static_cast<int>(grid_height)) {
PlaceFood();
}

void Game::Run(Controller &controller, Renderer &renderer,
const std::size_t &ms_per_frame) {
Uint32 frame_ms, start = SDL_GetTicks(), before, after;
void Game::Run(Controller const &controller, Renderer &renderer,
std::size_t target_frame_duration) {
Uint32 title_timestamp = SDL_GetTicks();
Uint32 frame_start;
Uint32 frame_end;
Uint32 frame_duration;
int frame_count = 0;
bool running = true;

while (running) {
before = SDL_GetTicks();
frame_start = SDL_GetTicks();

// Input, Update, Render - the main game loop.
controller.HandleInput(running, snake);
Update();
renderer.Render(snake, food);

after = SDL_GetTicks();
frame_end = SDL_GetTicks();

// Keep track of how long each loop through the input/update/render cycle
// takes.
frame_count++;
frame_ms = after - before;
frame_duration = frame_end - frame_start;

if (after - start >= 1000) {
// After every second, update the window title.
if (frame_end - title_timestamp >= 1000) {
renderer.UpdateWindowTitle(score, frame_count);
frame_count = 0;
start = after;
title_timestamp = frame_end;
}

if (ms_per_frame > frame_ms) {
SDL_Delay(ms_per_frame - frame_ms);
// 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) {
SDL_Delay(target_frame_duration - frame_duration);
}
}
}
Expand All @@ -51,13 +60,11 @@ void Game::PlaceFood() {
if (!snake.SnakeCell(x, y)) {
food.x = x;
food.y = y;
break;
return;
}
}
}

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

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

Expand All @@ -71,9 +78,10 @@ void Game::Update() {
score++;
PlaceFood();
// Grow snake and increase speed.
snake.GrowBody(1);
snake.GrowBody();
snake.speed += 0.02;
}
}

int Game::GetScore() const { return score; }
int Game::GetSize() const { return snake.size; }
18 changes: 6 additions & 12 deletions src/game.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,32 @@
#define GAME_H

#include <random>
#include <vector>
#include "SDL.h"
#include "controller.h"
#include "renderer.h"
#include "snake.h"
//#include "SDL_image.h"

class Game {
public:
Game(const std::size_t &grid_width, const std::size_t &grid_height);
void Run(Controller &controller, Renderer &renderer,
const std::size_t &ms_per_frame);
Game(std::size_t grid_width, std::size_t grid_height);
void Run(Controller const &controller, Renderer &renderer,
std::size_t target_frame_duration);
int GetScore() const;
int GetSize() const;

private:
const std::size_t kGridWidth;
const std::size_t kGridHeight;

Snake snake;
SDL_Point food;

std::random_device dev;
std::mt19937 engine;
std::uniform_int_distribution<int> random_w{0, static_cast<int>(kGridWidth)};
std::uniform_int_distribution<int> random_h{0, static_cast<int>(kGridHeight)};
std::uniform_int_distribution<int> random_w;
std::uniform_int_distribution<int> random_h;

int score = 0;
int score{0};

void PlaceFood();
void Update();
void PollEvents();
};

#endif
44 changes: 12 additions & 32 deletions src/snake.cpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,8 @@
#include <math.h>
#include <snake.h>
#include "snake.h"
#include <cmath>
#include <iostream>

Snake::Snake(int grid_width, int grid_height)
: grid_width(grid_width), grid_height(grid_height) {
head_x = grid_width / 2;
head_y = grid_height / 2;
}

void Snake::Update() {
last_dir = dir;
SDL_Point prev_cell{
static_cast<int>(head_x),
static_cast<int>(
Expand All @@ -27,70 +20,57 @@ void Snake::Update() {
}

void Snake::UpdateHead() {
switch (dir) {
switch (direction) {
case Direction::kUp:
head_y -= speed;
head_x = floorf(head_x);
break;

case Direction::kDown:
head_y += speed;
head_x = floorf(head_x);
break;

case Direction::kLeft:
head_x -= speed;
head_y = floorf(head_y);
break;

case Direction::kRight:
head_x += speed;
head_y = floorf(head_y);
break;
}

// Wrap the Snake around to the beginning if going off of the screen.
if (head_x < 0) {
head_x = grid_width - 1;
} else if (head_x > grid_width) {
head_x = 0;
}

if (head_y < 0) {
head_y = grid_height - 1;
} else if (head_y > grid_height) {
head_y = 0;
}
head_x = fmod(head_x + grid_width, grid_width);
head_y = fmod(head_y + grid_height, grid_height);
}

void Snake::UpdateBody(SDL_Point &current_cell, SDL_Point &prev_cell) {
void Snake::UpdateBody(SDL_Point &current_head_cell, SDL_Point &prev_head_cell) {
// Add previous head location to vector
body.push_back(prev_cell);
body.push_back(prev_head_cell);

if (!growing) {
// Remove the tail from the vector.
body.erase(body.begin());
} else {
growing--;
growing = false;
size++;
}

// Check if the snake has died.
for (auto item : body) {
if (current_cell.x == item.x && current_cell.y == item.y) {
for (auto const &item : body) {
if (current_head_cell.x == item.x && current_head_cell.y == item.y) {
alive = false;
}
}
}

void Snake::GrowBody(int quantity) { growing += quantity; }
void Snake::GrowBody() { growing = true; }

// Inefficient method to check if cell is occupied by snake.
bool Snake::SnakeCell(int x, int y) {
if (x == static_cast<int>(head_x) && y == static_cast<int>(head_y)) {
return true;
}
for (auto item : body) {
for (auto const &item : body) {
if (x == item.x && y == item.y) {
return true;
}
Expand Down
31 changes: 17 additions & 14 deletions src/snake.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,31 @@ class Snake {
public:
enum class Direction { kUp, kDown, kLeft, kRight };

Snake() {}
Snake(int grid_width, int grid_height);
Snake(int grid_width, int grid_height)
: grid_width(grid_width),
grid_height(grid_height),
head_x(grid_width / 2),
head_y(grid_height / 2) {}

void Update();
void UpdateHead();
void UpdateBody(SDL_Point &current_cell, SDL_Point &prev_cell);
void GrowBody(int quantity);
bool SnakeCell(int x, int y);

Direction last_dir = Direction::kUp;
Direction dir = Direction::kUp;

std::vector<SDL_Point> body;
void GrowBody();
bool SnakeCell(int x, int y);

int growing = 0;
float speed = 0.1f;
int size = 1;
bool alive = true;
Direction direction = Direction::kUp;

float speed{0.1f};
int size{1};
bool alive{true};
float head_x;
float head_y;
std::vector<SDL_Point> body;

private:
void UpdateHead();
void UpdateBody(SDL_Point &current_cell, SDL_Point &prev_cell);

bool growing{false};
int grid_width;
int grid_height;
};
Expand Down

0 comments on commit a4457ef

Please sign in to comment.