feat: (snake point main) finally got tail to work with a few bugs
This commit is contained in:
+1
-1
@@ -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 = 2;
|
constexpr uint16_t FPS = 5;
|
||||||
|
|
||||||
#endif // GLOBALS_HPP
|
#endif // GLOBALS_HPP
|
||||||
|
|||||||
@@ -15,6 +15,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;
|
||||||
|
void set_new_point(int x, int y, int dir);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // POINT_HPP
|
#endif // POINT_HPP
|
||||||
|
|||||||
+3
-1
@@ -33,8 +33,10 @@ struct Head {
|
|||||||
struct Tail {
|
struct Tail {
|
||||||
Tail(int follow_direction, int row, int col);
|
Tail(int follow_direction, int row, int col);
|
||||||
Point m_tail_pos;
|
Point m_tail_pos;
|
||||||
|
Point m_previous_tail_pos;
|
||||||
std::vector<Point> m_move_history;
|
std::vector<Point> m_move_history;
|
||||||
void get_new_point(Point &d);
|
// void set_point(Point &d);
|
||||||
|
void get_new_point();
|
||||||
};
|
};
|
||||||
|
|
||||||
struct Body {
|
struct Body {
|
||||||
|
|||||||
+23
-21
@@ -1,4 +1,5 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <string>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
@@ -17,42 +18,43 @@ int main (int argc, char *argv[]) {
|
|||||||
Body body;
|
Body body;
|
||||||
Head head { 2, 0, 0 };
|
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));
|
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
|
body.m_body.push_back(Tail{2, 0, 0});
|
||||||
* 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});
|
||||||
|
body.m_body.push_back(Tail{2, 0, 0});
|
||||||
body.m_body.push_back(Tail{2, 0, 0});
|
body.m_body.push_back(Tail{2, 0, 0});
|
||||||
body.m_body.push_back(Tail{2, 0, 0});
|
body.m_body.push_back(Tail{2, 0, 0});
|
||||||
while (running) {
|
while (running) {
|
||||||
// if(tail.m_tail_pos.m_direction != head.m_head.m_direction) {
|
for (int i = 0; i < body.m_body.size(); i++) {
|
||||||
// tail.m_move_history.push_back(Point{ head.m_head.m_row, head.m_head.m_col, head.m_head.m_direction });
|
if(i == 0) {
|
||||||
// tail.m_tail_pos.m_direction = head.
|
body.m_body.at(i).m_previous_tail_pos.set_new_point(head.m_head.m_row, head.m_head.m_col, head.m_head.m_direction);
|
||||||
// }
|
} else {
|
||||||
//
|
body.m_body.at(i).m_previous_tail_pos.set_new_point(body.m_body.at(i - 1).m_tail_pos.m_row, body.m_body.at(i - 1).m_tail_pos.m_col, body.m_body.at(i - 1).m_tail_pos.m_direction);
|
||||||
/* FIXME
|
}
|
||||||
* 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
|
}
|
||||||
* */
|
|
||||||
// body.m_body.at(0).m_tail_pos.m_row = head.m_head.m_row - 1;
|
|
||||||
// body.m_body.at(1).m_tail_pos.m_row = body.m_body.at(0).m_tail_pos.m_row - 1;
|
|
||||||
body.move(head);
|
|
||||||
|
|
||||||
|
for (int i = 0; i < body.m_body.size(); i++) {
|
||||||
|
body.m_body.at(i).get_new_point();
|
||||||
|
}
|
||||||
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);
|
body.draw(window.m_board);
|
||||||
window.draw_score();
|
window.draw_score();
|
||||||
for(int i = 0; i < body.m_body.size(); i++) {
|
// for(int i = 0; i < body.m_body.size(); i++) {
|
||||||
std::cout << body.m_body.at(i).m_tail_pos.to_string() << std::endl;
|
// std::cout << "Index: " + std::to_string(i) + " " + body.m_body.at(i).m_tail_pos.to_string() << std::endl;
|
||||||
}
|
// }
|
||||||
|
// std::cout << "Head: " + head.m_head.to_string() << std::endl;
|
||||||
window.update_loop(FPS);
|
window.update_loop(FPS);
|
||||||
body.clear(window.m_board);
|
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_head.m_direction);
|
head.get_new_point(head.m_head.m_direction);
|
||||||
}
|
}
|
||||||
//
|
|
||||||
// input_thread.join();
|
input_thread.join();
|
||||||
// return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,3 +8,9 @@ std::string Point::to_string() const {
|
|||||||
void Point::clear(std::array<std::array<char, COL>, ROW> &arr) const {
|
void Point::clear(std::array<std::array<char, COL>, ROW> &arr) const {
|
||||||
arr[m_row][m_col] = '.';
|
arr[m_row][m_col] = '.';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Point::set_new_point(int row, int col, int dir) {
|
||||||
|
m_row = row;
|
||||||
|
m_col = col;
|
||||||
|
m_direction = dir;
|
||||||
|
}
|
||||||
|
|||||||
+4
-64
@@ -5,7 +5,6 @@ Head::Head(int default_direction, int row, int col) {
|
|||||||
m_head = Point { row, col, default_direction };
|
m_head = Point { row, col, default_direction };
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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;
|
||||||
@@ -28,26 +27,10 @@ Tail::Tail(int follow_direction, int row, int col) {
|
|||||||
m_tail_pos = Point { row, col, follow_direction };
|
m_tail_pos = Point { row, col, follow_direction };
|
||||||
}
|
}
|
||||||
|
|
||||||
void Tail::get_new_point(Point &d) {
|
void Tail::get_new_point() {
|
||||||
if(m_tail_pos.m_row < 0) {
|
m_tail_pos.m_row = m_previous_tail_pos.m_row;
|
||||||
m_tail_pos.m_row += 10;
|
m_tail_pos.m_col = m_previous_tail_pos.m_col;
|
||||||
} else if(m_tail_pos.m_col < 0) {
|
m_tail_pos.m_direction = m_previous_tail_pos.m_direction;
|
||||||
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) {
|
void Body::clear(std::array<std::array<char, COL>, ROW> &arr) {
|
||||||
@@ -68,47 +51,4 @@ void Body::move(Head &head) {
|
|||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// 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) {
|
|
||||||
// // 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});
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// void Snake::get_new_tail_point(int tail_index, int direction) {
|
|
||||||
// if (direction == 1) {
|
|
||||||
// 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;
|
|
||||||
// } else if (direction == -1) {
|
|
||||||
// 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 == -2) {
|
|
||||||
// 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 == 2) {
|
|
||||||
// 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;
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|||||||
Reference in New Issue
Block a user