feat: (snake.cpp snake.hpp) adding tail movement (broken)

This commit is contained in:
DarthMooMancer
2025-08-26 20:21:01 -04:00
parent 1bb5ecad33
commit b2c90565bc
7 changed files with 131 additions and 41 deletions
+1 -1
View File
@@ -5,6 +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; constexpr uint16_t FPS = 2;
#endif // GLOBALS_HPP #endif // GLOBALS_HPP
+1 -2
View File
@@ -3,8 +3,7 @@
struct Input { struct Input {
char m_current_key = ' '; char m_current_key = ' ';
char m_old_key = ' '; void get_input(int &d, bool &r);
void get_input();
}; };
#endif // INPUT_HPP #endif // INPUT_HPP
+10 -6
View File
@@ -23,21 +23,25 @@
* separate the snake and tail from each other * separate the snake and tail from each other
* */ * */
struct Head { struct Head {
Head(int default_direction, int row, int col); Head(int default_direction, int row, int col);
Point m_head; Point m_head;
std::vector<Point> m_tails;
// -1: Left, 1: Right, 2: Down, -2: Up
int m_direction;
// Adds a new tail to m_tails at { row, col }
void create_tail(int row, int col);
void get_new_head_point(int tail_index, int direction);
void get_new_point(int direction); void get_new_point(int direction);
}; };
struct Tail { struct Tail {
Tail(int follow_direction, int row, int col);
Point m_tail_pos; Point m_tail_pos;
std::vector<Point> m_move_history; std::vector<Point> m_move_history;
void get_new_point(Point &d);
};
struct Body {
std::vector<Tail> m_body;
void clear(std::array<std::array<char, COL>, ROW> &arr);
void draw(std::array<std::array<char, COL>, ROW> &arr);
void move(Head &head);
}; };
#endif #endif
+24 -4
View File
@@ -1,17 +1,37 @@
#include "input.hpp" #include "input.hpp"
#include <ncurses.h> #include <ncurses.h>
void Input::get_input() { void Input::get_input(int &d, bool &r) {
while(true) { bool running = true;
while(running) {
initscr(); initscr();
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') { switch (m_current_key) {
case 'D':
case 'a':
d = -1;
break; break;
case 'C':
case 'd':
d = 1;
break;
case 'B':
case 's':
d = 2;
break;
case 'A':
case 'w':
d = -2;
break;
case 'q':
r = false;
running = false;
break;
} }
} }
} }
+29 -24
View File
@@ -2,6 +2,7 @@
#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"
@@ -10,44 +11,48 @@
#include "input.hpp" #include "input.hpp"
int main (int argc, char *argv[]) { int main (int argc, char *argv[]) {
Head head { 2, 0, 0 };
Window window; Window window;
Input input; Input input;
Fruit fruit; Fruit fruit;
Body body;
std::thread input_thread(&Input::get_input, &input); Head head { 2, 0, 0 };
bool running = true; bool running = true;
// std::thread input_thread(&Input::get_input, &input, std::ref(head.m_head.m_direction), std::ref(running));
int count = 0; int count = 0;
/* TODO
* Each tail is not meant to follow the head. Each tail should follow the tail or head in front of it. Tail 6 should follow tail 5 and not the head.
*/
body.m_body.push_back(Tail{2, 0, 0});
body.m_body.push_back(Tail{2, 0, 0});
while (running) { while (running) {
switch (input.m_current_key) { // if(tail.m_tail_pos.m_direction != head.m_head.m_direction) {
case 'a': // tail.m_move_history.push_back(Point{ head.m_head.m_row, head.m_head.m_col, head.m_head.m_direction });
head.m_direction = -1; // tail.m_tail_pos.m_direction = head.
break; // }
case 'd': //
head.m_direction = 1; /* FIXME
break; * It teleports the tail to the head because i am basing off of the head. It needs to be isolated but just follow the head
case 's': * */
head.m_direction = 2; // body.m_body.at(0).m_tail_pos.m_row = head.m_head.m_row - 1;
break; // body.m_body.at(1).m_tail_pos.m_row = body.m_body.at(0).m_tail_pos.m_row - 1;
case 'w': body.move(head);
head.m_direction = -2;
break;
case 'q':
running = false;
break;
}
fruit.reset(window.m_board, head.m_head.m_row, head.m_head.m_col, window.m_score, count); fruit.reset(window.m_board, head.m_head.m_row, head.m_head.m_col, window.m_score, count);
window.m_board[head.m_head.m_row][head.m_head.m_col] = '*'; window.m_board[head.m_head.m_row][head.m_head.m_col] = '*';
window.m_board[fruit.m_row][fruit.m_col] = 'X'; window.m_board[fruit.m_row][fruit.m_col] = 'X';
body.draw(window.m_board);
window.draw_score(); window.draw_score();
for(int i = 0; i < body.m_body.size(); i++) {
std::cout << body.m_body.at(i).m_tail_pos.to_string() << std::endl;
}
window.update_loop(FPS); window.update_loop(FPS);
body.clear(window.m_board);
head.m_head.clear(window.m_board); head.m_head.clear(window.m_board);
head.get_new_point(head.m_direction); head.get_new_point(head.m_head.m_direction);
} }
//
input_thread.join(); // input_thread.join();
return 0; // return 0;
} }
+1 -1
View File
@@ -2,7 +2,7 @@
#include "point.hpp" #include "point.hpp"
std::string Point::to_string() const { std::string Point::to_string() const {
return "(" + std::to_string(m_row) + ", " + std::to_string(m_col) + ")"; return "(" + std::to_string(m_row) + ", " + std::to_string(m_col) + "), " + std::to_string(m_direction);
} }
void Point::clear(std::array<std::array<char, COL>, ROW> &arr) const { void Point::clear(std::array<std::array<char, COL>, ROW> &arr) const {
+64 -2
View File
@@ -1,10 +1,11 @@
#include "snake.hpp" #include "snake.hpp"
#include <iostream>
Head::Head(int default_direction, int row, int col) { Head::Head(int default_direction, int row, int col) {
m_direction = default_direction; m_head = Point { row, col, default_direction };
m_head = Point { row, col };
} }
// Use the head movement for the tail and make the tail start -1 to the last tail but move them separately
void Head::get_new_point(int direction) { void Head::get_new_point(int direction) {
if (direction == -1) { if (direction == -1) {
if (m_head.m_col <= 0) m_head.m_col = COL - 1; if (m_head.m_col <= 0) m_head.m_col = COL - 1;
@@ -23,6 +24,67 @@ void Head::get_new_point(int direction) {
} }
} }
Tail::Tail(int follow_direction, int row, int col) {
m_tail_pos = Point { row, col, follow_direction };
}
void Tail::get_new_point(Point &d) {
if(m_tail_pos.m_row < 0) {
m_tail_pos.m_row += 10;
} else if(m_tail_pos.m_col < 0) {
m_tail_pos.m_col += 10;
} else if (d.m_direction == -1 && d.m_col + 1) {
if (m_tail_pos.m_col <= 0) m_tail_pos.m_col = COL - 1;
else m_tail_pos.m_col--;
} else if (d.m_direction == 1 && d.m_col - 1) {
if (m_tail_pos.m_col >= COL - 1) m_tail_pos.m_col = 0;
else m_tail_pos.m_col++;
} else if (d.m_direction == -2 && d.m_row + 1) {
if (m_tail_pos.m_row <= 0) m_tail_pos.m_row = ROW - 1;
else m_tail_pos.m_row--;
} else if (d.m_direction == 2 && d.m_row - 1) {
if (m_tail_pos.m_row >= ROW - 1) m_tail_pos.m_row = 0;
else m_tail_pos.m_row++;
} else {
return;
}
}
void Body::clear(std::array<std::array<char, COL>, ROW> &arr) {
for(int i = 0; i < m_body.size(); i++) {
m_body.at(i).m_tail_pos.clear(arr);
}
}
void Body::draw(std::array<std::array<char, COL>, ROW> &arr) {
for(int i = 0; i < m_body.size(); i++) {
arr[m_body.at(i).m_tail_pos.m_row][m_body.at(i).m_tail_pos.m_col] = '0';
}
}
void Body::move(Head &head) {
for (int i = 0; i < m_body.size(); i++) {
if(i == 0) {
}
}
// if(m_body.at(0).m_tail_pos.m_row < 0) {
// m_body.at(0).m_tail_pos.m_row = 9;
// }
// if(m_body.at(1).m_tail_pos.m_row < 0) {
// m_body.at(1).m_tail_pos.m_row = 8;
// }
// m_body.at(0).m_tail_pos.m_row = head.m_head.m_row - 1;
// m_body.at(1).m_tail_pos.m_row = m_body.at(0).m_tail_pos.m_row - 1;
// m_body.at(2).m_tail_pos.m_row = m_body.at(1).m_tail_pos.m_row - 1;
// m_body.at(3).m_tail_pos.m_row = m_body.at(2).m_tail_pos.m_row - 1;
// m_body.at(4).m_tail_pos.m_row = m_body.at(3).m_tail_pos.m_row - 1;
// for(int i = 0; i < m_body.size() - 1; i++) {
// if(i == 0) {
// }
// }
}
// void Snake::create_tail(int row, int col) { // void Snake::create_tail(int row, int col) {
// // if(row >= ROW - 1) { // // if(row >= ROW - 1) {
// // row = 0; // // row = 0;