This commit is contained in:
DarthMooMancer
2025-08-21 09:17:37 -04:00
parent a128048a8f
commit 5e887e3688
12 changed files with 134 additions and 133 deletions
+3 -3
View File
@@ -4,12 +4,12 @@
#include "point.hpp" #include "point.hpp"
struct Fruit { struct Fruit {
Fruit();
int unsigned m_row; int unsigned m_row;
int unsigned m_col; int unsigned m_col;
Point m_point; Point m_point;
Fruit();
void create_point(); void create_point();
void reset(std::array<std::array<char, COL>, ROW> &arr, int row, int col, int &score); void reset(std::array<std::array<char, COL>, ROW> &arr, int row, int col, int &score, int &count);
}; };
#endif // FRUIT_HPP #endif // FRUIT_HPP
+1
View File
@@ -5,5 +5,6 @@
constexpr uint16_t ROW = 10; constexpr uint16_t ROW = 10;
constexpr uint16_t COL = 10; constexpr uint16_t COL = 10;
constexpr uint16_t FPS = 5;
#endif // GLOBALS_HPP #endif // GLOBALS_HPP
+1
View File
@@ -3,6 +3,7 @@
struct Input { struct Input {
char m_current_key = ' '; char m_current_key = ' ';
char m_old_key = ' ';
void get_input(); void get_input();
}; };
+1 -1
View File
@@ -12,7 +12,7 @@ struct Point {
std::string to_string() const; std::string to_string() const;
void clear(std::array<std::array<char, COL>, ROW> &arr) const; void clear(std::array<std::array<char, COL>, ROW> &arr) const;
std::pair<int, int> get_new_point(int direction, int row, int col); void get_new_point(int direction);
}; };
#endif // POINT_HPP #endif // POINT_HPP
+5 -6
View File
@@ -5,14 +5,13 @@
#include "point.hpp" #include "point.hpp"
struct Snake { struct Snake {
Snake(int default_direction); Snake(int default_direction, int row, int col);
void create_tail(int row, int col); Point m_head;
std::pair<int, int> get_new_point(int tail_index, int direction, int row, int col);
void update_tails_count();
std::vector<Point> m_tails; std::vector<Point> m_tails;
int m_direction; // 1: Down, 2: Up, 3: Left, 4: Right 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 #endif
+2
View File
@@ -5,9 +5,11 @@
struct Window { struct Window {
std::array<std::array<char, COL>, ROW> m_board; std::array<std::array<char, COL>, ROW> m_board;
int m_score;
// Initializes board with . for each value // Initializes board with . for each value
Window(); Window();
void draw_score();
void draw_window(); void draw_window();
void clear_window(); void clear_window();
void tick(double frames_per_second); void tick(double frames_per_second);
+3 -1
View File
@@ -5,6 +5,7 @@
Fruit::Fruit() { Fruit::Fruit() {
std::srand(std::time(0)); std::srand(std::time(0));
create_point();
} }
void Fruit::create_point() { void Fruit::create_point() {
@@ -13,10 +14,11 @@ void Fruit::create_point() {
m_col = m_point.m_col; m_col = m_point.m_col;
} }
void Fruit::reset(std::array<std::array<char, COL>, ROW> &arr, int row, int col, int &score) { void Fruit::reset(std::array<std::array<char, COL>, ROW> &arr, int row, int col, int &score, int &count) {
if(m_row == row && m_col == col) { if(m_row == row && m_col == col) {
m_point.clear(arr); m_point.clear(arr);
create_point(); create_point();
score++; score++;
count++;
} }
} }
+2 -1
View File
@@ -7,9 +7,10 @@ void Input::get_input() {
cbreak(); cbreak();
noecho(); noecho();
m_old_key = m_current_key;
m_current_key = getch(); m_current_key = getch();
endwin(); endwin();
if(m_current_key == 'q') { if (m_current_key == 'q') {
break; break;
} }
} }
+48 -53
View File
@@ -2,7 +2,6 @@
#include <thread> #include <thread>
#include <cstdlib> #include <cstdlib>
#include <unistd.h> #include <unistd.h>
#include <vector>
#include "point.hpp" #include "point.hpp"
#include "fruit.hpp" #include "fruit.hpp"
#include "snake.hpp" #include "snake.hpp"
@@ -11,65 +10,61 @@
#include "input.hpp" #include "input.hpp"
int main (int argc, char *argv[]) { int main (int argc, char *argv[]) {
Snake snake {1}; Snake snake { 1, 0, 0 };
Window window; Window window;
Input input; Input input;
Fruit fruit; Fruit fruit;
int x = 5; std::thread input_thread(&Input::get_input, &input);
for(int i = 0; i < x; i++) { bool running = true;
snake.create_tail(i, 0);
}
int row = 0, col = 0;
int count = 0; int count = 0;
while(count < 2) { while (running) {
count++; switch (input.m_current_key) {
for(int i = 0; i < snake.m_tails.size(); i++) { case 's':
std::tie(row, col) = snake.get_new_point(i, 1, row, col); snake.m_direction = 1;
std::cout << snake.m_tails.at(i).to_string() << std::endl; 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 score = 0; fruit.reset(window.m_board, snake.m_head.m_row, snake.m_head.m_col, window.m_score, count);
// int fps = 5; if (window.m_score % 2 == 0 && count > 0) {
// bool running = true; if (snake.m_direction == 1) {
// int r = snake.m_head.m_row;
// Point previous {row,col}; // Creating initial snake head position if (r + 1 > ROW - 1) {
// fruit.create_point(); // Creating initial fruit object r = snake.m_head.m_row;
// std::array<std::array<char, COL>, ROW> &board = window.m_board; }
// std::thread input_thread(&Input::get_input, &input); snake.create_tail(snake.m_head.m_row + r + 1, snake.m_head.m_col);
// while(running) { ++r;
// switch (input.m_current_key) { }
// case 's': count--;
// 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();
/* 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);
}
input_thread.join();
return 0; return 0;
} }
+15 -19
View File
@@ -9,26 +9,22 @@ void Point::clear(std::array<std::array<char, COL>, ROW> &arr) const {
arr[m_row][m_col] = '.'; arr[m_row][m_col] = '.';
} }
std::pair<int, int> Point::get_new_point(int direction, int row, int col) { void Point::get_new_point(int direction) {
switch (direction) { if (direction == 1) {
case 1: if (m_row >= ROW - 1) m_row = 0;
row++; else m_row++;
if (row >= ROW) row = 0; } else if (direction == 2) {
break; if (m_row <= 0) m_row = ROW - 1;
case 2: else m_row--;
row--; } else if (direction == 3) {
if (row < 0) row = ROW - 1; if (m_col <= 0) m_col = COL - 1;
break; else m_col--;
case 3: } else if (direction == 4) {
col--; if (m_col >= COL - 1) m_col = 0;
if (col < 0) col = COL - 1; else m_col++;
break; } else {
case 4: return;
col++;
if (col >= COL) col = 0;
break;
} }
return {row,col};
} }
/* Create save_point to take old_direction, new_direction, row and col /* Create save_point to take old_direction, new_direction, row and col
+19 -19
View File
@@ -1,30 +1,36 @@
#include "snake.hpp" #include "snake.hpp"
Snake::Snake(int default_direction) { Snake::Snake(int default_direction, int row, int col) {
m_direction = default_direction; m_direction = default_direction;
m_tails_count = 0; m_head = Point { row, col };
} }
void Snake::create_tail(int row, int 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}); m_tails.push_back(Point {row, col});
} }
std::pair<int, int> Snake::get_new_point(int tail_index, int direction, int row, int col) { void Snake::get_new_tail_point(int tail_index, int direction) {
m_tails.at(tail_index) = Point {row, col};
if (direction == 1) { if (direction == 1) {
if (row >= ROW - 1) row = 1; if (m_tails.at(tail_index).m_row >= ROW - 1) m_tails.at(tail_index).m_row = 0;
else row++; else ++m_tails.at(tail_index).m_row;
} else if (direction == 2) { } else if (direction == 2) {
if (row <= 0) row = ROW - 1; if (m_tails.at(tail_index).m_row <= 0) m_tails.at(tail_index).m_row = ROW - 1;
else row--; else --m_tails.at(tail_index).m_row;
} else if (direction == 3) { } else if (direction == 3) {
if (col <= 0) col = COL - 1; if (m_tails.at(tail_index).m_col <= 0) m_tails.at(tail_index).m_col = COL - 1;
else col--; else --m_tails.at(tail_index).m_row;
} else if (direction == 4) { } else if (direction == 4) {
if (col >= COL - 1) col = 1; if (m_tails.at(tail_index).m_col >= COL - 1) m_tails.at(tail_index).m_col = 0;
else col++; else ++m_tails.at(tail_index).m_row;
} }
return { row, col };
} }
/* snake head for instance is (3, 0) /* snake head for instance is (3, 0)
@@ -34,9 +40,3 @@ std::pair<int, int> 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 * 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 * 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
*/
+4
View File
@@ -7,8 +7,12 @@ Window::Window() {
for(std::array<char, COL> &row : m_board) { for(std::array<char, COL> &row : m_board) {
row.fill('.'); row.fill('.');
} }
m_score = 0;
} }
void Window::draw_score() {
std::cout << "Score: " << m_score << std::endl;
}
void Window::draw_window() { void Window::draw_window() {
for(std::array<char, COL> &row : m_board) { for(std::array<char, COL> &row : m_board) {
for(char &col : row) { for(char &col : row) {