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