feat: fruit now spawns on map and can be collect to add to score
This commit is contained in:
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.
+1
-1
@@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.16)
|
|||||||
project(snake)
|
project(snake)
|
||||||
set(CMAKE_C_STANDARD 23)
|
set(CMAKE_C_STANDARD 23)
|
||||||
set(CMAKE_C_STANDARD_REQUIRED ON)
|
set(CMAKE_C_STANDARD_REQUIRED ON)
|
||||||
# Enable verbose makefile for better output
|
|
||||||
set(CMAKE_VERBOSE_MAKEFILE ON)
|
set(CMAKE_VERBOSE_MAKEFILE ON)
|
||||||
|
|
||||||
include_directories(include)
|
include_directories(include)
|
||||||
|
|||||||
Symlink
+1
@@ -0,0 +1 @@
|
|||||||
|
build/compile_commands.json
|
||||||
@@ -10,5 +10,6 @@ struct Fruit {
|
|||||||
Point m_point;
|
Point m_point;
|
||||||
Fruit();
|
Fruit();
|
||||||
void create_point();
|
void create_point();
|
||||||
|
void reset(std::array<std::array<char, COL>, ROW> &arr);
|
||||||
};
|
};
|
||||||
#endif // FRUIT_HPP
|
#endif // FRUIT_HPP
|
||||||
|
|||||||
+8
-2
@@ -1,8 +1,7 @@
|
|||||||
#include <iostream>
|
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
#include <ctime>
|
#include <ctime>
|
||||||
// #include "util.h"
|
|
||||||
#include "fruit.hpp"
|
#include "fruit.hpp"
|
||||||
|
#include "globals.hpp"
|
||||||
|
|
||||||
Fruit::Fruit() {
|
Fruit::Fruit() {
|
||||||
std::srand(std::time(0));
|
std::srand(std::time(0));
|
||||||
@@ -10,4 +9,11 @@ Fruit::Fruit() {
|
|||||||
|
|
||||||
void Fruit::create_point() {
|
void Fruit::create_point() {
|
||||||
m_point = Point {rand() % 9, rand() % 9};
|
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
@@ -1,5 +1,4 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <ostream>
|
|
||||||
#include <thread>
|
#include <thread>
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
@@ -16,16 +15,13 @@ Input input;
|
|||||||
Fruit fruit;
|
Fruit fruit;
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
// int randomNum = random() % 101;
|
int score = 0;
|
||||||
//
|
|
||||||
// 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;
|
std::array<std::array<char, COL>, ROW> &board = window.m_board;
|
||||||
bool update = true;
|
bool update = true;
|
||||||
int row = 0, col = 0;
|
int row = 0, col = 0;
|
||||||
|
|
||||||
Point previous {row,col};
|
Point previous {row,col};
|
||||||
|
fruit.create_point();
|
||||||
std::thread input_thread(&Input::get_input, &input);
|
std::thread input_thread(&Input::get_input, &input);
|
||||||
while(update) {
|
while(update) {
|
||||||
if (input.m_current_key == 's') snake.set_direction(1);
|
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 == 'd') snake.set_direction(4);
|
||||||
if (input.m_current_key == 'q') break;
|
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);
|
std::tie(row, col) = previous.get_point(snake.get_direction(), row, col);
|
||||||
|
|
||||||
board[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.clear(board); // Clear old point
|
||||||
previous = Point {row, col}; // Set new previous
|
previous = Point {row, col}; // Set new previous
|
||||||
window.update_loop(5); // Update loop to clear board, tick and redisplay board
|
window.update_loop(5); // Update loop to clear board, tick and redisplay board
|
||||||
|
|||||||
+6
-1
@@ -1,4 +1,4 @@
|
|||||||
#include <iostream>
|
#include <string>
|
||||||
#include "point.hpp"
|
#include "point.hpp"
|
||||||
|
|
||||||
std::string Point::get_point() const {
|
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};
|
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
|
||||||
|
* */
|
||||||
|
|||||||
@@ -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"
|
|
||||||
|
|
||||||
Reference in New Issue
Block a user