fix: blocks have proper collision and can be broken. Basically done.
This commit is contained in:
@@ -1,8 +1,6 @@
|
||||
#ifndef ELEMENTS_HPP
|
||||
#define ELEMENTS_HPP
|
||||
|
||||
#include <string>
|
||||
|
||||
struct Point {
|
||||
Point();
|
||||
int m_row;
|
||||
@@ -32,10 +30,12 @@ struct Paddle {
|
||||
struct Block {
|
||||
Block(char symbol, int row, int col);
|
||||
int _size;
|
||||
bool _deleted;
|
||||
char m_symbol;
|
||||
Point m_nodes[3];
|
||||
void get_pos();
|
||||
void check_collision(Ball &ball);
|
||||
void check_collision(Ball &ball, int &blocks_left);
|
||||
void _delete();
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
+1
-1
@@ -1,7 +1,7 @@
|
||||
#ifndef GLOBALS_HPP
|
||||
#define GLOBALS_HPP
|
||||
|
||||
constexpr int ROW = 10;
|
||||
constexpr int ROW = 15;
|
||||
constexpr int COL = 15;
|
||||
|
||||
#endif
|
||||
|
||||
@@ -9,5 +9,6 @@ struct Window {
|
||||
std::array<std::array<char, COL>, ROW> m_board = {};
|
||||
void update_display(Paddle &paddle, Ball &ball, Block (&block_list)[15]);
|
||||
void tick(int fps);
|
||||
void clear();
|
||||
};
|
||||
#endif
|
||||
|
||||
+21
-2
@@ -20,7 +20,7 @@ Paddle::Paddle() {
|
||||
_size = 3;
|
||||
m_nodes[0].m_col = 6;
|
||||
for(int i = 0; i < _size; i++) {
|
||||
m_nodes[i].m_row = 8;
|
||||
m_nodes[i].m_row = ROW - 2;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -57,7 +57,7 @@ void Ball::change_velocity(char d) {
|
||||
}
|
||||
|
||||
void Ball::check_collision(int &lives) {
|
||||
if(m_origin.m_row <= 0 || m_origin.m_row >= ROW -1) {
|
||||
if(m_origin.m_row <= 0) {
|
||||
change_velocity('y');
|
||||
} if(m_origin.m_col <= 0 || m_origin.m_col >= COL -1) {
|
||||
change_velocity('x');
|
||||
@@ -78,6 +78,7 @@ void Ball::get_new_pos() {
|
||||
|
||||
Block::Block(char symbol, int row, int col) {
|
||||
_size = 3;
|
||||
_deleted = false;
|
||||
m_symbol = symbol;
|
||||
m_nodes[0].m_col = col;
|
||||
for(int i = 0; i < _size; i++) {
|
||||
@@ -90,3 +91,21 @@ void Block::get_pos() {
|
||||
m_nodes[i].m_col = m_nodes[0].m_col + i;
|
||||
}
|
||||
}
|
||||
|
||||
void Block::_delete() {
|
||||
for(int i = 0; i < _size; i++) {
|
||||
m_nodes[i].assign(ROW - 1, COL - 1);
|
||||
}
|
||||
}
|
||||
|
||||
void Block::check_collision(Ball &ball, int &blocks_left) {
|
||||
for(int i = 0; i < _size; i++) {
|
||||
if(m_nodes[i].m_col == ball.m_origin.m_col) {
|
||||
if(m_nodes[i].m_row == ball.m_origin.m_row) {
|
||||
ball.change_velocity('y');
|
||||
_delete();
|
||||
blocks_left--;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+25
-15
@@ -10,13 +10,14 @@ int main() {
|
||||
Paddle paddle;
|
||||
Ball ball;
|
||||
Block block_list[15] = {
|
||||
Block('3', 0, 0), Block('3', 0, 3), Block('3', 0, 6), Block('3', 0, 9), Block('3', 0, 12),
|
||||
Block('2', 1, 0), Block('2', 1, 3), Block('2', 1, 6), Block('2', 1, 9), Block('2', 1, 12),
|
||||
Block('1', 2, 0), Block('1', 2, 3), Block('1', 2, 6), Block('1', 2, 9), Block('1', 2, 12),
|
||||
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;
|
||||
int lives = 3;
|
||||
int blocks_left = 15;
|
||||
std::thread input_thread(&Input::get_input, &input, std::ref(running), std::ref(paddle));
|
||||
|
||||
while (running) {
|
||||
@@ -24,23 +25,32 @@ int main() {
|
||||
paddle.get_new_pos();
|
||||
ball.check_collision(lives);
|
||||
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);
|
||||
}
|
||||
if(ball.m_origin.m_row >= ROW -1) {
|
||||
lives--;
|
||||
ball.m_origin.assign(ROW - 7, (COL / 2));
|
||||
ball.change_velocity('y');
|
||||
window.update_display(paddle, ball, block_list);
|
||||
window.tick(500);
|
||||
}
|
||||
// if(ball.m_origin.m_row >= ROW -1) {
|
||||
// lives--;
|
||||
// ball.m_origin.assign(ROW - 5, (COL / 2));
|
||||
// ball.vy = 1;
|
||||
// window.update_display(paddle, ball, block_list);
|
||||
// window.tick(500);
|
||||
// }
|
||||
|
||||
paddle.check_collision(ball);
|
||||
ball.get_new_pos();
|
||||
// std::cout << "Lives left: " << lives << std::endl;
|
||||
// if(lives <= 0) {
|
||||
// std::cout << "You lose!" << std::endl;
|
||||
// running = false;
|
||||
// }
|
||||
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;
|
||||
}
|
||||
}
|
||||
input_thread.join();
|
||||
return 0;
|
||||
|
||||
+5
-1
@@ -7,6 +7,10 @@ void Window::tick(int fps) {
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(fps)); // 1000 / fps; 200ms = 5fps
|
||||
}
|
||||
|
||||
void Window::clear() {
|
||||
std::cout << "\033[H" << std::flush;
|
||||
}
|
||||
|
||||
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++) {
|
||||
@@ -18,7 +22,7 @@ void Window::update_display(Paddle &paddle, Ball &ball, Block (&block_list)[15])
|
||||
}
|
||||
}
|
||||
m_board[ball.m_origin.m_row][ball.m_origin.m_col] = '*';
|
||||
std::cout << "\033[H" << std::flush;
|
||||
clear();
|
||||
for(std::array<char, COL> &row : m_board) {
|
||||
for(char &col : row) {
|
||||
std::cout << col << " ";
|
||||
|
||||
Reference in New Issue
Block a user