final refinement for the day 1 challenge that was supposed to be a bigger project in my head

This commit is contained in:
DarthMooMancer
2025-08-30 23:18:00 -04:00
parent fbdf7af31e
commit efa2952ae2
7 changed files with 25 additions and 51 deletions
+4 -9
View File
@@ -1,23 +1,18 @@
#ifndef ELEMENTS_HPP #ifndef ELEMENTS_HPP
#define ELEMENTS_HPP #define ELEMENTS_HPP
#include "globals.hpp"
#include <array>
struct Point { struct Point {
Point();
int m_row; int m_row;
int m_col; int m_col;
Point();
Point(int row, int col) : m_row(row), m_col(col) {};
void to_string();
void clear(std::array<std::array<char, COL>, ROW> &m_board);
void assign(int row, int col); void assign(int row, int col);
}; };
struct Ball { struct Ball {
Ball(); Ball();
Point m_origin; Point m_origin;
int m_vx = 1; int m_vx;
int m_vy = -1; int m_vy;
bool m_goal; bool m_goal;
void get_new_pos(); void get_new_pos();
void check_collision(); void check_collision();
@@ -28,7 +23,7 @@ struct Paddle {
Paddle(int col); Paddle(int col);
int _size; int _size;
int m_score; int m_score;
Point m_nodes[4]; Point m_nodes[3];
void get_new_pos(int col); void get_new_pos(int col);
void check_collision(Ball &ball); void check_collision(Ball &ball);
}; };
+2 -2
View File
@@ -1,7 +1,7 @@
#ifndef GLOBALS_HPP #ifndef GLOBALS_HPP
#define GLOBALS_HPP #define GLOBALS_HPP
constexpr unsigned int ROW = 10; constexpr unsigned int ROW = 15;
constexpr unsigned int COL = 21; constexpr unsigned int COL = 31;
#endif #endif
-1
View File
@@ -3,7 +3,6 @@
#include "elements.hpp" #include "elements.hpp"
struct Input { struct Input {
int m_current_key = 0;
char m_getch = ' '; char m_getch = ' ';
void get_input(bool &r, Paddle &a, Paddle &b); void get_input(bool &r, Paddle &a, Paddle &b);
}; };
+12 -33
View File
@@ -1,13 +1,8 @@
#include <iostream>
#include "elements.hpp" #include "elements.hpp"
#include "globals.hpp"
Point::Point() { Point::Point() {
m_col = 0; m_col = m_row = 0;
m_row = 0;
}
void Point::clear(std::array<std::array<char, COL>, ROW> &m_board) {
m_board[m_row][m_col] = '.';
} }
void Point::assign(int row, int col) { void Point::assign(int row, int col) {
@@ -15,22 +10,14 @@ void Point::assign(int row, int col) {
m_col = col; m_col = col;
} }
void Point::to_string() {
std::cout << "(" << m_row << ", " << m_col << ")";
}
Paddle::Paddle(int col) { Paddle::Paddle(int col) {
_size = 4; _size = 3;
m_score = 0; m_score = 0;
for(int i = 0; i < _size; i++) { for(int i = 0; i < _size; i++) { m_nodes[i].assign(i + (_size * 2), col); }
m_nodes[i].assign(i + 3, col);
}
} }
void Paddle::get_new_pos(int col) { void Paddle::get_new_pos(int col) {
for(int i = 1; i < _size; i++) { for(int i = 1; i < _size; i++) { m_nodes[i].assign(m_nodes[0].m_row + i, col); }
m_nodes[i].assign(m_nodes[0].m_row + i, col);
}
} }
void Paddle::check_collision(Ball &ball) { void Paddle::check_collision(Ball &ball) {
@@ -42,7 +29,7 @@ void Paddle::check_collision(Ball &ball) {
} }
Ball::Ball() { Ball::Ball() {
m_origin.assign(5, 10); m_origin.assign((ROW / 2), (COL / 2));
m_vx = 1; m_vx = 1;
m_vy = -1; m_vy = -1;
m_goal = false; m_goal = false;
@@ -59,21 +46,13 @@ void Ball::change_direction(char d) {
} }
void Ball::check_collision() { void Ball::check_collision() {
if(m_origin.m_row <= 0 || m_origin.m_row >= ROW - 1) { if(m_origin.m_row <= 0 || m_origin.m_row >= ROW - 1) change_direction('y');
change_direction('y'); if(m_origin.m_col <= 0 || m_origin.m_col >= COL - 1) m_goal = true;
} if(m_origin.m_col <= 0 || m_origin.m_col >= COL - 1) {
m_goal = true;
}
} }
void Ball::get_new_pos() { void Ball::get_new_pos() {
if(m_vx == 1) { if(m_vx == 1) m_origin.m_col++;
m_origin.m_col++; else if(m_vx == -1) m_origin.m_col--;
} else if(m_vx == -1) { if(m_vy == 1) m_origin.m_row++;
m_origin.m_col--; else if(m_vy == -1) m_origin.m_row--;
} if(m_vy == 1) {
m_origin.m_row++;
} else if(m_vy == -1) {
m_origin.m_row--;
}
} }
+2 -2
View File
@@ -17,7 +17,7 @@ void Input::get_input(bool &running, Paddle &a, Paddle &b) {
m_getch = ' '; m_getch = ' ';
} }
} else if(m_getch == 's') { } else if(m_getch == 's') {
if(a.m_nodes[3].m_row < ROW - 1) { if(a.m_nodes[a._size - 1].m_row < ROW - 1) {
a.m_nodes[0].m_row++; a.m_nodes[0].m_row++;
m_getch = ' '; m_getch = ' ';
} }
@@ -27,7 +27,7 @@ void Input::get_input(bool &running, Paddle &a, Paddle &b) {
m_getch = ' '; m_getch = ' ';
} }
} else if(m_getch == 'B') { } else if(m_getch == 'B') {
if(b.m_nodes[3].m_row < ROW - 1) { if(b.m_nodes[b._size - 1].m_row < ROW - 1) {
b.m_nodes[0].m_row++; b.m_nodes[0].m_row++;
m_getch = ' '; m_getch = ' ';
} }
+3 -2
View File
@@ -4,6 +4,7 @@
#include "timer.hpp" #include "timer.hpp"
#include "window.hpp" #include "window.hpp"
#include "elements.hpp" #include "elements.hpp"
#include "globals.hpp"
int main() { int main() {
bool running = true; bool running = true;
@@ -11,7 +12,7 @@ int main() {
Timer timer; Timer timer;
Window window; Window window;
Paddle left(1); Paddle left(1);
Paddle right(19); Paddle right(COL - 2);
Ball ball; Ball ball;
int time = 90; int time = 90;
bool timer_active = true; bool timer_active = true;
@@ -20,7 +21,7 @@ int main() {
while (running) { while (running) {
window.update_display(left, right, ball, time); window.update_display(left, right, ball, time);
left.get_new_pos(1); left.get_new_pos(1);
right.get_new_pos(19); right.get_new_pos(COL - 2);
ball.get_new_pos(); ball.get_new_pos();
ball.check_collision(); ball.check_collision();
left.check_collision(ball); left.check_collision(ball);
+2 -2
View File
@@ -5,7 +5,7 @@
void Window::update_display(Paddle &left, Paddle &right, Ball &ball, int &time) { void Window::update_display(Paddle &left, Paddle &right, Ball &ball, int &time) {
for(std::array<char, COL> &row : m_board) { row.fill('.'); } for(std::array<char, COL> &row : m_board) { row.fill('.'); }
for(int i = 0; i < 4; i++) { for(int i = 0; i < left._size; i++) {
m_board[left.m_nodes[i].m_row][left.m_nodes[i].m_col] = '|'; m_board[left.m_nodes[i].m_row][left.m_nodes[i].m_col] = '|';
m_board[right.m_nodes[i].m_row][right.m_nodes[i].m_col] = '|'; m_board[right.m_nodes[i].m_row][right.m_nodes[i].m_col] = '|';
} }
@@ -18,5 +18,5 @@ void Window::update_display(Paddle &left, Paddle &right, Ball &ball, int &time)
std::cout << "\r\n"; std::cout << "\r\n";
} }
std::cout << "Time left: " << time << ", Score ( " << left.m_score << " : " << right.m_score << " )" << std::endl; std::cout << "Time left: " << time << ", Score ( " << left.m_score << " : " << right.m_score << " )" << std::endl;
std::this_thread::sleep_for(std::chrono::milliseconds(200)); // 1000 / fps; 200ms = 5fps std::this_thread::sleep_for(std::chrono::milliseconds(130)); // 1000 / fps; 200ms = 5fps
} }