snake can collide and removed some magic numbers
This commit is contained in:
+22
-6
@@ -1,6 +1,11 @@
|
||||
#include "element.hpp"
|
||||
#include <cstdlib>
|
||||
#include <ctime>
|
||||
int _score = 0;
|
||||
|
||||
int get_score() {
|
||||
return _score;
|
||||
}
|
||||
|
||||
void Point::set_new_point(int row, int col) {
|
||||
m_row = row;
|
||||
@@ -16,16 +21,16 @@ void determine_index_zero_pos(Point* (*board)[COL], Point** segments) {
|
||||
segments[1]->set_new_point(segments[0]->m_row, segments[0]->m_col);
|
||||
}
|
||||
|
||||
if (segments[0]->m_direction == -1) {
|
||||
if (segments[0]->m_dir == LEFT) {
|
||||
if (new_point.m_col <= 0) new_point.m_col = COL - 1;
|
||||
else new_point.m_col--;
|
||||
} else if (segments[0]->m_direction == 1) {
|
||||
} else if (segments[0]->m_dir == RIGHT) {
|
||||
if (new_point.m_col >= COL - 1) new_point.m_col = 0;
|
||||
else new_point.m_col++;
|
||||
} else if (segments[0]->m_direction == -2) {
|
||||
} else if (segments[0]->m_dir == UP) {
|
||||
if (new_point.m_row <= 0) new_point.m_row = ROW - 1;
|
||||
else new_point.m_row--;
|
||||
} else if (segments[0]->m_direction == 2) {
|
||||
} else if (segments[0]->m_dir == DOWN) {
|
||||
if (new_point.m_row >= ROW - 1) new_point.m_row = 0;
|
||||
else new_point.m_row++;
|
||||
}
|
||||
@@ -50,13 +55,13 @@ void move_tail_indicies(Point** segments) {
|
||||
}
|
||||
}
|
||||
|
||||
void _append_tail(Point *(*board)[10], Point** segments) {
|
||||
void _append_tail(Point *(*board)[COL], Point** segments) {
|
||||
for(int i = (ROW * COL) - 1; i >= 3; i--) {
|
||||
segments[i] = segments[i - 1];
|
||||
}
|
||||
Point* old_tail = segments[4];
|
||||
if(old_tail != nullptr) {
|
||||
segments[3] = new Point { old_tail->m_row, old_tail->m_col, '@', -1 };
|
||||
segments[3] = new Point { old_tail->m_row, old_tail->m_col, '@' };
|
||||
} else {
|
||||
segments[3] = new Point {};
|
||||
}
|
||||
@@ -71,5 +76,16 @@ void on_fruit_eaten(Point* (*board)[COL], Point** segments) {
|
||||
} while (board[row][col] != nullptr);
|
||||
segments[2]->set_new_point(row, col);
|
||||
_append_tail(board, segments);
|
||||
_score++;
|
||||
}
|
||||
}
|
||||
|
||||
bool segment_overlap(Point** segments) {
|
||||
for(int i = (ROW * COL) - 1; i > 3; i--) {
|
||||
if(segments[i] == nullptr) continue;
|
||||
if(segments[i]->m_row == segments[0]->m_row && segments[i]->m_col == segments[0]->m_col) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
+5
-5
@@ -1,7 +1,7 @@
|
||||
#include "input.hpp"
|
||||
#include <ncurses.h>
|
||||
|
||||
void Input::get_input(int &direction, bool &running) {
|
||||
void Input::get_input(enum Direction &dir, bool &running) {
|
||||
while(running) {
|
||||
initscr();
|
||||
cbreak();
|
||||
@@ -11,16 +11,16 @@ void Input::get_input(int &direction, bool &running) {
|
||||
endwin();
|
||||
switch (m_getch) {
|
||||
case 65: // Up arrow
|
||||
if(m_current_key != 2) direction = m_current_key = -2;
|
||||
if(m_current_key != DOWN) dir = m_current_key = UP;
|
||||
break;
|
||||
case 66: // Down arrow
|
||||
if(m_current_key != -2) direction = m_current_key = 2;
|
||||
if(m_current_key != UP) dir = m_current_key = DOWN;
|
||||
break;
|
||||
case 67: // Right arrow
|
||||
if(m_current_key != -1) direction = m_current_key = 1;
|
||||
if(m_current_key != LEFT) dir = m_current_key = RIGHT;
|
||||
break;
|
||||
case 68: // Left arrow
|
||||
if(m_current_key != 1) direction = m_current_key = -1;
|
||||
if(m_current_key != RIGHT) dir = m_current_key = LEFT;
|
||||
break;
|
||||
case 113:
|
||||
running = false;
|
||||
|
||||
+14
-11
@@ -11,26 +11,29 @@ int main () {
|
||||
Input input;
|
||||
bool running = true;
|
||||
|
||||
Point* segments[ROW * COL] = {};
|
||||
segments[0] = new Point { 5, 5, '*', 1 }; // Head
|
||||
segments[2] = new Point { (rand() % ROW), (rand() % COL), 'o', 0 }; // Fruit
|
||||
Point* segments[(ROW * COL) + 3] = {}; // 2 for head, 1 for fruit, rest for tail
|
||||
segments[0] = new Point { 5, 5, '*' }; // Head
|
||||
segments[2] = new Point { (rand() % ROW), (rand() % COL), 'o' }; // Fruit
|
||||
|
||||
std::thread input_thread(
|
||||
&Input::get_input, &input,
|
||||
std::ref(segments[0]->m_direction),
|
||||
std::ref(segments[0]->m_dir),
|
||||
std::ref(running));
|
||||
|
||||
while (running) {
|
||||
on_fruit_eaten(window.m_board, segments);
|
||||
determine_index_zero_pos(window.m_board, segments);
|
||||
move_tail_indicies(segments);
|
||||
// if(body.m_body.size() >= (ROW * COL)) {
|
||||
// std::cout << "You win! Score: " + std::to_string(body.m_body.size()) << std::endl;
|
||||
// running = false;
|
||||
// } else if(body.check_collision(head)) {
|
||||
// std::cout << "You died! Score: " + std::to_string(body.m_body.size()) << std::endl;
|
||||
// running = false;
|
||||
// }
|
||||
|
||||
for(int i = (ROW * COL) - 1; i > 3; i--) {
|
||||
if(segments[i] == nullptr) break;
|
||||
std::cout << "You win! Score: " << get_score() << std::endl;
|
||||
running = false;
|
||||
}
|
||||
if(segment_overlap(segments)) {
|
||||
std::cout << "You died! Score: " << get_score() << std::endl;
|
||||
running = false;
|
||||
}
|
||||
window.clear_display();
|
||||
window.update_display(segments);
|
||||
window.draw_display(200);
|
||||
|
||||
@@ -9,7 +9,6 @@ void Window::clear_display() {
|
||||
m_board[i][j] = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void Window::draw_display(int milliseconds) {
|
||||
|
||||
Reference in New Issue
Block a user