complete rewrite of code as unlike snake and tic_tac_toe, this required bigger changes and more volatile changes all at once

This commit is contained in:
DarthMooMancer
2025-11-12 01:22:44 -05:00
parent 69f7393adb
commit 67b13ab164
12 changed files with 193 additions and 270 deletions
-49
View File
@@ -1,49 +0,0 @@
#ifndef ELEMENTS_HPP
#define ELEMENTS_HPP
#include "globals.hpp"
enum Direction {
NONE, UP, DOWN
};
struct vec2 {
vec2() : y(0), x(0) {};
vec2(int r, int c, char s) : y(r), x(c), m_symbol(s) {}
int x, y;
char m_symbol;
};
struct Ball {
Ball() : m_vx(1), m_vy(-1) {};
vec2* m_origin;
int m_vx;
int m_vy;
bool determine_next_valid_position();
};
struct Paddle {
Paddle() : m_score(0) {};
int m_score;
int m_size;
vec2** m_nodes;
enum Direction m_dir;
void determine_new_position(vec2* (*board)[COL]);
};
class Engine {
public:
Engine();
~Engine();
void run();
private:
};
void on_collision_with_ball(vec2** segments, Ball &ball, int t_size);
Ball create_ball(vec2** segments, int row, int col, char symbol, int t_size);
Paddle create_paddle(vec2** segments, int starting_index, int ending_index, int column, char symbol);
#endif
+59
View File
@@ -0,0 +1,59 @@
#ifndef ENGINE_HPP
#define ENGINE_HPP
#include <array>
#include <ncurses.h>
constexpr int ROW = 15;
constexpr int COL = 31;
constexpr int SIZE { 5 };
enum class Direction {
NONE, UP, DOWN
};
struct vec2 {
int a, b;
vec2() : b(0), a(0) {};
vec2(int x, int y) : a(x), b(y) {};
void set(int a1, int b1) { a = a1; b = b1; };
auto operator <=> (const vec2&) const = default;
};
struct Paddle {
Paddle(const vec2 init_pos);
std::array<vec2, SIZE> nodes;
const char symbol { '|' };
enum Direction dir = Direction::NONE;
void move();
};
struct Window {
std::array<std::array<char, COL>, ROW> view = {};
void clear_display();
void draw_display();
void update_display(const vec2 &ball, const Paddle &p1, const Paddle &p2);
};
class Engine {
public:
Engine();
~Engine() { endwin(); };
void run();
private:
Window win;
int time_seconds { 30 };
vec2 score { 0, 0 };
vec2 ball { (COL / 2), (ROW / 2) };
vec2 ball_direction { 1, 1 };
Paddle p1 { { 1, (ROW / 2) - (SIZE / 2) } };
Paddle p2 { { COL - 2, (ROW / 2) - (SIZE / 2) } };
bool exit_process();
bool process_input();
bool move_ball();
void on_collision_with_ball();
};
#endif
-7
View File
@@ -1,7 +0,0 @@
#ifndef GLOBALS_HPP
#define GLOBALS_HPP
constexpr unsigned int ROW = 15;
constexpr unsigned int COL = 31;
#endif
-10
View File
@@ -1,10 +0,0 @@
#ifndef INPUT_HPP
#define INPUT_HPP
#include "elements.hpp"
struct Input {
int m_getch = 0;
void get_input(enum Direction &left, enum Direction &right, bool &running);
};
#endif // INPUT_HPP
-13
View File
@@ -1,13 +0,0 @@
#ifndef WINDOW_HPP
#define WINDOW_HPP
#include "globals.hpp"
#include "elements.hpp"
struct Window {
vec2* m_board[ROW][COL] = {};
void clear_display();
void draw_display(int milliseconds);
void update_display(vec2** segments);
};
#endif