diff --git a/.cache/clangd/index/fruit.cpp.80CA8E09C6743CC5.idx b/.cache/clangd/index/fruit.cpp.80CA8E09C6743CC5.idx index 86dd2d7..46270e5 100644 Binary files a/.cache/clangd/index/fruit.cpp.80CA8E09C6743CC5.idx and b/.cache/clangd/index/fruit.cpp.80CA8E09C6743CC5.idx differ diff --git a/.cache/clangd/index/fruit.hpp.B7520D20FB102EAE.idx b/.cache/clangd/index/fruit.hpp.B7520D20FB102EAE.idx index 859faaa..68c1294 100644 Binary files a/.cache/clangd/index/fruit.hpp.B7520D20FB102EAE.idx and b/.cache/clangd/index/fruit.hpp.B7520D20FB102EAE.idx differ diff --git a/.cache/clangd/index/main.cpp.C070F90AD8D66EE4.idx b/.cache/clangd/index/main.cpp.C070F90AD8D66EE4.idx index 051f830..d3d1fc7 100644 Binary files a/.cache/clangd/index/main.cpp.C070F90AD8D66EE4.idx and b/.cache/clangd/index/main.cpp.C070F90AD8D66EE4.idx differ diff --git a/.cache/clangd/index/point.cpp.5A6C80E33F52E8D8.idx b/.cache/clangd/index/point.cpp.5A6C80E33F52E8D8.idx index 6cb2fe4..1384c2f 100644 Binary files a/.cache/clangd/index/point.cpp.5A6C80E33F52E8D8.idx and b/.cache/clangd/index/point.cpp.5A6C80E33F52E8D8.idx differ diff --git a/.cache/clangd/index/snake.cpp.60A7519252B9A7E3.idx b/.cache/clangd/index/snake.cpp.60A7519252B9A7E3.idx index a6e9199..389ad64 100644 Binary files a/.cache/clangd/index/snake.cpp.60A7519252B9A7E3.idx and b/.cache/clangd/index/snake.cpp.60A7519252B9A7E3.idx differ diff --git a/.cache/clangd/index/snake.hpp.8322EDE86440CED8.idx b/.cache/clangd/index/snake.hpp.8322EDE86440CED8.idx index 3c41a92..c94449e 100644 Binary files a/.cache/clangd/index/snake.hpp.8322EDE86440CED8.idx and b/.cache/clangd/index/snake.hpp.8322EDE86440CED8.idx differ diff --git a/.cache/clangd/index/window.cpp.CFB09F5DADCCC8C6.idx b/.cache/clangd/index/window.cpp.CFB09F5DADCCC8C6.idx index e7d2b64..ed73a2d 100644 Binary files a/.cache/clangd/index/window.cpp.CFB09F5DADCCC8C6.idx and b/.cache/clangd/index/window.cpp.CFB09F5DADCCC8C6.idx differ diff --git a/CMakeLists.txt b/CMakeLists.txt index 2fdbb24..817e200 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,8 +3,6 @@ project(snake) set(CMAKE_C_STANDARD 23) set(CMAKE_C_STANDARD_REQUIRED ON) -set(CMAKE_VERBOSE_MAKEFILE ON) - include_directories(include) set(SOURCES diff --git a/include/fruit.hpp b/include/fruit.hpp index 26ce878..a893308 100644 --- a/include/fruit.hpp +++ b/include/fruit.hpp @@ -10,6 +10,6 @@ struct Fruit { Point m_point; Fruit(); void create_point(); - void reset(std::array, ROW> &arr); + void reset(std::array, ROW> &arr, int row, int col, int &score); }; #endif // FRUIT_HPP diff --git a/include/point.hpp b/include/point.hpp index 8d41611..8a1d436 100644 --- a/include/point.hpp +++ b/include/point.hpp @@ -10,7 +10,7 @@ struct Point { Point() {} Point(int x, int y) : m_row(x), m_col(y) {} - std::string get_point() const; + std::string to_string() const; void clear(std::array, ROW> &arr) const; std::pair get_point(int direction, int row, int col); }; diff --git a/include/snake.hpp b/include/snake.hpp index b659d20..dc8feda 100644 --- a/include/snake.hpp +++ b/include/snake.hpp @@ -1,11 +1,17 @@ #ifndef SNAKE_HPP #define SNAKE_HPP +#include +#include "point.hpp" + struct Snake { Snake(int default_direction); - int get_direction() const; - void set_direction(int x); - int direction; // 1: Down, 2: Up, 3: Left, 4: Right + void create_tail(int row, int col); + void update_tails_count(); + + std::vector m_tails; + int m_direction; // 1: Down, 2: Up, 3: Left, 4: Right + int m_tails_count; }; #endif diff --git a/src/fruit.cpp b/src/fruit.cpp index d4b4680..d7b90f0 100644 --- a/src/fruit.cpp +++ b/src/fruit.cpp @@ -13,7 +13,10 @@ void Fruit::create_point() { m_col = m_point.m_col; } -void Fruit::reset(std::array, ROW> &arr) { - m_point.clear(arr); - create_point(); +void Fruit::reset(std::array, ROW> &arr, int row, int col, int &score) { + if(m_row == row && m_col == col) { + m_point.clear(arr); + create_point(); + score++; + } } diff --git a/src/main.cpp b/src/main.cpp index 8ba43ca..a74fb9f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -9,51 +9,66 @@ #include "globals.hpp" #include "input.hpp" -Snake snake {1}; -Window window; -Input input; -Fruit fruit; - int main() { - int score = 0; - std::array, ROW> &board = window.m_board; - bool update = true; - int row = 0, col = 0; + Snake snake {1}; + Window window; + Input input; + Fruit fruit; - Point previous {row,col}; - fruit.create_point(); - std::thread input_thread(&Input::get_input, &input); - while(update) { - if (input.m_current_key == 's') snake.set_direction(1); - if (input.m_current_key == 'w') snake.set_direction(2); - if (input.m_current_key == 'a') snake.set_direction(3); - if (input.m_current_key == 'd') snake.set_direction(4); - if (input.m_current_key == 'q') break; - - if(fruit.m_row == row && fruit.m_col == col) { - fruit.reset(board); - ++score; + int x = 5; + int y = 5; + for(int i = 0; i <= x; i++) { + for(int a = 0; a <= y; a++) { + snake.create_tail(i, a); } - std::tie(row, col) = previous.get_point(snake.get_direction(), row, col); - - board[row][col] = '*'; - board[fruit.m_row][fruit.m_col] = '*'; - std::cout << "Score: " << score << std::endl; - previous.clear(board); // Clear old point - previous = Point {row, col}; // Set new previous - window.update_loop(5); // Update loop to clear board, tick and redisplay board } - input_thread.join(); - /* First - * Learn how to make threads, - * for the input thread call the thread then use a while loop within the thread, DO NOT use a while loop to call the thread - * - * for the update display thread use a while loop within the thread, DO NOT USE a while loop to call the thread. - * - * the update display thread will ALSO BE YOUR GAME UPDATE THREAD. Your input thread should only be used for INPUT no other function calls should be made and it should NEVER be put to sleep. - * - */ + for(int i = 0; i < snake.m_tails.size(); i++) { + std::cout << &snake.m_tails.at(i) << std::endl; + } + std::cout << snake.m_tails.size() << std::endl; + + + // int score = 0; + // int row = 0, col = 0; + // int fps = 5; + // bool running = true; + // + // Point previous {row,col}; // Creating initial snake head position + // fruit.create_point(); // Creating initial fruit object + // std::array, ROW> &board = window.m_board; + // std::thread input_thread(&Input::get_input, &input); + // while(running) { + // switch (input.m_current_key) { + // case 's': + // snake.m_direction = 1; + // break; + // case 'w': + // snake.m_direction = 2; + // break; + // case 'a': + // snake.m_direction = 3; + // break; + // case 'd': + // snake.m_direction = 4; + // break; + // case 'q': + // running = false; + // break; + // } + // + // fruit.reset(board, row, col, score); + // std::tie(row, col) = previous.get_point(snake.m_direction, row, col); + // + // board[row][col] = '*'; + // board[fruit.m_row][fruit.m_col] = '*'; + // std::cout << "Score: " << score << std::endl; + // previous.clear(board); // Clear old previous point + // previous = Point {row, col}; // Set new previous point + // window.update_loop(fps); // Update loop to clear board, tick and redisplay board + // } + // + // input_thread.join(); return 0; } diff --git a/src/point.cpp b/src/point.cpp index b8e9a0f..dc3113b 100644 --- a/src/point.cpp +++ b/src/point.cpp @@ -1,7 +1,7 @@ #include #include "point.hpp" -std::string Point::get_point() const { +std::string Point::to_string() const { return "(" + std::to_string(m_row) + ", " + std::to_string(m_col) + ")"; } @@ -10,20 +10,25 @@ void Point::clear(std::array, ROW> &arr) const { } std::pair Point::get_point(int direction, int row, int col) { - if (direction == 1) { - row++; - if (row >= ROW) row = 0; - } else if (direction == 2) { - row--; - if (row < 0) row = ROW - 1; - } else if (direction == 3) { - col--; - if (col < 0) col = COL - 1; - } else if (direction == 4) { - col++; - if (col >= COL) col = 0; + switch (direction) { + case 1: + row ++; + if (row >= ROW) row = 0; + break; + case 2: + row--; + if (row < 0) row = ROW - 1; + break; + case 3: + col--; + if (col < 0) col = COL - 1; + break; + case 4: + col++; + if (col >= COL) col = 0; + break; } - return {row, col}; + return {row,col}; } /* Create save_point to take old_direction, new_direction, row and col diff --git a/src/snake.cpp b/src/snake.cpp index 550693e..4fc7ebc 100644 --- a/src/snake.cpp +++ b/src/snake.cpp @@ -1,16 +1,12 @@ #include "snake.hpp" -#include Snake::Snake(int default_direction) { - direction = default_direction; + m_direction = default_direction; + m_tails_count = 0; } -int Snake::get_direction() const { - return direction; -} - -void Snake::set_direction(int x) { - direction = x; +void Snake::create_tail(int row, int col) { + m_tails.push_back(Point {row, col}); } /* snake head for instance is (3, 0) diff --git a/src/window.cpp b/src/window.cpp index d960468..590d668 100644 --- a/src/window.cpp +++ b/src/window.cpp @@ -29,6 +29,6 @@ void Window::tick(double frames_per_second) { void Window::update_loop(int fps) { clear_window(); - tick(fps); draw_window(); + tick(fps); }