wip
This commit is contained in:
@@ -4,6 +4,7 @@
|
|||||||
#include <vector>
|
#include <vector>
|
||||||
#include "globals.hpp"
|
#include "globals.hpp"
|
||||||
|
|
||||||
|
static int _score = 0;
|
||||||
|
|
||||||
struct Point {
|
struct Point {
|
||||||
int m_row;
|
int m_row;
|
||||||
@@ -18,6 +19,7 @@ struct Point {
|
|||||||
};
|
};
|
||||||
|
|
||||||
void get_new_pos(Point* (*board)[COL], Point** segments);
|
void get_new_pos(Point* (*board)[COL], Point** segments);
|
||||||
|
void check_new_tail(Point** segments);
|
||||||
|
|
||||||
struct Head {
|
struct Head {
|
||||||
Head() : m_direction(2) {};
|
Head() : m_direction(2) {};
|
||||||
|
|||||||
+1
-1
@@ -4,7 +4,7 @@
|
|||||||
struct Input {
|
struct Input {
|
||||||
int m_current_key = 0;
|
int m_current_key = 0;
|
||||||
int m_getch = 0;
|
int m_getch = 0;
|
||||||
void get_input(int &d, bool &r);
|
void get_input(int &d, bool &r, int &s);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // INPUT_HPP
|
#endif // INPUT_HPP
|
||||||
|
|||||||
+28
-4
@@ -1,10 +1,9 @@
|
|||||||
// Used ptr to take a copy of the head, move head move tail at end of array to previous head position from copy and make the old tail a nullptr
|
// Used ptr to take a copy of the head, move head move tail at end of array to previous head position from copy and make the old tail a nullptr
|
||||||
#include "element.hpp"
|
#include "element.hpp"
|
||||||
|
#include <iostream>
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
#include <ctime>
|
#include <ctime>
|
||||||
|
|
||||||
int _score = 0;
|
|
||||||
|
|
||||||
void Point::set_new_point(int row, int col) {
|
void Point::set_new_point(int row, int col) {
|
||||||
m_row = row;
|
m_row = row;
|
||||||
m_col = col;
|
m_col = col;
|
||||||
@@ -63,8 +62,33 @@ bool Body::check_collision(Head &head) {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Body::check_new_tail() {
|
void check_new_tail(Point** segments) {
|
||||||
if(_score > m_body.size()) m_body.push_back(Tail{});
|
int size = 0;
|
||||||
|
std::cout << size << "\n";
|
||||||
|
for(int i = 1; i < ROW * COL; i++) {
|
||||||
|
if(segments[i] == nullptr) continue;
|
||||||
|
size++;
|
||||||
|
}
|
||||||
|
if(_score + 1 > size) {
|
||||||
|
for(int i = 0; i < ROW * COL; i++) {
|
||||||
|
if(segments[i] != nullptr) continue;
|
||||||
|
if(segments[i] == nullptr) {
|
||||||
|
if(segments[i - 1]->m_direction == -1) {
|
||||||
|
segments[i] = new Point { segments[i - 1]->m_row, segments[i - 1]->m_col + 1, '@', segments[i - 1]->m_direction };
|
||||||
|
}
|
||||||
|
else if(segments[i - 1]->m_direction == 1) {
|
||||||
|
segments[i] = new Point { segments[i - 1]->m_row, segments[i - 1]->m_col - 1, '@', segments[i - 1]->m_direction };
|
||||||
|
}
|
||||||
|
else if(segments[i - 1]->m_direction == -2) {
|
||||||
|
segments[i] = new Point { segments[i - 1]->m_row + 1, segments[i - 1]->m_col, '@', segments[i - 1]->m_direction };
|
||||||
|
}
|
||||||
|
else if(segments[i - 1]->m_direction == 2) {
|
||||||
|
segments[i] = new Point { segments[i - 1]->m_row - 1, segments[i - 1]->m_col, '@', segments[i - 1]->m_direction };
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Body::get_new_pos(Head &head) {
|
void Body::get_new_pos(Head &head) {
|
||||||
|
|||||||
+10
-5
@@ -1,7 +1,8 @@
|
|||||||
#include "input.hpp"
|
#include "input.hpp"
|
||||||
#include <ncurses.h>
|
#include <ncurses.h>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
void Input::get_input(int &direction, bool &running) {
|
void Input::get_input(int &direction, bool &running, int &score) {
|
||||||
while(running) {
|
while(running) {
|
||||||
initscr();
|
initscr();
|
||||||
cbreak();
|
cbreak();
|
||||||
@@ -10,16 +11,20 @@ void Input::get_input(int &direction, bool &running) {
|
|||||||
m_getch = getch();
|
m_getch = getch();
|
||||||
endwin();
|
endwin();
|
||||||
switch (m_getch) {
|
switch (m_getch) {
|
||||||
case 65:
|
case 43: // +
|
||||||
|
score++;
|
||||||
|
m_getch = 0;
|
||||||
|
break;
|
||||||
|
case 65: // Up arrow
|
||||||
if(m_current_key != 2) direction = m_current_key = -2;
|
if(m_current_key != 2) direction = m_current_key = -2;
|
||||||
break;
|
break;
|
||||||
case 66:
|
case 66: // Down arrow
|
||||||
if(m_current_key != -2) direction = m_current_key = 2;
|
if(m_current_key != -2) direction = m_current_key = 2;
|
||||||
break;
|
break;
|
||||||
case 67:
|
case 67: // Right arrow
|
||||||
if(m_current_key != -1) direction = m_current_key = 1;
|
if(m_current_key != -1) direction = m_current_key = 1;
|
||||||
break;
|
break;
|
||||||
case 68:
|
case 68: // Left arrow
|
||||||
if(m_current_key != 1) direction = m_current_key = -1;
|
if(m_current_key != 1) direction = m_current_key = -1;
|
||||||
break;
|
break;
|
||||||
case 113:
|
case 113:
|
||||||
|
|||||||
+5
-2
@@ -15,15 +15,17 @@ int main () {
|
|||||||
|
|
||||||
Point* segments[ROW * COL] = {};
|
Point* segments[ROW * COL] = {};
|
||||||
segments[0] = new Point { 5, 5, '*', 1 };
|
segments[0] = new Point { 5, 5, '*', 1 };
|
||||||
// segments[1] = new Point { 6, 5, '@', 0 };
|
segments[1] = new Point { 6, 5, '@', 0 };
|
||||||
|
|
||||||
std::thread input_thread(
|
std::thread input_thread(
|
||||||
&Input::get_input, &input,
|
&Input::get_input, &input,
|
||||||
std::ref(segments[0]->m_direction),
|
std::ref(segments[0]->m_direction),
|
||||||
std::ref(running));
|
std::ref(running),
|
||||||
|
std::ref(_score));
|
||||||
|
|
||||||
while (running) {
|
while (running) {
|
||||||
get_new_pos(window.m_board, segments);
|
get_new_pos(window.m_board, segments);
|
||||||
|
check_new_tail(segments);
|
||||||
// body.check_new_tail();
|
// body.check_new_tail();
|
||||||
// if(body.m_body.size() >= (ROW * COL)) {
|
// if(body.m_body.size() >= (ROW * COL)) {
|
||||||
// std::cout << "You win! Score: " + std::to_string(body.m_body.size()) << std::endl;
|
// std::cout << "You win! Score: " + std::to_string(body.m_body.size()) << std::endl;
|
||||||
@@ -37,6 +39,7 @@ int main () {
|
|||||||
// body.get_new_pos(head);
|
// body.get_new_pos(head);
|
||||||
//
|
//
|
||||||
// head.m_previous_head.set_new_point(head.m_head);
|
// head.m_previous_head.set_new_point(head.m_head);
|
||||||
|
std::cout << _score << "\n";
|
||||||
window.draw_display();
|
window.draw_display();
|
||||||
window.update_display(segments);
|
window.update_display(segments);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user