feat: added blocks. Forgot basics of looping arrays in process and cried

This commit is contained in:
DarthMooMancer
2025-09-01 04:41:38 -04:00
parent 0fc5a08ab2
commit 702be0363f
5 changed files with 86 additions and 20 deletions
+20 -12
View File
@@ -1,6 +1,8 @@
#ifndef ELEMENTS_HPP
#define ELEMENTS_HPP
#include <string>
struct Point {
Point();
int m_row;
@@ -9,25 +11,31 @@ struct Point {
void to_string();
};
struct Paddle {
Paddle();
int _size;
Point m_nodes[3];
void get_new_pos();
};
struct Ball {
Ball();
Point m_origin;
int vx;
int vy;
void get_new_pos();
void check_collision();
void check_collision(int &lives);
void change_velocity(char d);
};
// struct Block {
//
// };
//
struct Paddle {
Paddle();
int _size;
Point m_nodes[3];
void get_new_pos();
void check_collision(Ball &ball);
};
struct Block {
Block(char symbol, int row, int col);
int _size;
char m_symbol;
Point m_nodes[3];
void get_pos();
void check_collision(Ball &ball);
};
#endif
+2 -1
View File
@@ -7,6 +7,7 @@
struct Window {
std::array<std::array<char, COL>, ROW> m_board = {};
void update_display(Paddle &paddle, Ball &ball);
void update_display(Paddle &paddle, Ball &ball, Block (&block_list)[15]);
void tick(int fps);
};
#endif
+27 -3
View File
@@ -21,7 +21,6 @@ Paddle::Paddle() {
m_nodes[0].m_col = 6;
for(int i = 0; i < _size; i++) {
m_nodes[i].m_row = 8;
}
}
@@ -31,8 +30,18 @@ void Paddle::get_new_pos() {
}
}
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 - 3, (COL / 2));
m_origin.assign(ROW - 5, (COL / 2));
vx = 1;
vy = -1;
}
@@ -47,7 +56,7 @@ void Ball::change_velocity(char d) {
}
}
void Ball::check_collision() {
void Ball::check_collision(int &lives) {
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) {
@@ -66,3 +75,18 @@ void Ball::get_new_pos() {
m_origin.m_row++;
}
}
Block::Block(char symbol, int row, int col) {
_size = 3;
m_symbol = symbol;
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;
}
}
+26 -2
View File
@@ -1,3 +1,4 @@
#include <iostream>
#include <thread>
#include "input.hpp"
#include "window.hpp"
@@ -8,15 +9,38 @@ int main() {
Window window;
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),
};
bool running = true;
int lives = 3;
std::thread input_thread(&Input::get_input, &input, std::ref(running), std::ref(paddle));
while (running) {
window.update_display(paddle, ball);
window.update_display(paddle, ball, block_list);
paddle.get_new_pos();
ball.check_collision();
ball.check_collision(lives);
for(int i = 0; i < 15; i++) {
block_list[i].get_pos();
}
// 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;
// }
}
input_thread.join();
return 0;
+11 -2
View File
@@ -3,11 +3,20 @@
#include <chrono>
#include <thread>
void Window::update_display(Paddle &paddle, Ball &ball) {
void Window::tick(int fps) {
std::this_thread::sleep_for(std::chrono::milliseconds(fps)); // 1000 / fps; 200ms = 5fps
}
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++) {
m_board[paddle.m_nodes[i].m_row][paddle.m_nodes[i].m_col] = '-';
}
for(int i = 0; i < 15; i++) {
for(int j = 0; j < block_list[i]._size; j++) {
m_board[block_list[i].m_nodes[j].m_row][block_list[i].m_nodes[j].m_col] = block_list[i].m_symbol;
}
}
m_board[ball.m_origin.m_row][ball.m_origin.m_col] = '*';
std::cout << "\033[H" << std::flush;
for(std::array<char, COL> &row : m_board) {
@@ -16,5 +25,5 @@ void Window::update_display(Paddle &paddle, Ball &ball) {
}
std::cout << "\r\n";
}
std::this_thread::sleep_for(std::chrono::milliseconds(300)); // 1000 / fps; 200ms = 5fps
tick(150);
}