refactor: renamed snake to element and moved fruit to element. Removed a lot of unused functions and variables
This commit is contained in:
@@ -0,0 +1,78 @@
|
||||
#include "element.hpp"
|
||||
#include <cstdlib>
|
||||
#include <ctime>
|
||||
|
||||
Fruit::Fruit() {
|
||||
std::srand(std::time(0));
|
||||
m_point.set_new_point((rand() % 9), (rand() % 9), 0);
|
||||
}
|
||||
|
||||
void Fruit::get_new_pos(std::array<std::array<char, COL>, ROW> &board, Head &head, int &score) {
|
||||
if(m_point.m_row == head.m_head.m_row && m_point.m_col == head.m_head.m_col) {
|
||||
m_point.set_new_point((rand() % 9), (rand() % 9), 0);
|
||||
score++;
|
||||
}
|
||||
}
|
||||
|
||||
Head::Head() {
|
||||
m_head.set_new_point(5, 5, 2);
|
||||
}
|
||||
|
||||
void Head::get_new_pos() {
|
||||
if (m_head.m_direction == -1) {
|
||||
if (m_head.m_col <= 0) m_head.m_col = COL - 1;
|
||||
else m_head.m_col--;
|
||||
} else if (m_head.m_direction == 1) {
|
||||
if (m_head.m_col >= COL - 1) m_head.m_col = 0;
|
||||
else m_head.m_col++;
|
||||
} else if (m_head.m_direction == -2) {
|
||||
if (m_head.m_row <= 0) m_head.m_row = ROW - 1;
|
||||
else m_head.m_row--;
|
||||
} else if (m_head.m_direction == 2) {
|
||||
if (m_head.m_row >= ROW - 1) m_head.m_row = 0;
|
||||
else m_head.m_row++;
|
||||
}
|
||||
}
|
||||
|
||||
Body::Body() {
|
||||
m_score = 0;
|
||||
m_collision = false;
|
||||
}
|
||||
|
||||
void Body::collision(Head &head) {
|
||||
for(int i = 0; i < m_body.size(); i++) {
|
||||
if(m_body.at(i).m_tail_pos.m_row == head.m_head.m_row) {
|
||||
if(m_body.at(i).m_tail_pos.m_col == head.m_head.m_col) {
|
||||
m_collision = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Body::add_tail() {
|
||||
if(m_score >= 1) {
|
||||
m_score = 0;
|
||||
m_body.push_back(Tail{});
|
||||
}
|
||||
}
|
||||
|
||||
void Body::get_new_points(Head &head) {
|
||||
for (int i = 0; i < m_body.size(); i++) {
|
||||
m_body.front().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);
|
||||
if(i > 0) {
|
||||
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).m_tail_pos.set_new_point(
|
||||
m_body.at(i).m_previous_tail_pos.m_row,
|
||||
m_body.at(i).m_previous_tail_pos.m_col,
|
||||
m_body.at(i).m_previous_tail_pos.m_direction);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user