fix: properly skips block if deleted and doesnt print the old symbol. removed old helper functions that are redundant
This commit is contained in:
@@ -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();
|
||||
|
||||
@@ -6,9 +6,13 @@
|
||||
#include "elements.hpp"
|
||||
|
||||
struct Window {
|
||||
Window();
|
||||
std::array<std::array<char, COL>, 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
|
||||
|
||||
+4
-6
@@ -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
@@ -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;
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user