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
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
+2
View File
@@ -0,0 +1,2 @@
CompileFlags:
Add: [-Iinclude]
+1 -1
View File
@@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.16)
project(snake)
set(CMAKE_C_STANDARD 23)
set(CMAKE_C_STANDARD_REQUIRED ON)
# Enable verbose makefile for better output
set(CMAKE_VERBOSE_MAKEFILE ON)
include_directories(include)
+1
View File
@@ -0,0 +1 @@
build/compile_commands.json
+1
View File
@@ -10,5 +10,6 @@ struct Fruit {
Point m_point;
Fruit();
void create_point();
void reset(std::array<std::array<char, COL>, ROW> &arr);
};
#endif // FRUIT_HPP
+8 -2
View File
@@ -1,8 +1,7 @@
#include <iostream>
#include <cstdlib>
#include <ctime>
// #include "util.h"
#include "fruit.hpp"
#include "globals.hpp"
Fruit::Fruit() {
std::srand(std::time(0));
@@ -10,4 +9,11 @@ Fruit::Fruit() {
void Fruit::create_point() {
m_point = Point {rand() % 9, rand() % 9};
m_row = m_point.m_row;
m_col = m_point.m_col;
}
void Fruit::reset(std::array<std::array<char, COL>, ROW> &arr) {
m_point.clear(arr);
create_point();
}
+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
+6 -1
View File
@@ -1,4 +1,4 @@
#include <iostream>
#include <string>
#include "point.hpp"
std::string Point::get_point() const {
@@ -25,3 +25,8 @@ std::pair<int, int> Point::get_point(int direction, int row, int col) {
}
return {row, col};
}
/* Create save_point to take old_direction, new_direction, row and col
* Once a tail hits the { row, col }, change old_direction to new_direction
* Tail has own point to manage, game has array of points to manage tail and mantain coords with direction
* */
-21
View File
@@ -1,21 +0,0 @@
#!/usr/bin/env bash
# Start from the current directory
DIR="$(pwd)"
# Walk up until we find a marker or reach root
while [[ ! -f "$DIR/CMakeLists.txt" && \
! -f "$DIR/package.json" && \
! -d "$DIR/.git" && \
"$DIR" != "/" ]]; do
DIR="$(dirname "$DIR")"
done
# Check if root was reached without finding anything
if [[ "$DIR" == "/" ]]; then
echo "❌ Project root not found."
exit 1
fi
echo "✅ Project root found at: $DIR"