fix: (tail) fixed where tail index 0 is on top of the head

This commit is contained in:
DarthMooMancer
2025-08-28 20:14:35 -04:00
parent ac23c873c3
commit 157933c122
9 changed files with 86 additions and 84 deletions
+8 -26
View File
@@ -1,9 +1,7 @@
#include <iostream>
#include <string>
#include <thread>
#include <cstdlib>
#include <unistd.h>
#include <vector>
#include "point.hpp"
#include "fruit.hpp"
#include "snake.hpp"
@@ -18,37 +16,21 @@ int main (int argc, char *argv[]) {
Body body;
Head head { 2, 0, 0 };
bool running = true;
std::thread input_thread(&Input::get_input, &input, std::ref(head.m_head.m_direction), std::ref(running));
int count = 0;
std::thread input_thread(
&Input::get_input, &input,
std::ref(head.m_head.m_direction),
std::ref(running));
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) {
for (int i = 0; i < body.m_body.size(); i++) {
if(i == 0) {
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);
}
}
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);
window.update_score(fruit, body);
body.get_new_points(head);
head.get_previous_point();
fruit.reset(window.m_board, head.m_head.m_row, head.m_head.m_col, window.m_score);
window.m_board[head.m_head.m_row][head.m_head.m_col] = '*';
window.m_board[fruit.m_row][fruit.m_col] = 'X';
body.draw(window.m_board);
window.draw_score();
// for(int i = 0; i < body.m_body.size(); i++) {
// 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);
body.clear(window.m_board);
head.m_head.clear(window.m_board);