refactor: renamed snake to element and moved fruit to element. Removed a lot of unused functions and variables
This commit is contained in:
+1
-2
@@ -7,11 +7,10 @@ include_directories(include)
|
|||||||
|
|
||||||
set(SOURCES
|
set(SOURCES
|
||||||
src/main.cpp
|
src/main.cpp
|
||||||
src/snake.cpp
|
src/element.cpp
|
||||||
src/input.cpp
|
src/input.cpp
|
||||||
src/window.cpp
|
src/window.cpp
|
||||||
src/point.cpp
|
src/point.cpp
|
||||||
src/fruit.cpp
|
|
||||||
|
|
||||||
)
|
)
|
||||||
add_executable(snake ${SOURCES})
|
add_executable(snake ${SOURCES})
|
||||||
|
|||||||
@@ -0,0 +1,36 @@
|
|||||||
|
#ifndef ELEMENT_HPP
|
||||||
|
#define ELEMENT_HPP
|
||||||
|
|
||||||
|
#include <vector>
|
||||||
|
#include "point.hpp"
|
||||||
|
|
||||||
|
struct Head {
|
||||||
|
Head();
|
||||||
|
Point m_head;
|
||||||
|
Point m_previous_head;
|
||||||
|
void get_new_pos();
|
||||||
|
};
|
||||||
|
|
||||||
|
struct Tail {
|
||||||
|
Point m_tail_pos;
|
||||||
|
Point m_previous_tail_pos;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct Body {
|
||||||
|
Body();
|
||||||
|
std::vector<Tail> m_body;
|
||||||
|
int m_score;
|
||||||
|
bool m_collision;
|
||||||
|
void clear(std::array<std::array<char, COL>, ROW> &arr);
|
||||||
|
void add_tail();
|
||||||
|
void get_new_points(Head &head);
|
||||||
|
void collision(Head &head);
|
||||||
|
};
|
||||||
|
|
||||||
|
struct Fruit {
|
||||||
|
Fruit();
|
||||||
|
Point m_point;
|
||||||
|
void get_new_pos(std::array<std::array<char, COL>, ROW> &board, Head &head, int &score);
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // ELEMENT_HPP
|
||||||
@@ -1,12 +0,0 @@
|
|||||||
#ifndef FRUIT_HPP
|
|
||||||
#define FRUIT_HPP
|
|
||||||
|
|
||||||
#include "point.hpp"
|
|
||||||
|
|
||||||
struct Fruit {
|
|
||||||
Fruit();
|
|
||||||
Point m_point;
|
|
||||||
void reset(std::array<std::array<char, COL>, ROW> &board, int row, int col, int &score);
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif // FRUIT_HPP
|
|
||||||
@@ -5,6 +5,5 @@
|
|||||||
|
|
||||||
constexpr uint16_t ROW = 11;
|
constexpr uint16_t ROW = 11;
|
||||||
constexpr uint16_t COL = 11;
|
constexpr uint16_t COL = 11;
|
||||||
constexpr uint16_t FPS = 5;
|
|
||||||
|
|
||||||
#endif // GLOBALS_HPP
|
#endif // GLOBALS_HPP
|
||||||
|
|||||||
+1
-1
@@ -4,7 +4,7 @@
|
|||||||
struct Input {
|
struct Input {
|
||||||
int m_current_key = 0;
|
int m_current_key = 0;
|
||||||
char m_getch = ' ';
|
char m_getch = ' ';
|
||||||
void get_input(int &d, bool &c, bool &r);
|
void get_input(int &d, bool &r);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // INPUT_HPP
|
#endif // INPUT_HPP
|
||||||
|
|||||||
+2
-4
@@ -2,7 +2,6 @@
|
|||||||
#define POINT_HPP
|
#define POINT_HPP
|
||||||
|
|
||||||
#include <array>
|
#include <array>
|
||||||
#include <string>
|
|
||||||
#include "globals.hpp"
|
#include "globals.hpp"
|
||||||
|
|
||||||
struct Point {
|
struct Point {
|
||||||
@@ -13,9 +12,8 @@ struct Point {
|
|||||||
Point(int x, int y) : m_row(x), m_col(y) {}
|
Point(int x, int y) : m_row(x), m_col(y) {}
|
||||||
Point(int x, int y, int dir) : m_row(x), m_col(y), m_direction(dir) {}
|
Point(int x, int y, int dir) : m_row(x), m_col(y), m_direction(dir) {}
|
||||||
|
|
||||||
std::string to_string() const;
|
void clear(std::array<std::array<char, COL>, ROW> &board) const;
|
||||||
void clear(std::array<std::array<char, COL>, ROW> &arr) const;
|
void set_new_point(int row, int col, int dir);
|
||||||
void set_new_point(int x, int y, int dir);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // POINT_HPP
|
#endif // POINT_HPP
|
||||||
|
|||||||
@@ -1,33 +0,0 @@
|
|||||||
#ifndef SNAKE_HPP
|
|
||||||
#define SNAKE_HPP
|
|
||||||
|
|
||||||
#include <vector>
|
|
||||||
#include "point.hpp"
|
|
||||||
|
|
||||||
struct Head {
|
|
||||||
Head();
|
|
||||||
Head(int default_direction, int row, int col);
|
|
||||||
Point m_head;
|
|
||||||
Point m_previous_head;
|
|
||||||
void get_new_point(int direction);
|
|
||||||
void get_previous_point();
|
|
||||||
};
|
|
||||||
|
|
||||||
struct Tail {
|
|
||||||
Tail(int follow_direction, int row, int col);
|
|
||||||
Point m_tail_pos;
|
|
||||||
Point m_previous_tail_pos;
|
|
||||||
std::vector<Point> m_move_history;
|
|
||||||
void get_previous_point();
|
|
||||||
};
|
|
||||||
|
|
||||||
struct Body {
|
|
||||||
std::vector<Tail> m_body;
|
|
||||||
void clear(std::array<std::array<char, COL>, ROW> &arr);
|
|
||||||
void draw(std::array<std::array<char, COL>, ROW> &arr);
|
|
||||||
void move(Head &head);
|
|
||||||
void get_new_points(Head &head);
|
|
||||||
void collision(Head &head, bool &running);
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif
|
|
||||||
+2
-12
@@ -2,22 +2,12 @@
|
|||||||
#define WINDOW_HPP
|
#define WINDOW_HPP
|
||||||
#include <array>
|
#include <array>
|
||||||
#include "globals.hpp"
|
#include "globals.hpp"
|
||||||
#include "fruit.hpp"
|
#include "element.hpp"
|
||||||
#include "snake.hpp"
|
|
||||||
|
|
||||||
struct Window {
|
struct Window {
|
||||||
Window();
|
Window();
|
||||||
std::array<std::array<char, COL>, ROW> m_board;
|
std::array<std::array<char, COL>, ROW> m_board;
|
||||||
int m_score;
|
void update_display(Fruit &fruit, Head &head, Body &body);
|
||||||
int m_reset;
|
|
||||||
int m_previous_reset;
|
|
||||||
|
|
||||||
void draw_window();
|
|
||||||
void tick(double frames_per_second);
|
|
||||||
void kill(bool &running);
|
|
||||||
void update_score(Fruit &fruit, Body &body);
|
|
||||||
void update_display(int fps);
|
|
||||||
void check_exit(bool &running, Body &body, bool &collision);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // WINDOW_HPP
|
#endif // WINDOW_HPP
|
||||||
|
|||||||
@@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,19 +0,0 @@
|
|||||||
#include <cstdlib>
|
|
||||||
#include <ctime>
|
|
||||||
#include "fruit.hpp"
|
|
||||||
#include "globals.hpp"
|
|
||||||
|
|
||||||
Fruit::Fruit() {
|
|
||||||
std::srand(std::time(0));
|
|
||||||
m_point.m_row = (rand() % 9);
|
|
||||||
m_point.m_col = (rand() % 9);
|
|
||||||
}
|
|
||||||
|
|
||||||
void Fruit::reset(std::array<std::array<char, COL>, ROW> &board, int row, int col, int &score) {
|
|
||||||
if(m_point.m_row == row && m_point.m_col == col) {
|
|
||||||
m_point.clear(board);
|
|
||||||
m_point.m_row = (rand() % 9);
|
|
||||||
m_point.m_col = (rand() % 9);
|
|
||||||
score++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
+7
-9
@@ -1,9 +1,8 @@
|
|||||||
#include "input.hpp"
|
#include "input.hpp"
|
||||||
#include <ncurses.h>
|
#include <ncurses.h>
|
||||||
|
|
||||||
void Input::get_input(int &d, bool &c, bool &r) {
|
void Input::get_input(int &direction, bool &running) {
|
||||||
bool running = true;
|
while(running) {
|
||||||
while(running && !(c)) {
|
|
||||||
initscr();
|
initscr();
|
||||||
cbreak();
|
cbreak();
|
||||||
noecho();
|
noecho();
|
||||||
@@ -11,16 +10,15 @@ void Input::get_input(int &d, bool &c, bool &r) {
|
|||||||
m_getch = getch();
|
m_getch = getch();
|
||||||
endwin();
|
endwin();
|
||||||
if(m_current_key != 1 && (m_getch == 'a' || m_getch == 'D')) {
|
if(m_current_key != 1 && (m_getch == 'a' || m_getch == 'D')) {
|
||||||
d = m_current_key = -1;
|
direction = m_current_key = -1;
|
||||||
} else if(m_current_key != - 1 && (m_getch == 'd' || m_getch == 'C')) {
|
} else if(m_current_key != - 1 && (m_getch == 'd' || m_getch == 'C')) {
|
||||||
d = m_current_key = 1;
|
direction = m_current_key = 1;
|
||||||
m_current_key = 1;
|
|
||||||
} else if(m_current_key != -2 && (m_getch == 's' || m_getch == 'B')) {
|
} else if(m_current_key != -2 && (m_getch == 's' || m_getch == 'B')) {
|
||||||
d = m_current_key = 2;
|
direction = m_current_key = 2;
|
||||||
} else if(m_current_key != 2 && (m_getch == 'w' || m_getch == 'A')) {
|
} else if(m_current_key != 2 && (m_getch == 'w' || m_getch == 'A')) {
|
||||||
d = m_current_key = -2;
|
direction = m_current_key = -2;
|
||||||
} else if(m_getch == 'q') {
|
} else if(m_getch == 'q') {
|
||||||
r = running = false;
|
running = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+15
-17
@@ -1,7 +1,6 @@
|
|||||||
#include <thread>
|
#include <thread>
|
||||||
#include "point.hpp"
|
#include <iostream>
|
||||||
#include "fruit.hpp"
|
#include "element.hpp"
|
||||||
#include "snake.hpp"
|
|
||||||
#include "window.hpp"
|
#include "window.hpp"
|
||||||
#include "globals.hpp"
|
#include "globals.hpp"
|
||||||
#include "input.hpp"
|
#include "input.hpp"
|
||||||
@@ -13,28 +12,27 @@ int main () {
|
|||||||
Body body;
|
Body body;
|
||||||
Head head;
|
Head head;
|
||||||
bool running = true;
|
bool running = true;
|
||||||
bool collision = false;
|
|
||||||
std::thread input_thread(
|
std::thread input_thread(
|
||||||
&Input::get_input, &input,
|
&Input::get_input, &input,
|
||||||
std::ref(head.m_head.m_direction),
|
std::ref(head.m_head.m_direction),
|
||||||
std::ref(collision),
|
|
||||||
std::ref(running));
|
std::ref(running));
|
||||||
|
|
||||||
while (running) {
|
while (running) {
|
||||||
body.collision(head, collision);
|
body.collision(head);
|
||||||
window.update_score(fruit, body);
|
body.add_tail();
|
||||||
window.check_exit(running, body, collision);
|
if(body.m_body.size() >= (ROW * COL)) {
|
||||||
head.get_new_point(head.m_head.m_direction);
|
std::cout << "You win! Score: " + std::to_string(body.m_body.size()) << std::endl;
|
||||||
|
running = false;
|
||||||
|
} else if(body.m_collision) {
|
||||||
|
std::cout << "You died! Score: " + std::to_string(body.m_body.size()) << std::endl;
|
||||||
|
running = false;
|
||||||
|
}
|
||||||
|
head.get_new_pos();
|
||||||
body.get_new_points(head);
|
body.get_new_points(head);
|
||||||
head.get_previous_point();
|
head.m_previous_head.set_new_point(head.m_head.m_row, head.m_head.m_col, head.m_head.m_direction);
|
||||||
|
|
||||||
fruit.reset(window.m_board, head.m_head.m_row, head.m_head.m_col, window.m_score);
|
fruit.get_new_pos(window.m_board, head, body.m_score);
|
||||||
window.m_board[head.m_head.m_row][head.m_head.m_col] = '*';
|
window.update_display(fruit, head, body);
|
||||||
window.m_board[fruit.m_point.m_row][fruit.m_point.m_col] = 'X';
|
|
||||||
body.draw(window.m_board);
|
|
||||||
window.update_display(FPS);
|
|
||||||
body.clear(window.m_board);
|
|
||||||
head.m_head.clear(window.m_board);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
input_thread.join();
|
input_thread.join();
|
||||||
|
|||||||
+2
-7
@@ -1,12 +1,7 @@
|
|||||||
#include <string>
|
|
||||||
#include "point.hpp"
|
#include "point.hpp"
|
||||||
|
|
||||||
std::string Point::to_string() const {
|
void Point::clear(std::array<std::array<char, COL>, ROW> &board) const {
|
||||||
return "(" + std::to_string(m_row) + ", " + std::to_string(m_col) + "), " + std::to_string(m_direction);
|
board[m_row][m_col] = '.';
|
||||||
}
|
|
||||||
|
|
||||||
void Point::clear(std::array<std::array<char, COL>, ROW> &arr) const {
|
|
||||||
arr[m_row][m_col] = '.';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Point::set_new_point(int row, int col, int dir) {
|
void Point::set_new_point(int row, int col, int dir) {
|
||||||
|
|||||||
@@ -1,96 +0,0 @@
|
|||||||
#include "snake.hpp"
|
|
||||||
|
|
||||||
Head::Head() {
|
|
||||||
m_head = Point { 5, 5, 2 };
|
|
||||||
m_previous_head.set_new_point(m_previous_head.m_row = 0, m_previous_head.m_col = 0, m_previous_head.m_direction = 2);
|
|
||||||
}
|
|
||||||
|
|
||||||
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) {
|
|
||||||
if (direction == -1) {
|
|
||||||
if (m_head.m_col <= 0) m_head.m_col = COL - 1;
|
|
||||||
else m_head.m_col--;
|
|
||||||
} else if (direction == 1) {
|
|
||||||
if (m_head.m_col >= COL - 1) m_head.m_col = 0;
|
|
||||||
else m_head.m_col++;
|
|
||||||
} else if (direction == -2) {
|
|
||||||
if (m_head.m_row <= 0) m_head.m_row = ROW - 1;
|
|
||||||
else m_head.m_row--;
|
|
||||||
} else if (direction == 2) {
|
|
||||||
if (m_head.m_row >= ROW - 1) m_head.m_row = 0;
|
|
||||||
else m_head.m_row++;
|
|
||||||
} else {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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_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;
|
|
||||||
}
|
|
||||||
|
|
||||||
void Body::collision(Head &head, bool &collision) {
|
|
||||||
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) {
|
|
||||||
collision = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void Body::clear(std::array<std::array<char, COL>, ROW> &arr) {
|
|
||||||
for(int i = 0; i < m_body.size(); i++) {
|
|
||||||
m_body.at(i).m_tail_pos.clear(arr);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void Body::draw(std::array<std::array<char, COL>, ROW> &arr) {
|
|
||||||
for(int i = 0; i < m_body.size(); i++) {
|
|
||||||
if(i < m_body.size()) {
|
|
||||||
arr[m_body.at(i).m_tail_pos.m_row][m_body.at(i).m_tail_pos.m_col] = '0';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void Body::move(Head &head) {
|
|
||||||
for (int i = 0; i < m_body.size(); i++) {
|
|
||||||
if(i == 0) {
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
+15
-33
@@ -1,51 +1,33 @@
|
|||||||
#include "window.hpp"
|
#include "window.hpp"
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
#include <string>
|
|
||||||
#include <thread>
|
#include <thread>
|
||||||
|
|
||||||
Window::Window() {
|
Window::Window() {
|
||||||
for(std::array<char, COL> &row : m_board) {
|
for(std::array<char, COL> &row : m_board) { row.fill('.'); }
|
||||||
row.fill('.');
|
|
||||||
}
|
|
||||||
m_score = 0;
|
|
||||||
m_reset = 0;
|
|
||||||
m_previous_reset = 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Window::check_exit(bool &running, Body &body, bool &collision) {
|
void Window::update_display(Fruit &fruit, Head &head, Body &body) {
|
||||||
if(body.m_body.size() >= (ROW * COL)) {
|
// Update symbols of positions
|
||||||
std::cout << "You win! Score: " + std::to_string(body.m_body.size()) << std::endl;
|
m_board[head.m_head.m_row][head.m_head.m_col] = '*';
|
||||||
kill(running);
|
m_board[fruit.m_point.m_row][fruit.m_point.m_col] = 'X';
|
||||||
} else if(collision) {
|
for(int i = 0; i < body.m_body.size(); i++) {
|
||||||
std::cout << "You died! Score: " + std::to_string(body.m_body.size()) << std::endl;
|
m_board[body.m_body.at(i).m_tail_pos.m_row][body.m_body.at(i).m_tail_pos.m_col] = '0';
|
||||||
kill(running);
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
void Window::draw_window() {
|
std::cout << "\033[H" << std::flush; // Clear screen
|
||||||
|
// Draw Screen
|
||||||
for(std::array<char, COL> &row : m_board) {
|
for(std::array<char, COL> &row : m_board) {
|
||||||
for(char &col : row) {
|
for(char &col : row) {
|
||||||
std::cout << col << " ";
|
std::cout << col << " ";
|
||||||
}
|
}
|
||||||
std::cout << "\r\n";
|
std::cout << "\r\n";
|
||||||
}
|
}
|
||||||
std::cout << std::flush;
|
|
||||||
}
|
|
||||||
|
|
||||||
void Window::update_score(Fruit &fruit, Body &body) {
|
|
||||||
if(m_score >= 1) {
|
|
||||||
m_score = 0;
|
|
||||||
body.m_body.push_back(Tail{2, 0, 0});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void Window::kill(bool &running) {
|
|
||||||
running = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
void Window::update_display(int fps) {
|
|
||||||
std::cout<<"\033[H" << std::flush;
|
|
||||||
draw_window();
|
|
||||||
std::this_thread::sleep_for(std::chrono::milliseconds((int) (200))); // 1000 / fps; 200ms = 5fps
|
std::this_thread::sleep_for(std::chrono::milliseconds((int) (200))); // 1000 / fps; 200ms = 5fps
|
||||||
|
// Clear old spaces
|
||||||
|
for(int i = 0; i < body.m_body.size(); i++) {
|
||||||
|
body.m_body.at(i).m_tail_pos.clear(m_board);
|
||||||
|
}
|
||||||
|
head.m_head.clear(m_board);
|
||||||
|
fruit.m_point.clear(m_board);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user