Files
snake/src/main.cpp
T
2025-05-11 07:16:41 -04:00

58 lines
1.8 KiB
C++

#include <iostream>
#include <ostream>
#include <thread>
#include <cstdlib>
#include <unistd.h>
#include "point.hpp"
#include "fruit.hpp"
#include "snake.hpp"
#include "window.hpp"
#include "globals.hpp"
#include "input.hpp"
Snake snake {1};
Window window;
Input input;
Fruit fruit;
int main() {
// 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;
}