From 5e887e36880dda2d3b8073af05a6ce774f65b53c Mon Sep 17 00:00:00 2001 From: DarthMooMancer <107283522+DarthMooMancer@users.noreply.github.com> Date: Thu, 21 Aug 2025 09:17:37 -0400 Subject: [PATCH] wip --- include/fruit.hpp | 6 +-- include/globals.hpp | 1 + include/input.hpp | 5 +- include/point.hpp | 2 +- include/snake.hpp | 13 +++--- include/window.hpp | 2 + src/fruit.cpp | 4 +- src/input.cpp | 19 ++++---- src/main.cpp | 111 +++++++++++++++++++++----------------------- src/point.cpp | 34 ++++++-------- src/snake.cpp | 38 +++++++-------- src/window.cpp | 32 +++++++------ 12 files changed, 134 insertions(+), 133 deletions(-) diff --git a/include/fruit.hpp b/include/fruit.hpp index a893308..64aa5a6 100644 --- a/include/fruit.hpp +++ b/include/fruit.hpp @@ -4,12 +4,12 @@ #include "point.hpp" struct Fruit { + Fruit(); int unsigned m_row; int unsigned m_col; - Point m_point; - Fruit(); + void create_point(); - void reset(std::array, ROW> &arr, int row, int col, int &score); + void reset(std::array, ROW> &arr, int row, int col, int &score, int &count); }; #endif // FRUIT_HPP diff --git a/include/globals.hpp b/include/globals.hpp index 0963164..9cd596a 100644 --- a/include/globals.hpp +++ b/include/globals.hpp @@ -5,5 +5,6 @@ constexpr uint16_t ROW = 10; constexpr uint16_t COL = 10; +constexpr uint16_t FPS = 5; #endif // GLOBALS_HPP diff --git a/include/input.hpp b/include/input.hpp index f2931b5..4dec538 100644 --- a/include/input.hpp +++ b/include/input.hpp @@ -2,8 +2,9 @@ #define INPUT_HPP struct Input { - char m_current_key = ' '; - void get_input(); + char m_current_key = ' '; + char m_old_key = ' '; + void get_input(); }; #endif // INPUT_HPP diff --git a/include/point.hpp b/include/point.hpp index c7d0689..6730ec1 100644 --- a/include/point.hpp +++ b/include/point.hpp @@ -12,7 +12,7 @@ struct Point { std::string to_string() const; void clear(std::array, ROW> &arr) const; - std::pair get_new_point(int direction, int row, int col); + void get_new_point(int direction); }; #endif // POINT_HPP diff --git a/include/snake.hpp b/include/snake.hpp index d326b5c..1e1a0d0 100644 --- a/include/snake.hpp +++ b/include/snake.hpp @@ -5,14 +5,13 @@ #include "point.hpp" struct Snake { - Snake(int default_direction); - void create_tail(int row, int col); - std::pair get_new_point(int tail_index, int direction, int row, int col); - void update_tails_count(); - - std::vector m_tails; + Snake(int default_direction, int row, int col); + Point m_head; + std::vector m_tails; int m_direction; // 1: Down, 2: Up, 3: Left, 4: Right - int m_tails_count; + + void create_tail(int row, int col); // Adds a new tail to m_tails at { row, col } + void get_new_tail_point(int tail_index, int direction); }; #endif diff --git a/include/window.hpp b/include/window.hpp index 86fa0c6..139c710 100644 --- a/include/window.hpp +++ b/include/window.hpp @@ -5,9 +5,11 @@ struct Window { std::array, ROW> m_board; + int m_score; // Initializes board with . for each value Window(); + void draw_score(); void draw_window(); void clear_window(); void tick(double frames_per_second); diff --git a/src/fruit.cpp b/src/fruit.cpp index d7b90f0..ec1a1d1 100644 --- a/src/fruit.cpp +++ b/src/fruit.cpp @@ -5,6 +5,7 @@ Fruit::Fruit() { std::srand(std::time(0)); + create_point(); } void Fruit::create_point() { @@ -13,10 +14,11 @@ void Fruit::create_point() { m_col = m_point.m_col; } -void Fruit::reset(std::array, ROW> &arr, int row, int col, int &score) { +void Fruit::reset(std::array, ROW> &arr, int row, int col, int &score, int &count) { if(m_row == row && m_col == col) { m_point.clear(arr); create_point(); score++; + count++; } } diff --git a/src/input.cpp b/src/input.cpp index 3635b90..0a5ad3e 100644 --- a/src/input.cpp +++ b/src/input.cpp @@ -2,15 +2,16 @@ #include void Input::get_input() { - while(true) { - initscr(); - cbreak(); - noecho(); + while(true) { + initscr(); + cbreak(); + noecho(); - m_current_key = getch(); - endwin(); - if(m_current_key == 'q') { - break; + m_old_key = m_current_key; + m_current_key = getch(); + endwin(); + if (m_current_key == 'q') { + break; + } } - } } diff --git a/src/main.cpp b/src/main.cpp index 49cc149..877ab37 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -2,7 +2,6 @@ #include #include #include -#include #include "point.hpp" #include "fruit.hpp" #include "snake.hpp" @@ -11,65 +10,61 @@ #include "input.hpp" int main (int argc, char *argv[]) { - Snake snake {1}; - Window window; - Input input; - Fruit fruit; + Snake snake { 1, 0, 0 }; + Window window; + Input input; + Fruit fruit; - int x = 5; - for(int i = 0; i < x; i++) { - snake.create_tail(i, 0); - } + std::thread input_thread(&Input::get_input, &input); + bool running = true; + int count = 0; + 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; + } - int row = 0, col = 0; - int count = 0; - while(count < 2) { - count++; - for(int i = 0; i < snake.m_tails.size(); i++) { - std::tie(row, col) = snake.get_new_point(i, 1, row, col); - std::cout << snake.m_tails.at(i).to_string() << std::endl; + fruit.reset(window.m_board, snake.m_head.m_row, snake.m_head.m_col, window.m_score, count); + if (window.m_score % 2 == 0 && count > 0) { + if (snake.m_direction == 1) { + int r = snake.m_head.m_row; + if (r + 1 > ROW - 1) { + r = snake.m_head.m_row; + } + snake.create_tail(snake.m_head.m_row + r + 1, snake.m_head.m_col); + ++r; + } + count--; + } + + /* TODO + * update tail positions + * draw positions + * clear first tail + * repeat + * */ + + window.m_board[snake.m_head.m_row][snake.m_head.m_col] = '*'; + window.m_board[fruit.m_row][fruit.m_col] = '*'; + window.draw_score(); + window.update_loop(FPS); + snake.m_head.clear(window.m_board); + snake.m_head.get_new_point(snake.m_direction); } - } - // int score = 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; + input_thread.join(); + return 0; } diff --git a/src/point.cpp b/src/point.cpp index afc1fbc..d2e1c61 100644 --- a/src/point.cpp +++ b/src/point.cpp @@ -9,26 +9,22 @@ void Point::clear(std::array, ROW> &arr) const { arr[m_row][m_col] = '.'; } -std::pair Point::get_new_point(int direction, int row, int col) { - 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; +void Point::get_new_point(int direction) { + if (direction == 1) { + if (m_row >= ROW - 1) m_row = 0; + else m_row++; + } else if (direction == 2) { + if (m_row <= 0) m_row = ROW - 1; + else m_row--; + } else if (direction == 3) { + if (m_col <= 0) m_col = COL - 1; + else m_col--; + } else if (direction == 4) { + if (m_col >= COL - 1) m_col = 0; + else m_col++; + } else { + return; } - 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 f3e6c09..82cc0ae 100644 --- a/src/snake.cpp +++ b/src/snake.cpp @@ -1,30 +1,36 @@ #include "snake.hpp" -Snake::Snake(int default_direction) { +Snake::Snake(int default_direction, int row, int col) { m_direction = default_direction; - m_tails_count = 0; + m_head = Point { row, col }; } void Snake::create_tail(int row, int col) { + // if(row >= ROW - 1) { + // row = 0; + // m_tails.push_back(Point {row, col}); + // } + // if(col >= COL - 1) { + // col = 0; + // m_tails.push_back(Point {row, col}); + // } m_tails.push_back(Point {row, col}); } -std::pair Snake::get_new_point(int tail_index, int direction, int row, int col) { - m_tails.at(tail_index) = Point {row, col}; +void Snake::get_new_tail_point(int tail_index, int direction) { if (direction == 1) { - if (row >= ROW - 1) row = 1; - else row++; + if (m_tails.at(tail_index).m_row >= ROW - 1) m_tails.at(tail_index).m_row = 0; + else ++m_tails.at(tail_index).m_row; } else if (direction == 2) { - if (row <= 0) row = ROW - 1; - else row--; + if (m_tails.at(tail_index).m_row <= 0) m_tails.at(tail_index).m_row = ROW - 1; + else --m_tails.at(tail_index).m_row; } else if (direction == 3) { - if (col <= 0) col = COL - 1; - else col--; + if (m_tails.at(tail_index).m_col <= 0) m_tails.at(tail_index).m_col = COL - 1; + else --m_tails.at(tail_index).m_row; } else if (direction == 4) { - if (col >= COL - 1) col = 1; - else col++; + if (m_tails.at(tail_index).m_col >= COL - 1) m_tails.at(tail_index).m_col = 0; + else ++m_tails.at(tail_index).m_row; } - return { row, col }; } /* snake head for instance is (3, 0) @@ -34,9 +40,3 @@ std::pair Snake::get_new_point(int tail_index, int direction, int row, * so if I change direction at (0, 0) from north to west then at (0, 0) there is a postion change saved and once a body hits that * switch direction */ - -/* Make a way to get key input - * Create a sense of direction - * save last position for head and head, save posisition for direction changes - * head class and body subclass maybe -*/ diff --git a/src/window.cpp b/src/window.cpp index 590d668..863dfe2 100644 --- a/src/window.cpp +++ b/src/window.cpp @@ -4,31 +4,35 @@ #include Window::Window() { - for(std::array &row : m_board) { - row.fill('.'); - } + for(std::array &row : m_board) { + row.fill('.'); + } + m_score = 0; } +void Window::draw_score() { + std::cout << "Score: " << m_score << std::endl; +} void Window::draw_window() { - for(std::array &row : m_board) { - for(char &col : row) { - std::cout << col << " "; + for(std::array &row : m_board) { + for(char &col : row) { + std::cout << col << " "; + } + std::cout << "\r\n"; } - std::cout << "\r\n"; - } - std::cout << std::flush; + std::cout << std::flush; } void Window::clear_window() { - std::cout<<"\033[H" << std::flush; + std::cout<<"\033[H" << std::flush; } void Window::tick(double frames_per_second) { - std::this_thread::sleep_for(std::chrono::milliseconds((int) (1000 / frames_per_second))); + std::this_thread::sleep_for(std::chrono::milliseconds((int) (1000 / frames_per_second))); } void Window::update_loop(int fps) { - clear_window(); - draw_window(); - tick(fps); + clear_window(); + draw_window(); + tick(fps); }