This commit is contained in:
DarthMooMancer
2025-05-12 17:15:25 -04:00
parent ad605dd7a2
commit ff942631aa
16 changed files with 96 additions and 73 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.
-2
View File
@@ -3,8 +3,6 @@ project(snake)
set(CMAKE_C_STANDARD 23) set(CMAKE_C_STANDARD 23)
set(CMAKE_C_STANDARD_REQUIRED ON) set(CMAKE_C_STANDARD_REQUIRED ON)
set(CMAKE_VERBOSE_MAKEFILE ON)
include_directories(include) include_directories(include)
set(SOURCES set(SOURCES
+1 -1
View File
@@ -10,6 +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); void reset(std::array<std::array<char, COL>, ROW> &arr, int row, int col, int &score);
}; };
#endif // FRUIT_HPP #endif // FRUIT_HPP
+1 -1
View File
@@ -10,7 +10,7 @@ struct Point {
Point() {} Point() {}
Point(int x, int y) : m_row(x), m_col(y) {} Point(int x, int y) : m_row(x), m_col(y) {}
std::string get_point() const; std::string to_string() const;
void clear(std::array<std::array<char, COL>, ROW> &arr) const; void clear(std::array<std::array<char, COL>, ROW> &arr) const;
std::pair<int, int> get_point(int direction, int row, int col); std::pair<int, int> get_point(int direction, int row, int col);
}; };
+9 -3
View File
@@ -1,11 +1,17 @@
#ifndef SNAKE_HPP #ifndef SNAKE_HPP
#define SNAKE_HPP #define SNAKE_HPP
#include <vector>
#include "point.hpp"
struct Snake { struct Snake {
Snake(int default_direction); Snake(int default_direction);
int get_direction() const; void create_tail(int row, int col);
void set_direction(int x); void update_tails_count();
int direction; // 1: Down, 2: Up, 3: Left, 4: Right
std::vector<Point> m_tails;
int m_direction; // 1: Down, 2: Up, 3: Left, 4: Right
int m_tails_count;
}; };
#endif #endif
+6 -3
View File
@@ -13,7 +13,10 @@ void Fruit::create_point() {
m_col = m_point.m_col; m_col = m_point.m_col;
} }
void Fruit::reset(std::array<std::array<char, COL>, ROW> &arr) { void Fruit::reset(std::array<std::array<char, COL>, ROW> &arr, int row, int col, int &score) {
m_point.clear(arr); if(m_row == row && m_col == col) {
create_point(); m_point.clear(arr);
create_point();
score++;
}
} }
+55 -40
View File
@@ -9,51 +9,66 @@
#include "globals.hpp" #include "globals.hpp"
#include "input.hpp" #include "input.hpp"
Snake snake {1};
Window window;
Input input;
Fruit fruit;
int main() { int main() {
int score = 0; Snake snake {1};
std::array<std::array<char, COL>, ROW> &board = window.m_board; Window window;
bool update = true; Input input;
int row = 0, col = 0; Fruit fruit;
Point previous {row,col}; int x = 5;
fruit.create_point(); int y = 5;
std::thread input_thread(&Input::get_input, &input); for(int i = 0; i <= x; i++) {
while(update) { for(int a = 0; a <= y; a++) {
if (input.m_current_key == 's') snake.set_direction(1); snake.create_tail(i, a);
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;
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
} }
input_thread.join(); for(int i = 0; i < snake.m_tails.size(); i++) {
/* First std::cout << &snake.m_tails.at(i) << std::endl;
* 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 std::cout << snake.m_tails.size() << std::endl;
*
* for the update display thread use a while loop within the thread, DO NOT USE a while loop to call the thread.
* // int score = 0;
* 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. // int row = 0, col = 0;
* // int fps = 5;
*/ // bool running = true;
//
// Point previous {row,col}; // Creating initial snake head position
// fruit.create_point(); // Creating initial fruit object
// std::array<std::array<char, COL>, ROW> &board = window.m_board;
// std::thread input_thread(&Input::get_input, &input);
// while(running) {
// switch (input.m_current_key) {
// case 's':
// snake.m_direction = 1;
// break;
// case 'w':
// snake.m_direction = 2;
// break;
// case 'a':
// snake.m_direction = 3;
// break;
// case 'd':
// snake.m_direction = 4;
// break;
// case 'q':
// running = false;
// break;
// }
//
// fruit.reset(board, row, col, score);
// std::tie(row, col) = previous.get_point(snake.m_direction, row, col);
//
// board[row][col] = '*';
// board[fruit.m_row][fruit.m_col] = '*';
// std::cout << "Score: " << score << std::endl;
// previous.clear(board); // Clear old previous point
// previous = Point {row, col}; // Set new previous point
// window.update_loop(fps); // Update loop to clear board, tick and redisplay board
// }
//
// input_thread.join();
return 0; return 0;
} }
+19 -14
View File
@@ -1,7 +1,7 @@
#include <string> #include <string>
#include "point.hpp" #include "point.hpp"
std::string Point::get_point() const { std::string Point::to_string() const {
return "(" + std::to_string(m_row) + ", " + std::to_string(m_col) + ")"; return "(" + std::to_string(m_row) + ", " + std::to_string(m_col) + ")";
} }
@@ -10,20 +10,25 @@ void Point::clear(std::array<std::array<char, COL>, ROW> &arr) const {
} }
std::pair<int, int> Point::get_point(int direction, int row, int col) { std::pair<int, int> Point::get_point(int direction, int row, int col) {
if (direction == 1) { switch (direction) {
row++; case 1:
if (row >= ROW) row = 0; row ++;
} else if (direction == 2) { if (row >= ROW) row = 0;
row--; break;
if (row < 0) row = ROW - 1; case 2:
} else if (direction == 3) { row--;
col--; if (row < 0) row = ROW - 1;
if (col < 0) col = COL - 1; break;
} else if (direction == 4) { case 3:
col++; col--;
if (col >= COL) col = 0; if (col < 0) col = COL - 1;
break;
case 4:
col++;
if (col >= COL) col = 0;
break;
} }
return {row, col}; return {row,col};
} }
/* Create save_point to take old_direction, new_direction, row and col /* Create save_point to take old_direction, new_direction, row and col
+4 -8
View File
@@ -1,16 +1,12 @@
#include "snake.hpp" #include "snake.hpp"
#include <iostream>
Snake::Snake(int default_direction) { Snake::Snake(int default_direction) {
direction = default_direction; m_direction = default_direction;
m_tails_count = 0;
} }
int Snake::get_direction() const { void Snake::create_tail(int row, int col) {
return direction; m_tails.push_back(Point {row, col});
}
void Snake::set_direction(int x) {
direction = x;
} }
/* snake head for instance is (3, 0) /* snake head for instance is (3, 0)
+1 -1
View File
@@ -29,6 +29,6 @@ void Window::tick(double frames_per_second) {
void Window::update_loop(int fps) { void Window::update_loop(int fps) {
clear_window(); clear_window();
tick(fps);
draw_window(); draw_window();
tick(fps);
} }