This commit is contained in:
DarthMooMancer
2025-09-28 18:00:34 -04:00
parent 6529353f2f
commit a0b29a40d1
7 changed files with 96 additions and 57 deletions
+15
View File
@@ -1,10 +1,24 @@
#ifndef ELEMENTS_HPP #ifndef ELEMENTS_HPP
#define ELEMENTS_HPP #define ELEMENTS_HPP
struct Point;
struct Paddle;
struct Block;
struct Ball;
Paddle create_new_paddle(int size);
enum Direction {
LEFT = -1,
RIGHT = 1,
NONE = 0,
};
struct Point { struct Point {
Point(); Point();
Point(int row, int col, char symbol) : m_row(row), m_col(col), m_symbol(symbol) {};
int m_row; int m_row;
int m_col; int m_col;
char m_symbol;
void assign(int row, int col); void assign(int row, int col);
}; };
@@ -23,6 +37,7 @@ struct Paddle {
Paddle(); Paddle();
int _size; int _size;
Point m_nodes[3]; Point m_nodes[3];
enum Direction m_direction;
void get_new_pos(); void get_new_pos();
void check_collision(Ball &ball); void check_collision(Ball &ball);
}; };
+3 -3
View File
@@ -1,7 +1,7 @@
#ifndef GLOBALS_HPP #ifndef GLOBALS_HPP
#define GLOBALS_HPP #define GLOBALS_HPP
#define ROW 15
constexpr int ROW = 15; #define COL 15
constexpr int COL = 15; #define FPMS 300
#endif #endif
+2 -2
View File
@@ -3,8 +3,8 @@
#include "elements.hpp" #include "elements.hpp"
struct Input { struct Input {
char m_getch = ' '; int m_getch;
void get_input(bool &running, Paddle &paddle); void get_input(bool &running, enum Direction &dir);
}; };
#endif // INPUT_HPP #endif // INPUT_HPP
+6 -3
View File
@@ -1,18 +1,21 @@
#ifndef WINDOW_HPP #ifndef WINDOW_HPP
#define WINDOW_HPP #define WINDOW_HPP
#include <array>
#include "globals.hpp" #include "globals.hpp"
#include "elements.hpp" #include "elements.hpp"
struct Window { struct Window {
Window(); Window();
std::array<std::array<char, COL>, ROW> m_board = {}; Point* m_board[ROW][COL] = {};
int m_lives; int m_lives;
void update_display(Paddle &paddle, Ball &ball, Block (&block_list)[15]); // void update_display(Paddle &paddle, Ball &ball, Block (&block_list)[15]);
void tick(int fps); void tick(int fps);
void clear(); void clear();
void terminate(Ball &ball, bool &running); void terminate(Ball &ball, bool &running);
void clear_display();
void draw_display(int fps);
void update_display(Point** segments, int segments_size);
}; };
#endif #endif
+13 -9
View File
@@ -1,8 +1,7 @@
#include <ncurses.h> #include <ncurses.h>
#include "input.hpp" #include "input.hpp"
#include "globals.hpp"
void Input::get_input(bool &running, Paddle &paddle) { void Input::get_input(bool &running, enum Direction &dir) {
while(running) { while(running) {
initscr(); initscr();
cbreak(); cbreak();
@@ -10,12 +9,17 @@ void Input::get_input(bool &running, Paddle &paddle) {
m_getch = getch(); m_getch = getch();
endwin(); endwin();
if(m_getch == 'a' && paddle.m_nodes[0].m_col > 0) { switch (m_getch) {
paddle.m_nodes[0].m_col--; case 67:
m_getch = ' '; dir = RIGHT;
} else if(m_getch == 'd' && paddle.m_nodes[paddle._size - 1].m_col < COL - 1) { break;
paddle.m_nodes[0].m_col++; case 68:
m_getch = ' '; dir = LEFT;
} if(m_getch == 'q') running = false; break;
break;
case 113:
running = false;
break;
}
} }
} }
+36 -25
View File
@@ -1,4 +1,5 @@
#include <thread> #include <thread>
#include "globals.hpp"
#include "input.hpp" #include "input.hpp"
#include "window.hpp" #include "window.hpp"
#include "elements.hpp" #include "elements.hpp"
@@ -8,38 +9,48 @@ int main() {
Window window; Window window;
Paddle paddle; Paddle paddle;
Ball ball; Ball ball;
Block block_list[15] = {
Block('3', 1, 0), Block('3', 1, 3), Block('3', 1, 6), Block('3', 1, 9), Block('3', 1, 12), Point* segments[50] = {};
Block('2', 2, 0), Block('2', 2, 3), Block('2', 2, 6), Block('2', 2, 9), Block('2', 2, 12), segments[1] = new Point { 2, 5, '*' };
Block('1', 3, 0), Block('1', 3, 3), Block('1', 3, 6), Block('1', 3, 9), Block('1', 3, 12), // Block block_list[15] = {
}; // Block('3', 1, 0), Block('3', 1, 3), Block('3', 1, 6), Block('3', 1, 9), Block('3', 1, 12),
// Block('2', 2, 0), Block('2', 2, 3), Block('2', 2, 6), Block('2', 2, 9), Block('2', 2, 12),
// Block('1', 3, 0), Block('1', 3, 3), Block('1', 3, 6), Block('1', 3, 9), Block('1', 3, 12),
// };
bool running = true; bool running = true;
std::thread input_thread(&Input::get_input, &input, std::ref(running), std::ref(paddle)); std::thread input_thread([&] { input.get_input(running, paddle.m_direction); } );
while (running) { while (running) {
window.update_display(paddle, ball, block_list); // window.update_display(paddle, ball, block_list);
paddle.get_new_pos(); paddle.get_new_pos();
ball.check_collision(); // ball.check_collision();
for(int i = 0; i < 15; i++) { // for(int i = 0; i < 15; i++) {
if(block_list[i]._deleted) { // if(block_list[i]._deleted) {
continue; // continue;
} // }
block_list[i].get_pos(); // block_list[i].get_pos();
block_list[i].check_collision(ball, ball.m_blocks_left); // block_list[i].check_collision(ball, ball.m_blocks_left);
} // }
if(ball.m_origin.m_row >= ROW -1) { // if(ball.m_origin.m_row >= ROW -1) {
window.m_lives--; // window.m_lives--;
ball.m_origin.assign(ROW - 7, (COL / 2)); // ball.m_origin.assign(ROW - 7, (COL / 2));
ball.change_velocity('y'); // ball.change_velocity('y');
window.update_display(paddle, ball, block_list); // // window.update_display(paddle, ball, block_list);
window.tick(500); // window.tick(FPMS);
} // }
paddle.check_collision(ball); // paddle.check_collision(ball);
ball.get_new_pos(); // ball.get_new_pos();
window.terminate(ball, running); // window.terminate(ball, running);
window.clear_display();
window.update_display(segments, 50);
window.draw_display(FPMS);
} }
input_thread.join(); input_thread.join();
for(int i = 0; i < 50; i++) {
if(segments[i] == nullptr) continue;
delete segments[i];
}
return 0; return 0;
} }
+21 -15
View File
@@ -26,24 +26,30 @@ void Window::terminate(Ball &ball, bool &running) {
} }
} }
void Window::update_display(Paddle &paddle, Ball &ball, Block (&block_list)[15]) { void Window::clear_display() {
for(std::array<char, COL> &row : m_board) { row.fill('.'); } for(int i = 0; i < ROW; i++) {
for(int i = 0; i < paddle._size; i++) { for(int j = 0; j < COL; j++) {
m_board[paddle.m_nodes[i].m_row][paddle.m_nodes[i].m_col] = '-'; m_board[i][j] = nullptr;
}
for(int i = 0; i < 15; i++) {
for(int j = 0; j < block_list[i]._size; j++) {
m_board[block_list[i].m_nodes[j].m_row][block_list[i].m_nodes[j].m_col] = block_list[i].m_symbol;
} }
} }
m_board[ball.m_origin.m_row][ball.m_origin.m_col] = '*'; }
clear();
for(std::array<char, COL> &row : m_board) { void Window::update_display(Point** segments, int segments_size) {
for(char &col : row) { for(int i = segments_size - 1; i >= 0; i--) {
std::cout << col << " "; if(segments[i] != nullptr) {
m_board[segments[i]->m_row][segments[i]->m_col] = segments[i];
}
}
}
void Window::draw_display(int fps) {
std::cout << "\033[H" << std::flush;
for(int i = 0; i < ROW; i++) {
for(int j = 0; j < COL; j++) {
if(m_board[i][j] != nullptr) std::cout << m_board[i][j]->m_symbol << " ";
else std::cout << ". ";
} }
std::cout << "\r\n"; std::cout << "\r\n";
} }
std::cout << "Lives left: " << m_lives << std::endl; std::this_thread::sleep_for(std::chrono::milliseconds(fps));
tick(150);
} }