feat: fruits, globals, input, snake, point, window
This commit is contained in:
@@ -0,0 +1,13 @@
|
||||
#include <iostream>
|
||||
#include <cstdlib>
|
||||
#include <ctime>
|
||||
#include "util.h"
|
||||
#include "fruit.hpp"
|
||||
|
||||
Fruit::Fruit() {
|
||||
std::srand(std::time(0));
|
||||
}
|
||||
|
||||
void Fruit::create_point() {
|
||||
m_point = Point {rand() % 9, rand() % 9};
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
#include "input.hpp"
|
||||
#include <ncurses.h>
|
||||
|
||||
void Input::get_input() {
|
||||
while(true) {
|
||||
initscr();
|
||||
cbreak();
|
||||
noecho();
|
||||
|
||||
m_current_key = getch();
|
||||
endwin();
|
||||
if(m_current_key == 'q') {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
+47
-51
@@ -1,60 +1,56 @@
|
||||
#include <iostream>
|
||||
#include <termios.h>
|
||||
#include <thread>
|
||||
#include <cstdlib>
|
||||
#include <unistd.h>
|
||||
#include "snake.hpp"
|
||||
#include "point.hpp"
|
||||
#include "fruit.hpp"
|
||||
#include "window.hpp"
|
||||
#include "globals.hpp"
|
||||
#include "input.hpp"
|
||||
|
||||
const int ROW = 10;
|
||||
const int COL = 10;
|
||||
|
||||
char getch();
|
||||
void create_board();
|
||||
void draw_board();
|
||||
|
||||
struct {
|
||||
char board[ROW][COL];
|
||||
} window;
|
||||
|
||||
char (*g_pboard)[ROW][COL] = &window.board;
|
||||
|
||||
char getch() {
|
||||
termios oldt, newt;
|
||||
char ch;
|
||||
tcgetattr(STDIN_FILENO, &oldt); // get current terminal attributes
|
||||
newt = oldt;
|
||||
newt.c_lflag &= ~(ICANON | ECHO); // disable canonical mode and echo
|
||||
tcsetattr(STDIN_FILENO, TCSANOW, &newt); // apply new settings
|
||||
read(STDIN_FILENO, &ch, 1); // read one char
|
||||
tcsetattr(STDIN_FILENO, TCSANOW, &oldt); // restore original settings
|
||||
return ch;
|
||||
}
|
||||
|
||||
void create_board() {
|
||||
for(int i = 0; i < ROW; i++) {
|
||||
for(int a = 0; a < COL; a++) {
|
||||
(*g_pboard)[i][a] = '.';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void draw_board() {
|
||||
for(int i = 0; i < ROW; i++) {
|
||||
for(int a = 0; a < COL; a++) {
|
||||
std::cout << (*g_pboard)[i][a] << " ";
|
||||
}
|
||||
std::cout << "\n";
|
||||
}
|
||||
}
|
||||
Snake snake {1};
|
||||
Window window;
|
||||
Input input;
|
||||
Fruit fruit;
|
||||
|
||||
int main() {
|
||||
create_board();
|
||||
draw_board();
|
||||
draw_snake();
|
||||
// int count = 0;
|
||||
// std::cout << "Press any key other than space or esc: " << std::endl;
|
||||
// while(count < 10) {
|
||||
// char ch = getch();
|
||||
// std::cout << "\nYou pressed: " << ch << '\n';
|
||||
// ++count;
|
||||
// int randomNum = random() % 101;
|
||||
//
|
||||
// std::cout << randomNum;
|
||||
fruit.create_point();
|
||||
std::cout << fruit.m_point.get_point() << std::endl;
|
||||
// std::array<std::array<char, COL>, ROW> &board = window.m_board;
|
||||
// bool update = true;
|
||||
// int row = 0, col = 0;
|
||||
//
|
||||
// Point previous {row,col};
|
||||
// std::thread input_thread(&Input::get_input, &input);
|
||||
// while(update) {
|
||||
// if (input.m_current_key == 's') snake.set_direction(1);
|
||||
// if (input.m_current_key == 'w') snake.set_direction(2);
|
||||
// if (input.m_current_key == 'a') snake.set_direction(3);
|
||||
// if (input.m_current_key == 'd') snake.set_direction(4);
|
||||
// if (input.m_current_key == 'q') break;
|
||||
//
|
||||
// std::tie(row, col) = previous.get_point(snake.get_direction(), row, col);
|
||||
//
|
||||
// board[row][col] = '*';
|
||||
// previous.clear(board); // Clear old point
|
||||
// previous = Point {row, col}; // Set new previous
|
||||
// window.update_loop(5); // Update loop to clear board, tick and redisplay board
|
||||
// }
|
||||
//
|
||||
// input_thread.join();
|
||||
/* First
|
||||
* Learn how to make threads,
|
||||
* for the input thread call the thread then use a while loop within the thread, DO NOT use a while loop to call the thread
|
||||
*
|
||||
* for the update display thread use a while loop within the thread, DO NOT USE a while loop to call the thread.
|
||||
*
|
||||
* the update display thread will ALSO BE YOUR GAME UPDATE THREAD. Your input thread should only be used for INPUT no other function calls should be made and it should NEVER be put to sleep.
|
||||
*
|
||||
*/
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
#include <iostream>
|
||||
#include "point.hpp"
|
||||
|
||||
std::string Point::get_point() const {
|
||||
return "(" + std::to_string(m_row) + ", " + std::to_string(m_col) + ")";
|
||||
}
|
||||
|
||||
void Point::clear(std::array<std::array<char, COL>, ROW> &arr) const {
|
||||
arr[m_row][m_col] = '.';
|
||||
}
|
||||
|
||||
std::pair<int, int> Point::get_point(int direction, int row, int col) {
|
||||
if (direction == 1) {
|
||||
row++;
|
||||
if (row >= ROW) row = 0;
|
||||
} else if (direction == 2) {
|
||||
row--;
|
||||
if (row < 0) row = ROW - 1;
|
||||
} else if (direction == 3) {
|
||||
col--;
|
||||
if (col < 0) col = COL - 1;
|
||||
} else if (direction == 4) {
|
||||
col++;
|
||||
if (col >= COL) col = 0;
|
||||
}
|
||||
return {row, col};
|
||||
}
|
||||
+15
-4
@@ -1,11 +1,16 @@
|
||||
#include "snake.hpp"
|
||||
#include <iostream>
|
||||
|
||||
void create_snake() {
|
||||
std::cout << "Create Snake Test" << std::endl;
|
||||
Snake::Snake(int default_direction) {
|
||||
direction = default_direction;
|
||||
}
|
||||
void draw_snake() {
|
||||
std::cout << "Draw Snake Test" << std::endl;
|
||||
|
||||
int Snake::get_direction() const {
|
||||
return direction;
|
||||
}
|
||||
|
||||
void Snake::set_direction(int x) {
|
||||
direction = x;
|
||||
}
|
||||
|
||||
/* snake head for instance is (3, 0)
|
||||
@@ -15,3 +20,9 @@ void draw_snake() {
|
||||
* 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
|
||||
*/
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
#include "window.hpp"
|
||||
#include <iostream>
|
||||
#include <chrono>
|
||||
#include <thread>
|
||||
|
||||
Window::Window() {
|
||||
for(std::array<char, COL> &row : m_board) {
|
||||
row.fill('.');
|
||||
}
|
||||
}
|
||||
|
||||
void Window::draw_window() {
|
||||
for(std::array<char, COL> &row : m_board) {
|
||||
for(char &col : row) {
|
||||
std::cout << col << " ";
|
||||
}
|
||||
std::cout << "\r\n";
|
||||
}
|
||||
std::cout << std::flush;
|
||||
}
|
||||
|
||||
void Window::clear_window() {
|
||||
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)));
|
||||
}
|
||||
|
||||
void Window::update_loop(int fps) {
|
||||
clear_window();
|
||||
tick(fps);
|
||||
draw_window();
|
||||
}
|
||||
Reference in New Issue
Block a user