diff --git a/include/elements.hpp b/include/elements.hpp index a5d3406..de624ef 100644 --- a/include/elements.hpp +++ b/include/elements.hpp @@ -6,7 +6,6 @@ struct Point { int m_row; int m_col; void assign(int row, int col); - void to_string(); }; struct Ball { @@ -14,8 +13,9 @@ struct Ball { Point m_origin; int vx; int vy; + int m_blocks_left; void get_new_pos(); - void check_collision(int &lives); + void check_collision(); void change_velocity(char d); }; @@ -30,7 +30,7 @@ struct Paddle { struct Block { Block(char symbol, int row, int col); int _size; - bool _deleted; + int _deleted; char m_symbol; Point m_nodes[3]; void get_pos(); diff --git a/include/window.hpp b/include/window.hpp index 6d49e2f..13f1605 100644 --- a/include/window.hpp +++ b/include/window.hpp @@ -6,9 +6,13 @@ #include "elements.hpp" struct Window { + Window(); std::array, ROW> m_board = {}; + int m_lives; void update_display(Paddle &paddle, Ball &ball, Block (&block_list)[15]); void tick(int fps); void clear(); + void terminate(Ball &ball, bool &running); }; + #endif diff --git a/src/elements.cpp b/src/elements.cpp index 0b51d9e..0c61e48 100644 --- a/src/elements.cpp +++ b/src/elements.cpp @@ -1,6 +1,5 @@ #include "elements.hpp" #include "globals.hpp" -#include Point::Point() { m_row = 0; @@ -12,10 +11,6 @@ void Point::assign(int row, int col) { m_col = col; } -void Point::to_string() { - std::cout << "(" << m_row << ", " << m_col << ")"; -} - Paddle::Paddle() { _size = 3; m_nodes[0].m_col = 6; @@ -44,6 +39,7 @@ Ball::Ball() { m_origin.assign(ROW - 5, (COL / 2)); vx = 1; vy = -1; + m_blocks_left = 15; } void Ball::change_velocity(char d) { @@ -56,7 +52,7 @@ void Ball::change_velocity(char d) { } } -void Ball::check_collision(int &lives) { +void Ball::check_collision() { if(m_origin.m_row <= 0) { change_velocity('y'); } if(m_origin.m_col <= 0 || m_origin.m_col >= COL -1) { @@ -95,6 +91,8 @@ void Block::get_pos() { void Block::_delete() { for(int i = 0; i < _size; i++) { m_nodes[i].assign(ROW - 1, COL - 1); + m_symbol = '.'; + _deleted = true; } } diff --git a/src/main.cpp b/src/main.cpp index 29e84d4..ac5d3fc 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,4 +1,3 @@ -#include #include #include "input.hpp" #include "window.hpp" @@ -16,23 +15,21 @@ int main() { }; bool running = true; - int lives = 3; - int blocks_left = 15; std::thread input_thread(&Input::get_input, &input, std::ref(running), std::ref(paddle)); while (running) { window.update_display(paddle, ball, block_list); paddle.get_new_pos(); - ball.check_collision(lives); + ball.check_collision(); for(int i = 0; i < 15; i++) { if(block_list[i]._deleted) { continue; } block_list[i].get_pos(); - block_list[i].check_collision(ball, blocks_left); + block_list[i].check_collision(ball, ball.m_blocks_left); } if(ball.m_origin.m_row >= ROW -1) { - lives--; + window.m_lives--; ball.m_origin.assign(ROW - 7, (COL / 2)); ball.change_velocity('y'); window.update_display(paddle, ball, block_list); @@ -41,16 +38,7 @@ int main() { paddle.check_collision(ball); ball.get_new_pos(); - std::cout << "Lives left: " << lives << std::endl; - if(lives <= 0) { - window.clear(); - std::cout << "You lose!" << std::endl; - running = false; - } if(blocks_left <= 0) { - window.clear(); - std::cout << "You win!" << std::endl; - running = false; - } + window.terminate(ball, running); } input_thread.join(); return 0; diff --git a/src/window.cpp b/src/window.cpp index bfd8021..2b36563 100644 --- a/src/window.cpp +++ b/src/window.cpp @@ -3,6 +3,9 @@ #include #include +Window::Window() { + m_lives = 3; +} void Window::tick(int fps) { std::this_thread::sleep_for(std::chrono::milliseconds(fps)); // 1000 / fps; 200ms = 5fps } @@ -11,6 +14,18 @@ void Window::clear() { std::cout << "\033[H" << std::flush; } +void Window::terminate(Ball &ball, bool &running) { + if(m_lives <= 0) { + clear(); + std::cout << "You lose!" << std::endl; + running = false; + } if(ball.m_blocks_left <= 0) { + clear(); + std::cout << "You win!" << std::endl; + running = false; + } +} + void Window::update_display(Paddle &paddle, Ball &ball, Block (&block_list)[15]) { for(std::array &row : m_board) { row.fill('.'); } for(int i = 0; i < paddle._size; i++) { @@ -29,5 +44,6 @@ void Window::update_display(Paddle &paddle, Ball &ball, Block (&block_list)[15]) } std::cout << "\r\n"; } + std::cout << "Lives left: " << m_lives << std::endl; tick(150); }