diff --git a/CMakeLists.txt b/CMakeLists.txt index b7df1a3..cd5f96e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -9,7 +9,6 @@ include_directories(include) set(SOURCES src/main.cpp src/element.cpp - src/input.cpp src/window.cpp ) add_executable(snake ${SOURCES}) diff --git a/include/element.hpp b/include/element.hpp index e7d4654..29ca748 100644 --- a/include/element.hpp +++ b/include/element.hpp @@ -4,6 +4,8 @@ #include #include "globals.hpp" +class Window; + enum Direction { UP, DOWN, LEFT, RIGHT }; @@ -19,19 +21,21 @@ struct Vector2i { class SnakeGame { public: SnakeGame(); - // ~SnakeGame(); - // Vector2i* buf[MAX_LEN] = {}; + ~SnakeGame(); + void run(); + +private: std::vector buf; bool running = true; enum Direction m_dir; + int m_score = 0; + Window* win; + void process_input(); void determine_index_zero_pos(); void move_tail_indicies(); void on_fruit_eaten(Vector2i (*board)[COL]); bool segment_overlap(); bool exit_summary(); -private: - int m_score = 0; - // int m_snake_size = 3; }; #endif // ELEMENT_HPP diff --git a/include/input.hpp b/include/input.hpp deleted file mode 100644 index 567f778..0000000 --- a/include/input.hpp +++ /dev/null @@ -1,7 +0,0 @@ -#ifndef INPUT_HPP -#define INPUT_HPP -#include "element.hpp" - -void get_input(enum Direction &dir, bool &r); - -#endif // INPUT_HPP diff --git a/include/window.hpp b/include/window.hpp index 914c1d8..0efd5e6 100644 --- a/include/window.hpp +++ b/include/window.hpp @@ -7,7 +7,7 @@ struct Window { Vector2i board[ROW][COL] = {}; void clear_display(); - void draw_display(int milliseconds); + void draw_display(); void update_display(std::vector &buf); }; diff --git a/src/element.cpp b/src/element.cpp index fe2728a..68c3060 100644 --- a/src/element.cpp +++ b/src/element.cpp @@ -2,8 +2,12 @@ #include "globals.hpp" #include #include +#include #include #include +#include +#include +#include void Vector2i::set(int _x, int _y) { y = _y; @@ -11,14 +15,49 @@ void Vector2i::set(int _x, int _y) { } SnakeGame::SnakeGame() { + std::srand(std::time(0)); + initscr(); + cbreak(); + noecho(); + curs_set(0); + nodelay(stdscr, true); buf.push_back(Vector2i{ COL / 2, ROW / 2, '*' }); buf.push_back(Vector2i{ buf.front().x, buf.front().y, '.' }); buf.push_back(Vector2i{ (rand() % COL), (rand() % ROW), 'o' }); + win = new Window(); } -// SnakeGame::~SnakeGame() { -// for(auto& p : buf) delete p; -// } +SnakeGame::~SnakeGame() { + delete win; + endwin(); +} + +void SnakeGame::run() { + while (exit_summary()) { + process_input(); + on_fruit_eaten(win->board); + determine_index_zero_pos(); + move_tail_indicies(); + + win->clear_display(); + win->update_display(buf); + win->draw_display(); + std::this_thread::sleep_for(std::chrono::milliseconds((int) (200))); + } +} + +void SnakeGame::process_input() { + int ch; + while((ch = getch()) != ERR) { + switch(ch) { + case 'w': if(m_dir != DOWN) m_dir = UP; break; + case 's': if(m_dir != UP) m_dir = DOWN; break; + case 'd': if(m_dir != LEFT) m_dir = RIGHT; break; + case 'a': if(m_dir != RIGHT) m_dir = LEFT; break; + case 'q': running = false; break; + } + } +} bool SnakeGame::exit_summary() { if(!running) { @@ -34,14 +73,13 @@ bool SnakeGame::exit_summary() { } void SnakeGame::determine_index_zero_pos() { - auto& head = buf[HEAD]; - buf.at(PREVIOUS_HEAD).set(head.x, head.y); + buf.at(PREVIOUS_HEAD).set(buf[HEAD].x, buf[HEAD].y); switch(m_dir) { - case LEFT: head.x = (head.x + COL - 1) % COL; break; - case RIGHT: head.x = (head.x + 1) % COL; break; - case UP: head.y = (head.y + ROW - 1) % ROW; break; - case DOWN: head.y = (head.y + 1) % ROW; break; + case LEFT: buf[HEAD].x = (buf[HEAD].x + COL - 1) % COL; break; + case RIGHT: buf[HEAD].x = (buf[HEAD].x + 1) % COL; break; + case UP: buf[HEAD].y = (buf[HEAD].y + ROW - 1) % ROW; break; + case DOWN: buf[HEAD].y = (buf[HEAD].y + 1) % ROW; break; } } diff --git a/src/input.cpp b/src/input.cpp deleted file mode 100644 index 98cdb94..0000000 --- a/src/input.cpp +++ /dev/null @@ -1,21 +0,0 @@ -#include "input.hpp" -#include -#include - -void get_input(enum Direction &dir, bool &running) { - uint16_t m_getch; - while(running) { - initscr(); - cbreak(); - noecho(); - - m_getch = getch(); - endwin(); - - if(m_getch == 65) if(dir != DOWN) dir = UP; - if(m_getch == 66) if(dir != UP) dir = DOWN; - if(m_getch == 67) if(dir != LEFT) dir = RIGHT; - if(m_getch == 68) if(dir != RIGHT) dir = LEFT; - if(m_getch == 113) running = false; - } -} diff --git a/src/main.cpp b/src/main.cpp index 27456b8..2385f80 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,25 +1,7 @@ -#include #include "element.hpp" -#include "window.hpp" -#include "input.hpp" int main () { - std::srand(std::time(0)); SnakeGame engine; - Window win; - - std::thread input_thread([&] { get_input(engine.m_dir, engine.running ); } ); - - while (engine.exit_summary()) { - engine.on_fruit_eaten(win.board); - engine.determine_index_zero_pos(); - engine.move_tail_indicies(); - - win.clear_display(); - win.update_display(engine.buf); - win.draw_display(200); - } - - input_thread.join(); + engine.run(); return 0; } diff --git a/src/window.cpp b/src/window.cpp index 825b685..b580d98 100644 --- a/src/window.cpp +++ b/src/window.cpp @@ -1,9 +1,7 @@ #include "window.hpp" #include "element.hpp" #include -#include #include -#include #include void Window::clear_display() { @@ -14,7 +12,7 @@ void Window::clear_display() { } } -void Window::draw_display(int milliseconds) { +void Window::draw_display() { std::cout << "\033[H" << std::flush; // Clear screen for(int i : std::ranges::views::iota(0, ROW)) { for(int j : std::ranges::views::iota(0, COL)) { @@ -23,7 +21,6 @@ void Window::draw_display(int milliseconds) { } std::cout << "\r\n"; } - std::this_thread::sleep_for(std::chrono::milliseconds((int) (milliseconds))); // 1000 / fps; 200ms = 5fps } void Window::update_display(std::vector &buf) {