wip
This commit is contained in:
+3
-3
@@ -4,12 +4,12 @@
|
|||||||
#include "point.hpp"
|
#include "point.hpp"
|
||||||
|
|
||||||
struct Fruit {
|
struct Fruit {
|
||||||
|
Fruit();
|
||||||
int unsigned m_row;
|
int unsigned m_row;
|
||||||
int unsigned m_col;
|
int unsigned m_col;
|
||||||
|
|
||||||
Point m_point;
|
Point m_point;
|
||||||
Fruit();
|
|
||||||
void create_point();
|
void create_point();
|
||||||
void reset(std::array<std::array<char, COL>, ROW> &arr, int row, int col, int &score);
|
void reset(std::array<std::array<char, COL>, ROW> &arr, int row, int col, int &score, int &count);
|
||||||
};
|
};
|
||||||
#endif // FRUIT_HPP
|
#endif // FRUIT_HPP
|
||||||
|
|||||||
@@ -5,5 +5,6 @@
|
|||||||
|
|
||||||
constexpr uint16_t ROW = 10;
|
constexpr uint16_t ROW = 10;
|
||||||
constexpr uint16_t COL = 10;
|
constexpr uint16_t COL = 10;
|
||||||
|
constexpr uint16_t FPS = 5;
|
||||||
|
|
||||||
#endif // GLOBALS_HPP
|
#endif // GLOBALS_HPP
|
||||||
|
|||||||
+3
-2
@@ -2,8 +2,9 @@
|
|||||||
#define INPUT_HPP
|
#define INPUT_HPP
|
||||||
|
|
||||||
struct Input {
|
struct Input {
|
||||||
char m_current_key = ' ';
|
char m_current_key = ' ';
|
||||||
void get_input();
|
char m_old_key = ' ';
|
||||||
|
void get_input();
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // INPUT_HPP
|
#endif // INPUT_HPP
|
||||||
|
|||||||
+1
-1
@@ -12,7 +12,7 @@ struct Point {
|
|||||||
|
|
||||||
std::string to_string() const;
|
std::string to_string() const;
|
||||||
void clear(std::array<std::array<char, COL>, ROW> &arr) const;
|
void clear(std::array<std::array<char, COL>, ROW> &arr) const;
|
||||||
std::pair<int, int> get_new_point(int direction, int row, int col);
|
void get_new_point(int direction);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // POINT_HPP
|
#endif // POINT_HPP
|
||||||
|
|||||||
+6
-7
@@ -5,14 +5,13 @@
|
|||||||
#include "point.hpp"
|
#include "point.hpp"
|
||||||
|
|
||||||
struct Snake {
|
struct Snake {
|
||||||
Snake(int default_direction);
|
Snake(int default_direction, int row, int col);
|
||||||
void create_tail(int row, int col);
|
Point m_head;
|
||||||
std::pair<int, int> get_new_point(int tail_index, int direction, int row, int col);
|
std::vector<Point> m_tails;
|
||||||
void update_tails_count();
|
|
||||||
|
|
||||||
std::vector<Point> m_tails;
|
|
||||||
int m_direction; // 1: Down, 2: Up, 3: Left, 4: Right
|
int m_direction; // 1: Down, 2: Up, 3: Left, 4: Right
|
||||||
int m_tails_count;
|
|
||||||
|
void create_tail(int row, int col); // Adds a new tail to m_tails at { row, col }
|
||||||
|
void get_new_tail_point(int tail_index, int direction);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -5,9 +5,11 @@
|
|||||||
|
|
||||||
struct Window {
|
struct Window {
|
||||||
std::array<std::array<char, COL>, ROW> m_board;
|
std::array<std::array<char, COL>, ROW> m_board;
|
||||||
|
int m_score;
|
||||||
// Initializes board with . for each value
|
// Initializes board with . for each value
|
||||||
Window();
|
Window();
|
||||||
|
|
||||||
|
void draw_score();
|
||||||
void draw_window();
|
void draw_window();
|
||||||
void clear_window();
|
void clear_window();
|
||||||
void tick(double frames_per_second);
|
void tick(double frames_per_second);
|
||||||
|
|||||||
+3
-1
@@ -5,6 +5,7 @@
|
|||||||
|
|
||||||
Fruit::Fruit() {
|
Fruit::Fruit() {
|
||||||
std::srand(std::time(0));
|
std::srand(std::time(0));
|
||||||
|
create_point();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Fruit::create_point() {
|
void Fruit::create_point() {
|
||||||
@@ -13,10 +14,11 @@ void Fruit::create_point() {
|
|||||||
m_col = m_point.m_col;
|
m_col = m_point.m_col;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Fruit::reset(std::array<std::array<char, COL>, ROW> &arr, int row, int col, int &score) {
|
void Fruit::reset(std::array<std::array<char, COL>, ROW> &arr, int row, int col, int &score, int &count) {
|
||||||
if(m_row == row && m_col == col) {
|
if(m_row == row && m_col == col) {
|
||||||
m_point.clear(arr);
|
m_point.clear(arr);
|
||||||
create_point();
|
create_point();
|
||||||
score++;
|
score++;
|
||||||
|
count++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+10
-9
@@ -2,15 +2,16 @@
|
|||||||
#include <ncurses.h>
|
#include <ncurses.h>
|
||||||
|
|
||||||
void Input::get_input() {
|
void Input::get_input() {
|
||||||
while(true) {
|
while(true) {
|
||||||
initscr();
|
initscr();
|
||||||
cbreak();
|
cbreak();
|
||||||
noecho();
|
noecho();
|
||||||
|
|
||||||
m_current_key = getch();
|
m_old_key = m_current_key;
|
||||||
endwin();
|
m_current_key = getch();
|
||||||
if(m_current_key == 'q') {
|
endwin();
|
||||||
break;
|
if (m_current_key == 'q') {
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
+53
-58
@@ -2,7 +2,6 @@
|
|||||||
#include <thread>
|
#include <thread>
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <vector>
|
|
||||||
#include "point.hpp"
|
#include "point.hpp"
|
||||||
#include "fruit.hpp"
|
#include "fruit.hpp"
|
||||||
#include "snake.hpp"
|
#include "snake.hpp"
|
||||||
@@ -11,65 +10,61 @@
|
|||||||
#include "input.hpp"
|
#include "input.hpp"
|
||||||
|
|
||||||
int main (int argc, char *argv[]) {
|
int main (int argc, char *argv[]) {
|
||||||
Snake snake {1};
|
Snake snake { 1, 0, 0 };
|
||||||
Window window;
|
Window window;
|
||||||
Input input;
|
Input input;
|
||||||
Fruit fruit;
|
Fruit fruit;
|
||||||
|
|
||||||
int x = 5;
|
std::thread input_thread(&Input::get_input, &input);
|
||||||
for(int i = 0; i < x; i++) {
|
bool running = true;
|
||||||
snake.create_tail(i, 0);
|
int count = 0;
|
||||||
}
|
while (running) {
|
||||||
|
switch (input.m_current_key) {
|
||||||
|
case 's':
|
||||||
|
snake.m_direction = 1;
|
||||||
|
break;
|
||||||
|
case 'w':
|
||||||
|
snake.m_direction = 2;
|
||||||
|
break;
|
||||||
|
case 'a':
|
||||||
|
snake.m_direction = 3;
|
||||||
|
break;
|
||||||
|
case 'd':
|
||||||
|
snake.m_direction = 4;
|
||||||
|
break;
|
||||||
|
case 'q':
|
||||||
|
running = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
int row = 0, col = 0;
|
fruit.reset(window.m_board, snake.m_head.m_row, snake.m_head.m_col, window.m_score, count);
|
||||||
int count = 0;
|
if (window.m_score % 2 == 0 && count > 0) {
|
||||||
while(count < 2) {
|
if (snake.m_direction == 1) {
|
||||||
count++;
|
int r = snake.m_head.m_row;
|
||||||
for(int i = 0; i < snake.m_tails.size(); i++) {
|
if (r + 1 > ROW - 1) {
|
||||||
std::tie(row, col) = snake.get_new_point(i, 1, row, col);
|
r = snake.m_head.m_row;
|
||||||
std::cout << snake.m_tails.at(i).to_string() << std::endl;
|
}
|
||||||
|
snake.create_tail(snake.m_head.m_row + r + 1, snake.m_head.m_col);
|
||||||
|
++r;
|
||||||
|
}
|
||||||
|
count--;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* TODO
|
||||||
|
* update tail positions
|
||||||
|
* draw positions
|
||||||
|
* clear first tail
|
||||||
|
* repeat
|
||||||
|
* */
|
||||||
|
|
||||||
|
window.m_board[snake.m_head.m_row][snake.m_head.m_col] = '*';
|
||||||
|
window.m_board[fruit.m_row][fruit.m_col] = '*';
|
||||||
|
window.draw_score();
|
||||||
|
window.update_loop(FPS);
|
||||||
|
snake.m_head.clear(window.m_board);
|
||||||
|
snake.m_head.get_new_point(snake.m_direction);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// int score = 0;
|
input_thread.join();
|
||||||
// int fps = 5;
|
return 0;
|
||||||
// bool running = true;
|
|
||||||
//
|
|
||||||
// Point previous {row,col}; // Creating initial snake head position
|
|
||||||
// fruit.create_point(); // Creating initial fruit object
|
|
||||||
// std::array<std::array<char, COL>, ROW> &board = window.m_board;
|
|
||||||
// std::thread input_thread(&Input::get_input, &input);
|
|
||||||
// while(running) {
|
|
||||||
// switch (input.m_current_key) {
|
|
||||||
// case 's':
|
|
||||||
// snake.m_direction = 1;
|
|
||||||
// break;
|
|
||||||
// case 'w':
|
|
||||||
// snake.m_direction = 2;
|
|
||||||
// break;
|
|
||||||
// case 'a':
|
|
||||||
// snake.m_direction = 3;
|
|
||||||
// break;
|
|
||||||
// case 'd':
|
|
||||||
// snake.m_direction = 4;
|
|
||||||
// break;
|
|
||||||
// case 'q':
|
|
||||||
// running = false;
|
|
||||||
// break;
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// fruit.reset(board, row, col, score);
|
|
||||||
// std::tie(row, col) = previous.get_point(snake.m_direction, row, col);
|
|
||||||
//
|
|
||||||
// board[row][col] = '*';
|
|
||||||
// board[fruit.m_row][fruit.m_col] = '*';
|
|
||||||
// std::cout << "Score: " << score << std::endl;
|
|
||||||
// previous.clear(board); // Clear old previous point
|
|
||||||
// previous = Point {row, col}; // Set new previous point
|
|
||||||
// window.update_loop(fps); // Update loop to clear board, tick and redisplay board
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// input_thread.join();
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|||||||
+15
-19
@@ -9,26 +9,22 @@ void Point::clear(std::array<std::array<char, COL>, ROW> &arr) const {
|
|||||||
arr[m_row][m_col] = '.';
|
arr[m_row][m_col] = '.';
|
||||||
}
|
}
|
||||||
|
|
||||||
std::pair<int, int> Point::get_new_point(int direction, int row, int col) {
|
void Point::get_new_point(int direction) {
|
||||||
switch (direction) {
|
if (direction == 1) {
|
||||||
case 1:
|
if (m_row >= ROW - 1) m_row = 0;
|
||||||
row++;
|
else m_row++;
|
||||||
if (row >= ROW) row = 0;
|
} else if (direction == 2) {
|
||||||
break;
|
if (m_row <= 0) m_row = ROW - 1;
|
||||||
case 2:
|
else m_row--;
|
||||||
row--;
|
} else if (direction == 3) {
|
||||||
if (row < 0) row = ROW - 1;
|
if (m_col <= 0) m_col = COL - 1;
|
||||||
break;
|
else m_col--;
|
||||||
case 3:
|
} else if (direction == 4) {
|
||||||
col--;
|
if (m_col >= COL - 1) m_col = 0;
|
||||||
if (col < 0) col = COL - 1;
|
else m_col++;
|
||||||
break;
|
} else {
|
||||||
case 4:
|
return;
|
||||||
col++;
|
|
||||||
if (col >= COL) col = 0;
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
return {row,col};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Create save_point to take old_direction, new_direction, row and col
|
/* Create save_point to take old_direction, new_direction, row and col
|
||||||
|
|||||||
+19
-19
@@ -1,30 +1,36 @@
|
|||||||
#include "snake.hpp"
|
#include "snake.hpp"
|
||||||
|
|
||||||
Snake::Snake(int default_direction) {
|
Snake::Snake(int default_direction, int row, int col) {
|
||||||
m_direction = default_direction;
|
m_direction = default_direction;
|
||||||
m_tails_count = 0;
|
m_head = Point { row, col };
|
||||||
}
|
}
|
||||||
|
|
||||||
void Snake::create_tail(int row, int col) {
|
void Snake::create_tail(int row, int col) {
|
||||||
|
// if(row >= ROW - 1) {
|
||||||
|
// row = 0;
|
||||||
|
// m_tails.push_back(Point {row, col});
|
||||||
|
// }
|
||||||
|
// if(col >= COL - 1) {
|
||||||
|
// col = 0;
|
||||||
|
// m_tails.push_back(Point {row, col});
|
||||||
|
// }
|
||||||
m_tails.push_back(Point {row, col});
|
m_tails.push_back(Point {row, col});
|
||||||
}
|
}
|
||||||
|
|
||||||
std::pair<int, int> Snake::get_new_point(int tail_index, int direction, int row, int col) {
|
void Snake::get_new_tail_point(int tail_index, int direction) {
|
||||||
m_tails.at(tail_index) = Point {row, col};
|
|
||||||
if (direction == 1) {
|
if (direction == 1) {
|
||||||
if (row >= ROW - 1) row = 1;
|
if (m_tails.at(tail_index).m_row >= ROW - 1) m_tails.at(tail_index).m_row = 0;
|
||||||
else row++;
|
else ++m_tails.at(tail_index).m_row;
|
||||||
} else if (direction == 2) {
|
} else if (direction == 2) {
|
||||||
if (row <= 0) row = ROW - 1;
|
if (m_tails.at(tail_index).m_row <= 0) m_tails.at(tail_index).m_row = ROW - 1;
|
||||||
else row--;
|
else --m_tails.at(tail_index).m_row;
|
||||||
} else if (direction == 3) {
|
} else if (direction == 3) {
|
||||||
if (col <= 0) col = COL - 1;
|
if (m_tails.at(tail_index).m_col <= 0) m_tails.at(tail_index).m_col = COL - 1;
|
||||||
else col--;
|
else --m_tails.at(tail_index).m_row;
|
||||||
} else if (direction == 4) {
|
} else if (direction == 4) {
|
||||||
if (col >= COL - 1) col = 1;
|
if (m_tails.at(tail_index).m_col >= COL - 1) m_tails.at(tail_index).m_col = 0;
|
||||||
else col++;
|
else ++m_tails.at(tail_index).m_row;
|
||||||
}
|
}
|
||||||
return { row, col };
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* snake head for instance is (3, 0)
|
/* snake head for instance is (3, 0)
|
||||||
@@ -34,9 +40,3 @@ std::pair<int, int> Snake::get_new_point(int tail_index, int direction, int row,
|
|||||||
* so if I change direction at (0, 0) from north to west then at (0, 0) there is a postion change saved and once a body hits that
|
* so if I change direction at (0, 0) from north to west then at (0, 0) there is a postion change saved and once a body hits that
|
||||||
* switch direction
|
* switch direction
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* Make a way to get key input
|
|
||||||
* Create a sense of direction
|
|
||||||
* save last position for head and head, save posisition for direction changes
|
|
||||||
* head class and body subclass maybe
|
|
||||||
*/
|
|
||||||
|
|||||||
+18
-14
@@ -4,31 +4,35 @@
|
|||||||
#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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Window::draw_score() {
|
||||||
|
std::cout << "Score: " << m_score << std::endl;
|
||||||
|
}
|
||||||
void Window::draw_window() {
|
void Window::draw_window() {
|
||||||
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;
|
||||||
}
|
|
||||||
std::cout << std::flush;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Window::clear_window() {
|
void Window::clear_window() {
|
||||||
std::cout<<"\033[H" << std::flush;
|
std::cout<<"\033[H" << std::flush;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Window::tick(double frames_per_second) {
|
void Window::tick(double frames_per_second) {
|
||||||
std::this_thread::sleep_for(std::chrono::milliseconds((int) (1000 / frames_per_second)));
|
std::this_thread::sleep_for(std::chrono::milliseconds((int) (1000 / frames_per_second)));
|
||||||
}
|
}
|
||||||
|
|
||||||
void Window::update_loop(int fps) {
|
void Window::update_loop(int fps) {
|
||||||
clear_window();
|
clear_window();
|
||||||
draw_window();
|
draw_window();
|
||||||
tick(fps);
|
tick(fps);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user