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
+2 -2
View File
@@ -6,6 +6,7 @@
Fruit::Fruit() {
std::srand(std::time(0));
create_point();
m_total_eaten = 0;
}
void Fruit::create_point() {
@@ -14,11 +15,10 @@ void Fruit::create_point() {
m_col = m_point.m_col;
}
void Fruit::reset(std::array<std::array<char, COL>, ROW> &arr, int row, int col, int &score, int &count) {
void Fruit::reset(std::array<std::array<char, COL>, ROW> &arr, int row, int col, int &score) {
if(m_row == row && m_col == col) {
m_point.clear(arr);
create_point();
score++;
count++;
}
}
+18 -23
View File
@@ -8,30 +8,25 @@ void Input::get_input(int &d, bool &r) {
cbreak();
noecho();
m_current_key = getch();
m_getch = getch();
endwin();
switch (m_current_key) {
case 'D':
case 'a':
d = -1;
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;
// TODO
// m_current_key should be the m_getch and then as long as the opposite doesnt equal m_current_key
if(m_getch == 'a' || m_getch == 'D') {
d = -1;
m_current_key = -1;
} else if(m_getch == 'd' || m_getch == 'C') {
d = 1;
m_current_key = 1;
} else if(m_getch == 's' || m_getch == 'B') {
d = 2;
m_current_key = m_getch;
} else if(m_getch == 'w' || m_getch == 'A') {
d = -2;
m_current_key = m_getch;
} else if(m_getch == 'q') {
r = false;
running = false;
}
}
}
+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);
+27 -1
View File
@@ -3,6 +3,7 @@
Head::Head(int default_direction, int row, int col) {
m_head = Point { row, col, default_direction };
m_previous_head.set_new_point(m_previous_head.m_row = 0, m_previous_head.m_col = 0, m_previous_head.m_direction = 2);
}
void Head::get_new_point(int direction) {
@@ -23,11 +24,17 @@ void Head::get_new_point(int direction) {
}
}
void Head::get_previous_point() {
m_previous_head.m_row = m_head.m_row;
m_previous_head.m_col = m_head.m_col;
m_previous_head.m_direction = m_head.m_direction;
}
Tail::Tail(int follow_direction, int row, int col) {
m_tail_pos = Point { row, col, follow_direction };
}
void Tail::get_new_point() {
void Tail::get_previous_point() {
m_tail_pos.m_row = m_previous_tail_pos.m_row;
m_tail_pos.m_col = m_previous_tail_pos.m_col;
m_tail_pos.m_direction = m_previous_tail_pos.m_direction;
@@ -52,3 +59,22 @@ void Body::move(Head &head) {
}
}
}
void Body::get_new_points(Head &head) {
for (int i = 0; i < m_body.size(); i++) {
if(i == 0) {
m_body.at(i).m_previous_tail_pos.set_new_point(
head.m_previous_head.m_row,
head.m_previous_head.m_col,
head.m_previous_head.m_direction);
} else {
m_body.at(i).m_previous_tail_pos.set_new_point(
m_body.at(i - 1).m_tail_pos.m_row,
m_body.at(i - 1).m_tail_pos.m_col,
m_body.at(i - 1).m_tail_pos.m_direction);
}
}
for (int i = 0; i < m_body.size(); i++) {
m_body.at(i).get_previous_point();
}
}
+11
View File
@@ -8,6 +8,8 @@ Window::Window() {
row.fill('.');
}
m_score = 0;
m_reset = 0;
m_previous_reset = 0;
}
void Window::draw_score() {
@@ -23,6 +25,15 @@ void Window::draw_window() {
std::cout << std::flush;
}
void Window::update_score(Fruit &fruit, Body &body) {
if(m_score % 1 == 0 && m_score >= 1) {
fruit.m_total_eaten++;
m_score = 0;
body.m_body.push_back(Tail{2, 0, 0});
}
}
void Window::clear_window() {
std::cout<<"\033[H" << std::flush;
}