wip
This commit is contained in:
+3
-3
@@ -4,12 +4,12 @@
|
||||
#include "point.hpp"
|
||||
|
||||
struct Fruit {
|
||||
Fruit();
|
||||
int unsigned m_row;
|
||||
int unsigned m_col;
|
||||
|
||||
Point m_point;
|
||||
Fruit();
|
||||
|
||||
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
|
||||
|
||||
@@ -5,5 +5,6 @@
|
||||
|
||||
constexpr uint16_t ROW = 10;
|
||||
constexpr uint16_t COL = 10;
|
||||
constexpr uint16_t FPS = 5;
|
||||
|
||||
#endif // GLOBALS_HPP
|
||||
|
||||
+3
-2
@@ -2,8 +2,9 @@
|
||||
#define INPUT_HPP
|
||||
|
||||
struct Input {
|
||||
char m_current_key = ' ';
|
||||
void get_input();
|
||||
char m_current_key = ' ';
|
||||
char m_old_key = ' ';
|
||||
void get_input();
|
||||
};
|
||||
|
||||
#endif // INPUT_HPP
|
||||
|
||||
+1
-1
@@ -12,7 +12,7 @@ struct Point {
|
||||
|
||||
std::string to_string() 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
|
||||
|
||||
+6
-7
@@ -5,14 +5,13 @@
|
||||
#include "point.hpp"
|
||||
|
||||
struct Snake {
|
||||
Snake(int default_direction);
|
||||
void create_tail(int row, int col);
|
||||
std::pair<int, int> get_new_point(int tail_index, int direction, int row, int col);
|
||||
void update_tails_count();
|
||||
|
||||
std::vector<Point> m_tails;
|
||||
Snake(int default_direction, int row, int col);
|
||||
Point m_head;
|
||||
std::vector<Point> m_tails;
|
||||
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
|
||||
|
||||
@@ -5,9 +5,11 @@
|
||||
|
||||
struct Window {
|
||||
std::array<std::array<char, COL>, ROW> m_board;
|
||||
int m_score;
|
||||
// Initializes board with . for each value
|
||||
Window();
|
||||
|
||||
void draw_score();
|
||||
void draw_window();
|
||||
void clear_window();
|
||||
void tick(double frames_per_second);
|
||||
|
||||
+3
-1
@@ -5,6 +5,7 @@
|
||||
|
||||
Fruit::Fruit() {
|
||||
std::srand(std::time(0));
|
||||
create_point();
|
||||
}
|
||||
|
||||
void Fruit::create_point() {
|
||||
@@ -13,10 +14,11 @@ void Fruit::create_point() {
|
||||
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) {
|
||||
m_point.clear(arr);
|
||||
create_point();
|
||||
score++;
|
||||
count++;
|
||||
}
|
||||
}
|
||||
|
||||
+10
-9
@@ -2,15 +2,16 @@
|
||||
#include <ncurses.h>
|
||||
|
||||
void Input::get_input() {
|
||||
while(true) {
|
||||
initscr();
|
||||
cbreak();
|
||||
noecho();
|
||||
while(true) {
|
||||
initscr();
|
||||
cbreak();
|
||||
noecho();
|
||||
|
||||
m_current_key = getch();
|
||||
endwin();
|
||||
if(m_current_key == 'q') {
|
||||
break;
|
||||
m_old_key = m_current_key;
|
||||
m_current_key = getch();
|
||||
endwin();
|
||||
if (m_current_key == 'q') {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+53
-58
@@ -2,7 +2,6 @@
|
||||
#include <thread>
|
||||
#include <cstdlib>
|
||||
#include <unistd.h>
|
||||
#include <vector>
|
||||
#include "point.hpp"
|
||||
#include "fruit.hpp"
|
||||
#include "snake.hpp"
|
||||
@@ -11,65 +10,61 @@
|
||||
#include "input.hpp"
|
||||
|
||||
int main (int argc, char *argv[]) {
|
||||
Snake snake {1};
|
||||
Window window;
|
||||
Input input;
|
||||
Fruit fruit;
|
||||
Snake snake { 1, 0, 0 };
|
||||
Window window;
|
||||
Input input;
|
||||
Fruit fruit;
|
||||
|
||||
int x = 5;
|
||||
for(int i = 0; i < x; i++) {
|
||||
snake.create_tail(i, 0);
|
||||
}
|
||||
std::thread input_thread(&Input::get_input, &input);
|
||||
bool running = true;
|
||||
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;
|
||||
int count = 0;
|
||||
while(count < 2) {
|
||||
count++;
|
||||
for(int i = 0; i < snake.m_tails.size(); i++) {
|
||||
std::tie(row, col) = snake.get_new_point(i, 1, row, col);
|
||||
std::cout << snake.m_tails.at(i).to_string() << std::endl;
|
||||
fruit.reset(window.m_board, snake.m_head.m_row, snake.m_head.m_col, window.m_score, count);
|
||||
if (window.m_score % 2 == 0 && count > 0) {
|
||||
if (snake.m_direction == 1) {
|
||||
int r = snake.m_head.m_row;
|
||||
if (r + 1 > ROW - 1) {
|
||||
r = snake.m_head.m_row;
|
||||
}
|
||||
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;
|
||||
// int fps = 5;
|
||||
// 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;
|
||||
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] = '.';
|
||||
}
|
||||
|
||||
std::pair<int, int> Point::get_new_point(int direction, int row, int col) {
|
||||
switch (direction) {
|
||||
case 1:
|
||||
row++;
|
||||
if (row >= ROW) row = 0;
|
||||
break;
|
||||
case 2:
|
||||
row--;
|
||||
if (row < 0) row = ROW - 1;
|
||||
break;
|
||||
case 3:
|
||||
col--;
|
||||
if (col < 0) col = COL - 1;
|
||||
break;
|
||||
case 4:
|
||||
col++;
|
||||
if (col >= COL) col = 0;
|
||||
break;
|
||||
void Point::get_new_point(int direction) {
|
||||
if (direction == 1) {
|
||||
if (m_row >= ROW - 1) m_row = 0;
|
||||
else m_row++;
|
||||
} else if (direction == 2) {
|
||||
if (m_row <= 0) m_row = ROW - 1;
|
||||
else m_row--;
|
||||
} else if (direction == 3) {
|
||||
if (m_col <= 0) m_col = COL - 1;
|
||||
else m_col--;
|
||||
} else if (direction == 4) {
|
||||
if (m_col >= COL - 1) m_col = 0;
|
||||
else m_col++;
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
return {row,col};
|
||||
}
|
||||
|
||||
/* Create save_point to take old_direction, new_direction, row and col
|
||||
|
||||
+19
-19
@@ -1,30 +1,36 @@
|
||||
#include "snake.hpp"
|
||||
|
||||
Snake::Snake(int default_direction) {
|
||||
Snake::Snake(int default_direction, int row, int col) {
|
||||
m_direction = default_direction;
|
||||
m_tails_count = 0;
|
||||
m_head = Point { row, 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});
|
||||
}
|
||||
|
||||
std::pair<int, int> Snake::get_new_point(int tail_index, int direction, int row, int col) {
|
||||
m_tails.at(tail_index) = Point {row, col};
|
||||
void Snake::get_new_tail_point(int tail_index, int direction) {
|
||||
if (direction == 1) {
|
||||
if (row >= ROW - 1) row = 1;
|
||||
else row++;
|
||||
if (m_tails.at(tail_index).m_row >= ROW - 1) m_tails.at(tail_index).m_row = 0;
|
||||
else ++m_tails.at(tail_index).m_row;
|
||||
} else if (direction == 2) {
|
||||
if (row <= 0) row = ROW - 1;
|
||||
else row--;
|
||||
if (m_tails.at(tail_index).m_row <= 0) m_tails.at(tail_index).m_row = ROW - 1;
|
||||
else --m_tails.at(tail_index).m_row;
|
||||
} else if (direction == 3) {
|
||||
if (col <= 0) col = COL - 1;
|
||||
else col--;
|
||||
if (m_tails.at(tail_index).m_col <= 0) m_tails.at(tail_index).m_col = COL - 1;
|
||||
else --m_tails.at(tail_index).m_row;
|
||||
} else if (direction == 4) {
|
||||
if (col >= COL - 1) col = 1;
|
||||
else col++;
|
||||
if (m_tails.at(tail_index).m_col >= COL - 1) m_tails.at(tail_index).m_col = 0;
|
||||
else ++m_tails.at(tail_index).m_row;
|
||||
}
|
||||
return { row, col };
|
||||
}
|
||||
|
||||
/* 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
|
||||
* 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>
|
||||
|
||||
Window::Window() {
|
||||
for(std::array<char, COL> &row : m_board) {
|
||||
row.fill('.');
|
||||
}
|
||||
for(std::array<char, COL> &row : m_board) {
|
||||
row.fill('.');
|
||||
}
|
||||
m_score = 0;
|
||||
}
|
||||
|
||||
void Window::draw_score() {
|
||||
std::cout << "Score: " << m_score << std::endl;
|
||||
}
|
||||
void Window::draw_window() {
|
||||
for(std::array<char, COL> &row : m_board) {
|
||||
for(char &col : row) {
|
||||
std::cout << col << " ";
|
||||
for(std::array<char, COL> &row : m_board) {
|
||||
for(char &col : row) {
|
||||
std::cout << col << " ";
|
||||
}
|
||||
std::cout << "\r\n";
|
||||
}
|
||||
std::cout << "\r\n";
|
||||
}
|
||||
std::cout << std::flush;
|
||||
std::cout << std::flush;
|
||||
}
|
||||
|
||||
void Window::clear_window() {
|
||||
std::cout<<"\033[H" << std::flush;
|
||||
std::cout<<"\033[H" << std::flush;
|
||||
}
|
||||
|
||||
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) {
|
||||
clear_window();
|
||||
draw_window();
|
||||
tick(fps);
|
||||
clear_window();
|
||||
draw_window();
|
||||
tick(fps);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user