feat: fruit now spawns on map and can be collect to add to score

This commit is contained in:
DarthMooMancer
2025-05-12 11:22:00 -04:00
parent 552d45c613
commit ad605dd7a2
20 changed files with 27 additions and 31 deletions
+8 -6
View File
@@ -1,5 +1,4 @@
#include <iostream>
#include <ostream>
#include <thread>
#include <cstdlib>
#include <unistd.h>
@@ -16,16 +15,13 @@ 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;
int score = 0;
std::array<std::array<char, COL>, ROW> &board = window.m_board;
bool update = true;
int row = 0, col = 0;
Point previous {row,col};
fruit.create_point();
std::thread input_thread(&Input::get_input, &input);
while(update) {
if (input.m_current_key == 's') snake.set_direction(1);
@@ -34,9 +30,15 @@ int main() {
if (input.m_current_key == 'd') snake.set_direction(4);
if (input.m_current_key == 'q') break;
if(fruit.m_row == row && fruit.m_col == col) {
fruit.reset(board);
++score;
}
std::tie(row, col) = previous.get_point(snake.get_direction(), row, col);
board[row][col] = '*';
board[fruit.m_row][fruit.m_col] = '*';
std::cout << "Score: " << score << std::endl;
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