fix: properly skips block if deleted and doesnt print the old symbol. removed old helper functions that are redundant

This commit is contained in:
DarthMooMancer
2025-09-01 17:51:36 -04:00
parent f1e32089ae
commit 6529353f2f
5 changed files with 31 additions and 25 deletions
+4 -6
View File
@@ -1,6 +1,5 @@
#include "elements.hpp"
#include "globals.hpp"
#include <iostream>
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;
}
}
+4 -16
View File
@@ -1,4 +1,3 @@
#include <iostream>
#include <thread>
#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;
+16
View File
@@ -3,6 +3,9 @@
#include <chrono>
#include <thread>
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<char, COL> &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);
}