From f424ee3d6192aaba22516df60894caf5031daa89 Mon Sep 17 00:00:00 2001 From: DarthMooMancer <107283522+DarthMooMancer@users.noreply.github.com> Date: Wed, 12 Nov 2025 12:29:17 -0500 Subject: [PATCH] refactored drawing method --- .DS_Store | Bin 6148 -> 0 bytes include/engine.hpp | 20 ++++------ src/engine.cpp | 95 +++++++++++++++++++++------------------------ 3 files changed, 52 insertions(+), 63 deletions(-) delete mode 100644 .DS_Store diff --git a/.DS_Store b/.DS_Store deleted file mode 100644 index 754fba5f1a103e53413dd0e3091235ee3dc7328e..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 6148 zcmeHK!AiqG5S_KD7QGZK>M?i|L@541ED<~kJ$Th7(h8|5YL7||9`zsk171}80S_Ma z*rVVt^aGsP9n#$<;;o3x!0g-X%u_-Z%lNW4(M;eGq z&(VX^q}yyqX@5}2mH|;f6!>cj(6_q?I}pJMdg1%sdY+B<(=hBLX$Qw^o0FsM_1(9< zVov;P&Y)-S6C<1gJX}W?&fv=3Lq4wUn9cwA^zeEgYW}>opH8{U;tsF(NxW6)zz}&8 zh#ls}IC7&l_t(qqn7OTbo4q{4;a?jO{}OVaqxuQ1BA(qrtCM1ey^i;TT!Wc;?6v%y z<*Ww|X+0IVgbRoqEnxL-8nO9rhB-gi*}TPQ2_JEa&0hgLv&m{J2Bj4RL;+DCSAg~h zADu8@%na(S0~>t>K=g1}8}{igK{Y;Oz?d1t2%0jbh^AEW6+@YF9QQddV9X4fa!`Ev zP|U3OhN3h(&hP7VP=P^dMFCO3RiI)X>vaC_|9t;K6MZS#>^m&pvjMbmO&a(V6F;$16eDjN&o-= diff --git a/include/engine.hpp b/include/engine.hpp index 34640d0..12482e8 100644 --- a/include/engine.hpp +++ b/include/engine.hpp @@ -7,17 +7,13 @@ constexpr int ROW = 15; constexpr int COL = 31; -constexpr int SIZE { 5 }; - -enum class Direction { - NONE, UP, DOWN -}; +constexpr int SIZE { 3 }; struct vec2 { - int a, b; - vec2() : b(0), a(0) {}; - vec2(int x, int y) : a(x), b(y) {}; - void set(int a1, int b1) { a = a1; b = b1; }; + int x, y; + vec2() : y(0), x(0) {}; + vec2(int x, int y) : x(x), y(y) {}; + void set(int a1, int b1) { x = a1; y = b1; }; auto operator <=> (const vec2&) const = default; }; @@ -25,14 +21,14 @@ struct Paddle { Paddle(const vec2 init_pos); std::array nodes; const char symbol { '|' }; - enum Direction dir = Direction::NONE; + enum class Dir { NONE, UP, DOWN } dir; void move(); }; struct Window { std::array, ROW> view = {}; void clear_display(); - void draw_display(); + void draw_display() const; void update_display(const vec2 &ball, const Paddle &p1, const Paddle &p2); }; @@ -49,7 +45,7 @@ private: vec2 ball { (COL / 2), (ROW / 2) }; vec2 ball_direction { 1, 1 }; Paddle p1 { { 1, (ROW / 2) - (SIZE / 2) } }; - Paddle p2 { { COL - 2, (ROW / 2) - (SIZE / 2) } }; + Paddle p2 { { COL - 2, (ROW / 2) - (SIZE / 2) - 1 } }; std::chrono::steady_clock::time_point last_clock_update; bool exit_process(); diff --git a/src/engine.cpp b/src/engine.cpp index b78e6b7..2686fa6 100644 --- a/src/engine.cpp +++ b/src/engine.cpp @@ -1,8 +1,8 @@ #include #include -#include -#include #include +#include +#include void Window::clear_display() { for(auto &row : view) { @@ -10,40 +10,43 @@ void Window::clear_display() { } } -void Window::draw_display() { +void Window::draw_display() const { std::cout << "\033[H" << std::flush; // Clear screen + std::string output; for(const auto &row : view) { for(const auto val : row) { - if(val == '.') std::cout << ". "; - else std::cout << val << " "; + output += std::isprint(val) ? val : '.'; + output += ' '; } - std::cout << "\r\n"; + output.append("\r\n"); } + std::cout << output; } void Window::update_display(const vec2 &ball, const Paddle &p1, const Paddle &p2) { - view[ball.b][ball.a] = '*'; + view[ball.y][ball.x] = '*'; for(auto &i : p1.nodes) { - view[i.b][i.a] = p1.symbol; + view[i.y][i.x] = p1.symbol; } for(auto &i : p2.nodes) { - view[i.b][i.a] = p2.symbol; + view[i.y][i.x] = p2.symbol; } } Paddle::Paddle(const vec2 init_pos) { for(int i : std::ranges::views::iota(0, (int)nodes.size())) { - nodes[i].set(init_pos.a, init_pos.b + i); + nodes[i].set(init_pos.x, init_pos.y + i); } } void Paddle::move() { - if(dir == Direction::UP && nodes.front().b > 0) { - for(auto &i : nodes) { i.b--; } - } else if(dir == Direction::DOWN && nodes.back().b < ROW - 1) { - for(auto &i : nodes) { i.b++; } + if(dir == Dir::UP && nodes.front().y > 0) { + for(auto &i : nodes) { i.y--; } } - dir = Direction::NONE; + else if(dir == Dir::DOWN && nodes.back().y < ROW - 1) { + for(auto &i : nodes) { i.y++; } + } + dir = Dir::NONE; } Engine::Engine() { @@ -52,29 +55,15 @@ Engine::Engine() { noecho(); curs_set(0); nodelay(stdscr, true); + keypad(stdscr, true); last_clock_update = std::chrono::steady_clock::now(); } -void Engine::on_collision_with_ball() { - for(const auto &i : p1.nodes) { - if(i == ball) { - if (ball_direction.a == 1) ball_direction.a = -1; - else ball_direction.a = 1; - } - } - for(const auto &i : p2.nodes) { - if(i == ball) { - if (ball_direction.a == 1) ball_direction.a = -1; - else ball_direction.a = 1; - } - } -} - void Engine::run() { while(process_input() && exit_process()) { if(!move_ball()) { - if(ball.a < 1) { score.b++; } - else if(ball.a > COL - 2) { score.a++; } + if(ball.x < 1) { score.y++; } + else if(ball.x > COL - 2) { score.x++; } ball.set(COL / 2, ROW / 2); } p1.move(); @@ -89,28 +78,29 @@ void Engine::run() { clock -= elapsed_time; last_clock_update = current_time; } - std::cout << "Time left: " << clock << ", Score ( " << score.a << " : " << score.b << " )\n"; + std::cout << std::format("Time left: {}, Score ({}, {})\n", clock, score.x, score.y); + // std::cout << "Time left: " << clock << ", Score ( " << score.x << " : " << score.y << " )\n"; std::this_thread::sleep_for(std::chrono::milliseconds(150)); // 1000 / fps; 200ms = 5fps } } bool Engine::process_input() { - int ch; - while((ch = getch()) != ERR) { - if(ch == 'q') { return false; } - if(ch == 'w') { p1.dir = Direction::UP; } - else if(ch == 's') { p1.dir = Direction::DOWN; } + int key; + while((key = getch()) != ERR) { + if(key == 'q') { return false; } + if(key == 'w') { p1.dir = Paddle::Dir::UP; } + else if(key == 's') { p1.dir = Paddle::Dir::DOWN; } - if(ch == 65) { p2.dir = Direction::UP; } - else if(ch == 66) { p2.dir = Direction::DOWN; } + if(key == KEY_UP) { p2.dir = Paddle::Dir::UP; } + else if(key == KEY_DOWN) { p2.dir = Paddle::Dir::DOWN; } } return true; } bool Engine::exit_process() { - if(time_seconds <= 0) { - std::cout << ((score.a > score.b ) ? "Player 1 wins\n" : (score.a == score.b) ? "It's a tie\n" : "Player 2 wins\n"); + if(clock <= 0) { + std::cout << ((score.x > score.y ) ? "Player 1 wins\n" : (score.x == score.y) ? "It's a tie\n" : "Player 2 wins\n"); std::this_thread::sleep_for(std::chrono::seconds(1)); return false; } @@ -118,15 +108,18 @@ bool Engine::exit_process() { } bool Engine::move_ball() { - if(ball.b <= 0 || ball.b >= ROW - 1) { - if (ball_direction.b == 1) ball_direction.b = -1; - else ball_direction.b = 1; + if(ball.y <= 0 || ball.y >= ROW - 1) { + ball_direction.y *= -1; } - if(ball.a <= 0 || ball.a >= COL - 1) return false; - if(ball_direction.a == 1) { ball.a++; } - else if(ball_direction.a == -1) { ball.a--; } - - if(ball_direction.b == 1) { ball.b++; } - else if(ball_direction.b == -1) { ball.b--; } + if(ball.x <= 0 || ball.x >= COL - 1) return false; + ball.x += ball_direction.x; + ball.y += ball_direction.y; return true; } + +void Engine::on_collision_with_ball() { + if(std::ranges::any_of(p1.nodes, [&](const auto& node) { return node == ball; }) + || std::ranges::any_of(p2.nodes, [&](const auto& node) { return node == ball; })) { + ball_direction.x *= -1; + } +}