can draw block buffer to screen and collide but the collide is broken
This commit is contained in:
+13
-10
@@ -8,8 +8,11 @@ struct Paddle;
|
|||||||
struct Block;
|
struct Block;
|
||||||
struct Ball;
|
struct Ball;
|
||||||
Paddle create_new_paddle(Point** buffer, int starting_array_index, int size, int row, char symbol);
|
Paddle create_new_paddle(Point** buffer, int starting_array_index, int size, int row, char symbol);
|
||||||
Block create_new_block(char symbol, int row, int col, int size);
|
Ball create_new_ball(Point** buffer, int index, int default_vx, int default_vy, int row, int col, char symbol);
|
||||||
Block* create_block_buffer(Point** buffer, int starting_symbol, int rows, int col, int length);
|
Block** create_block_buffer(int starting_array_index, int starting_symbol, int rows, int cols, int length);
|
||||||
|
void rasterize_block(Point** buffer, Block* block, int starting_index);
|
||||||
|
void rasterize_buffer(Point** buffer, Block** block_buffer, int offset);
|
||||||
|
void trim_block_buffer_on_collision(Block** block_buffer, int block_buffer_size, Ball &ball);
|
||||||
|
|
||||||
enum Direction {
|
enum Direction {
|
||||||
LEFT = -1,
|
LEFT = -1,
|
||||||
@@ -25,16 +28,16 @@ struct Point {
|
|||||||
int m_col;
|
int m_col;
|
||||||
char m_symbol;
|
char m_symbol;
|
||||||
void assign(int row, int col);
|
void assign(int row, int col);
|
||||||
|
void to_string();
|
||||||
};
|
};
|
||||||
|
|
||||||
struct Ball {
|
struct Ball {
|
||||||
Ball();
|
Ball(int default_vx, int default_vy) : vx(default_vx), vy(default_vy) {};
|
||||||
Point m_origin;
|
Point* m_origin;
|
||||||
int vx;
|
int vx;
|
||||||
int vy;
|
int vy;
|
||||||
int m_blocks_left;
|
int m_blocks_left;
|
||||||
void get_new_pos();
|
void determine_new_position();
|
||||||
void check_collision();
|
|
||||||
void change_velocity(char d);
|
void change_velocity(char d);
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -49,14 +52,14 @@ struct Paddle {
|
|||||||
};
|
};
|
||||||
|
|
||||||
struct Block {
|
struct Block {
|
||||||
// Block();
|
Block();
|
||||||
// Block(char symbol, int row, int col, int size);
|
Block(int size, int row, int col, char symbol);
|
||||||
// ~Block();
|
~Block();
|
||||||
int _size;
|
int _size;
|
||||||
int _deleted;
|
int _deleted;
|
||||||
Point** m_nodes;
|
Point** m_nodes;
|
||||||
void get_pos();
|
void get_pos();
|
||||||
void check_collision(Ball &ball, int &blocks_left);
|
bool check_collision(Ball &ball);
|
||||||
void _delete();
|
void _delete();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -2,6 +2,6 @@
|
|||||||
#define GLOBALS_HPP
|
#define GLOBALS_HPP
|
||||||
#define ROW 15
|
#define ROW 15
|
||||||
#define COL 15
|
#define COL 15
|
||||||
#define FPMS 100
|
#define FPMS 150
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
+1
-1
@@ -15,7 +15,7 @@ struct Window {
|
|||||||
|
|
||||||
void clear_display();
|
void clear_display();
|
||||||
void draw_display(int fps);
|
void draw_display(int fps);
|
||||||
void update_display(Point** buffer, int buffer_size);
|
void update_board(Point** buffer, int buffer_size);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
+81
-98
@@ -1,14 +1,15 @@
|
|||||||
#include "elements.hpp"
|
#include "elements.hpp"
|
||||||
#include "globals.hpp"
|
#include "globals.hpp"
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
|
||||||
Point::Point() {
|
Point::Point() {
|
||||||
m_row = 0;
|
m_row = 0;
|
||||||
m_col = 0;
|
m_col = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Point::assign(int row, int col) {
|
void Point::to_string() {
|
||||||
m_row = row;
|
std::cout << "(" << m_row << ", " << m_col << ")\n";
|
||||||
m_col = col;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Paddle create_new_paddle(Point** buffer, int starting_array_index, int size, int row, char symbol) {
|
Paddle create_new_paddle(Point** buffer, int starting_array_index, int size, int row, char symbol) {
|
||||||
@@ -23,30 +24,52 @@ Paddle create_new_paddle(Point** buffer, int starting_array_index, int size, int
|
|||||||
return paddle;
|
return paddle;
|
||||||
}
|
}
|
||||||
|
|
||||||
Block create_new_block(char symbol, int row, int col, int size) {
|
Ball create_new_ball(Point** buffer, int index, int default_vx, int default_vy, int row, int col, char symbol) {
|
||||||
Block block;
|
Ball ball { default_vx, default_vy };
|
||||||
block._size = size;
|
ball.m_origin = new Point { row, col, symbol };
|
||||||
block.m_nodes = new Point*[size];
|
buffer[index] = ball.m_origin;
|
||||||
for(int i = 0; i < size; i++) {
|
return ball;
|
||||||
block.m_nodes[i] = new Point { row, col + i, symbol };
|
|
||||||
}
|
|
||||||
return block;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Block* create_block_buffer(Point** buffer, int starting_symbol, int rows, int cols, int length) {
|
Block** create_block_buffer(int starting_array_index, int starting_symbol, int rows, int cols, int length) {
|
||||||
// Block* block_buffer;
|
Block** block_buffer = new Block*[rows * cols];
|
||||||
// block_buffer = new Block[rows * cols];
|
int row, int_symbol, col;
|
||||||
// for(int i = 0; i < rows; i++) {
|
char char_symbol;
|
||||||
// for(int b = 0; b < cols; b++) {
|
for(int index = 0; index < rows * cols; index++) {
|
||||||
// block_buffer[i + b] = create_new_block(symbol, i, b + length, int size)
|
row = index / 5;
|
||||||
// }
|
int_symbol = (starting_symbol - '0') - row;
|
||||||
// Block block = create_new_block('3', 0, 0, 3);
|
char_symbol = '0' + int_symbol;
|
||||||
// // if(buffer[i + 4] == nullptr) std::cout << "block point is a nullptr\n";
|
col = (index % 5) * 3;
|
||||||
// buffer[i + 4] = block.m_nodes[i];
|
block_buffer[index] = new Block(length, row, col, char_symbol);
|
||||||
// }
|
}
|
||||||
//
|
return block_buffer;
|
||||||
// return block_buffer;
|
}
|
||||||
// }
|
|
||||||
|
void rasterize_block(Point** buffer, Block* block, int starting_index) {
|
||||||
|
for(int i = 0; i < block->_size; i++) {
|
||||||
|
buffer[starting_index + i] = block->m_nodes[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void rasterize_buffer(Point** buffer, Block** block_buffer, int offset) {
|
||||||
|
for(int i = 0; i < 15; i++) {
|
||||||
|
rasterize_block(buffer, block_buffer[i], offset);
|
||||||
|
offset += 3;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void trim_block_buffer_on_collision(Block** block_buffer, int block_buffer_size, Ball &ball) {
|
||||||
|
for(int i = 0; i < block_buffer_size; i++) {
|
||||||
|
if(block_buffer[i] == nullptr) continue;
|
||||||
|
if(block_buffer[i]->check_collision(ball)) {
|
||||||
|
for(int a = 0; a < block_buffer[i]->_size; a++) {
|
||||||
|
delete block_buffer[i]->m_nodes[a];
|
||||||
|
}
|
||||||
|
ball.change_velocity('y');
|
||||||
|
delete block_buffer[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Paddle::~Paddle() {
|
Paddle::~Paddle() {
|
||||||
delete[] m_nodes;
|
delete[] m_nodes;
|
||||||
@@ -68,21 +91,12 @@ void Paddle::determine_new_position(Point* (*board)[COL]) {
|
|||||||
m_direction = NONE;
|
m_direction = NONE;
|
||||||
}
|
}
|
||||||
|
|
||||||
// void Paddle::check_collision(Ball &ball) {
|
void Paddle::check_collision(Ball &ball) {
|
||||||
// for(int i = 0; i < _size; i++) {
|
for(int i = 0; i < _size; i++) {
|
||||||
// if(ball.m_origin.m_row == m_nodes[i].m_row) {
|
if(ball.m_origin->m_row != m_nodes[i]->m_row) continue;
|
||||||
// if(ball.m_origin.m_col == m_nodes[i].m_col) {
|
if(ball.m_origin->m_col != m_nodes[i]->m_col) continue;
|
||||||
// ball.change_velocity('y');
|
ball.change_velocity('y');
|
||||||
// }
|
}
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
Ball::Ball() {
|
|
||||||
m_origin.assign(ROW - 5, (COL / 2));
|
|
||||||
vx = 1;
|
|
||||||
vy = -1;
|
|
||||||
m_blocks_left = 15;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Ball::change_velocity(char d) {
|
void Ball::change_velocity(char d) {
|
||||||
@@ -95,67 +109,36 @@ void Ball::change_velocity(char d) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Ball::check_collision() {
|
void Ball::determine_new_position() {
|
||||||
if(m_origin.m_row <= 0) {
|
if(m_origin->m_row <= 0 || m_origin->m_row >= ROW - 1) change_velocity('y');
|
||||||
change_velocity('y');
|
if(m_origin->m_col <= 0 || m_origin->m_col >= COL - 1) change_velocity('x');
|
||||||
} if(m_origin.m_col <= 0 || m_origin.m_col >= COL -1) {
|
|
||||||
change_velocity('x');
|
if(vx == -1) m_origin->m_col--;
|
||||||
|
else if(vx == 1) m_origin->m_col++;
|
||||||
|
|
||||||
|
if(vy == -1) m_origin->m_row--;
|
||||||
|
else if(vy == 1) m_origin->m_row++;
|
||||||
|
}
|
||||||
|
|
||||||
|
Block::~Block() {
|
||||||
|
delete[] m_nodes;
|
||||||
|
}
|
||||||
|
|
||||||
|
Block::Block(int size, int row, int col, char symbol) {
|
||||||
|
_size = size;
|
||||||
|
m_nodes = new Point*[size];
|
||||||
|
for(int i = 0; i < size; i++) {
|
||||||
|
m_nodes[i] = new Point { row, col + i, symbol };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Ball::get_new_pos() {
|
bool Block::check_collision(Ball &ball) {
|
||||||
if(vx == -1) {
|
for(int i = 0; i < _size; i++) {
|
||||||
m_origin.m_col--;
|
if(m_nodes[i]->m_col != ball.m_origin->m_col) continue;
|
||||||
} else if(vx == 1) {
|
if(m_nodes[i]->m_row != ball.m_origin->m_row) continue;
|
||||||
m_origin.m_col++;
|
return true;
|
||||||
} if(vy == -1) {
|
|
||||||
m_origin.m_row--;
|
|
||||||
} else if(vy == 1) {
|
|
||||||
m_origin.m_row++;
|
|
||||||
}
|
}
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Block::~Block() {
|
// ball.change_velocity('y');
|
||||||
// delete[] m_nodes;
|
|
||||||
// }
|
|
||||||
|
|
||||||
// Block::Block(char symbol, int row, int col, int size) {
|
|
||||||
// _size = size;
|
|
||||||
// m_symbol = symbol;
|
|
||||||
// for(int i = 0; i < size; i++) {
|
|
||||||
// m_nodes[i] = new Point;
|
|
||||||
// m_nodes[i]->assign(row, col + i);
|
|
||||||
//
|
|
||||||
// }
|
|
||||||
// m_nodes[0]->m_col = col;
|
|
||||||
// for(int i = 0; i < _size; i++) {
|
|
||||||
// m_nodes[i].m_row = row;
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
//
|
|
||||||
// void Block::get_pos() {
|
|
||||||
// for(int i = 1; i < _size; i++) {
|
|
||||||
// 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);
|
|
||||||
// m_symbol = '.';
|
|
||||||
// _deleted = true;
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// 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--;
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|||||||
+8
-41
@@ -8,57 +8,23 @@
|
|||||||
int main(int argc, char **argv) {
|
int main(int argc, char **argv) {
|
||||||
Input input;
|
Input input;
|
||||||
Window window;
|
Window window;
|
||||||
// Ball ball;
|
|
||||||
|
|
||||||
Point* buffer[50] = {};
|
Point* buffer[50] = {};
|
||||||
Paddle paddle = create_new_paddle(buffer, 1, 3, ROW - 2, '-');
|
Paddle paddle = create_new_paddle(buffer, 1, 3, ROW - 2, '-');
|
||||||
|
Ball ball = create_new_ball(buffer, 0, 1, -1, (ROW - 5), (COL / 2), '*' );
|
||||||
// Block block_list[15] = {
|
Block** block_buffer = create_block_buffer(4, '3', 3, 5, 3);
|
||||||
// Block('3', 1, 0), Block('3', 1, 3), Block('3', 1, 6), Block('3', 1, 9), Block('3', 1, 12),
|
rasterize_buffer(buffer, block_buffer, 4);
|
||||||
// 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),
|
|
||||||
// };
|
|
||||||
Block block = create_new_block('3', 0, 0, 3);
|
|
||||||
for(int i = 0; i < block._size; i++) {
|
|
||||||
// if(buffer[i + 4] == nullptr) std::cout << "block point is a nullptr\n";
|
|
||||||
buffer[i + 4] = block.m_nodes[i];
|
|
||||||
}
|
|
||||||
|
|
||||||
bool running = true;
|
bool running = true;
|
||||||
std::thread input_thread([&] { input.get_input(running, paddle.m_direction); } );
|
std::thread input_thread([&] { input.get_input(running, paddle.m_direction); } );
|
||||||
|
|
||||||
while (running) {
|
while (running) {
|
||||||
// window.update_display(paddle, ball, block_list);
|
|
||||||
// paddle.m_direction = RIGHT;
|
|
||||||
// for(int i = 0; i < 8; i++) {
|
|
||||||
// paddle.m_direction = RIGHT;
|
|
||||||
// }
|
|
||||||
// for(int i = 0; i < 10; i++) {
|
|
||||||
// if(buffer[i] == nullptr) continue;
|
|
||||||
// buffer[i]->to_string();
|
|
||||||
// }
|
|
||||||
// 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, ball.m_blocks_left);
|
|
||||||
// }
|
|
||||||
// if(ball.m_origin.m_row >= ROW -1) {
|
|
||||||
// window.m_lives--;
|
|
||||||
// ball.m_origin.assign(ROW - 7, (COL / 2));
|
|
||||||
// ball.change_velocity('y');
|
|
||||||
// // window.update_display(paddle, ball, block_list);
|
|
||||||
// window.tick(FPMS);
|
|
||||||
// }
|
|
||||||
|
|
||||||
// paddle.check_collision(ball);
|
|
||||||
// ball.get_new_pos();
|
|
||||||
// window.terminate(ball, running);
|
|
||||||
paddle.determine_new_position(window.m_board);
|
paddle.determine_new_position(window.m_board);
|
||||||
|
paddle.check_collision(ball);
|
||||||
|
trim_block_buffer_on_collision(block_buffer, 15, ball);
|
||||||
|
ball.determine_new_position();
|
||||||
window.clear_display();
|
window.clear_display();
|
||||||
window.update_display(buffer, 50);
|
window.update_board(buffer, 50);
|
||||||
window.draw_display(FPMS);
|
window.draw_display(FPMS);
|
||||||
}
|
}
|
||||||
input_thread.join();
|
input_thread.join();
|
||||||
@@ -66,5 +32,6 @@ int main(int argc, char **argv) {
|
|||||||
if(buffer[i] == nullptr) continue;
|
if(buffer[i] == nullptr) continue;
|
||||||
delete buffer[i];
|
delete buffer[i];
|
||||||
}
|
}
|
||||||
|
delete[] block_buffer;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-1
@@ -34,7 +34,7 @@ void Window::clear_display() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Window::update_display(Point** buffer, int buffer_size) {
|
void Window::update_board(Point** buffer, int buffer_size) {
|
||||||
for(int i = buffer_size - 1; i >= 0; i--) {
|
for(int i = buffer_size - 1; i >= 0; i--) {
|
||||||
if(buffer[i] != nullptr) {
|
if(buffer[i] != nullptr) {
|
||||||
m_board[buffer[i]->m_row][buffer[i]->m_col] = buffer[i];
|
m_board[buffer[i]->m_row][buffer[i]->m_col] = buffer[i];
|
||||||
|
|||||||
Reference in New Issue
Block a user