From de4f9facf602a2ecd80c4b01485f566fe6d108e1 Mon Sep 17 00:00:00 2001 From: DarthMooMancer <107283522+DarthMooMancer@users.noreply.github.com> Date: Thu, 4 Sep 2025 16:49:56 -0400 Subject: [PATCH] feat: added pipes and logic for pipes --- include/elements.hpp | 12 +++++++++++- include/window.hpp | 3 ++- src/elements.cpp | 31 +++++++++++++++++++++++++++++-- src/main.cpp | 29 ++++++++++++++++++++++++++++- src/window.cpp | 7 ++++++- 5 files changed, 76 insertions(+), 6 deletions(-) diff --git a/include/elements.hpp b/include/elements.hpp index 2743bda..429d7db 100644 --- a/include/elements.hpp +++ b/include/elements.hpp @@ -8,13 +8,23 @@ struct Point { Point(int row, int col) : m_row(row), m_col(col) {}; int m_row; int m_col; + void assign(int row, int col); + void to_string(); }; struct Bird { Bird() : m_fly(false) {}; - Point m_body {(ROW/2), 2}; + Point m_body {(ROW/2), 4}; bool m_fly; void move(); + void check_collision(); +}; + +struct Pipe { + Pipe(); + int m_size; + Point m_nodes[ROW]; + void move(); }; #endif diff --git a/include/window.hpp b/include/window.hpp index b9c5e86..c6a64bc 100644 --- a/include/window.hpp +++ b/include/window.hpp @@ -2,12 +2,13 @@ #define WINDOW_HPP #include +#include #include "globals.hpp" #include "elements.hpp" struct Window { std::array, ROW> m_board = {}; - void update_display(Bird &bird); + void update_display(Bird &bird, std::vector pipes); void tick(int fps); }; diff --git a/src/elements.cpp b/src/elements.cpp index 51c8bb5..e655529 100644 --- a/src/elements.cpp +++ b/src/elements.cpp @@ -1,4 +1,14 @@ #include "elements.hpp" +#include + +void Point::assign(int row, int col) { + m_row = row; + m_col = col; +} + +void Point::to_string() { + std::cout << "(" << m_row << ", " << m_col << ")" << std::endl; +} void Bird::move() { if(m_fly) { @@ -10,7 +20,24 @@ void Bird::move() { } void Bird::check_collision() { - if(m_body.m_row <= 0 || m_body.m_row > ROW - 1) { - // Add velocity + if(m_body.m_row <= 0) { + m_body.m_row = 0; + } +} + +Pipe::Pipe() { + m_size = ROW; + int _start = (rand() % (ROW - 3)); + for(int i = 0; i < m_size; i++) { + m_nodes[i].assign(i, COL - 1); + } + for(int i = _start; i < _start + 3; i++) { + m_nodes[i].assign(ROW, COL); + } +} + +void Pipe::move() { + for(int i = 0; i < m_size; i++) { + m_nodes[i].assign(m_nodes[i].m_row, m_nodes[i].m_col - 1); } } diff --git a/src/main.cpp b/src/main.cpp index cef0d0a..7b018f9 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,24 +1,51 @@ #include #include +#include +#include +#include #include "window.hpp" #include "input.hpp" #include "elements.hpp" +void _add_pipe(std::vector &p) { + p.push_back(Pipe()); +} + +void _delete_pipe(std::vector &p) { + p.erase(p.begin()); +} + int main() { + std::srand(std::time(0)); Window window; Input input; Bird bird; bool running = true; bool pause = false; + int frames = 0; + std::vector pipes; std::thread input_thread(&Input::get_input, &input, std::ref(running), std::ref(pause), std::ref(bird)); + _add_pipe(pipes); while (running) { if(pause) { continue; } bird.move(); - window.update_display(bird); + bird.check_collision(); + for(int i = 0; i < pipes.size(); i++) { + pipes.at(i).move(); + } + if(pipes.front().m_nodes->m_col < 3) { + _delete_pipe(pipes); + } + window.update_display(bird, pipes); + frames++; + if(frames > 4) { + _add_pipe(pipes); + frames = 0; + } } input_thread.join(); diff --git a/src/window.cpp b/src/window.cpp index 732f9ab..de4ed82 100644 --- a/src/window.cpp +++ b/src/window.cpp @@ -7,10 +7,15 @@ void Window::tick(int fps) { std::this_thread::sleep_for(std::chrono::milliseconds(fps)); // 1000 / fps; 200ms = 5fps } -void Window::update_display(Bird &bird) { +void Window::update_display(Bird &bird, std::vector pipes) { for(std::array &row : m_board) { row.fill('.'); } m_board[bird.m_body.m_row][bird.m_body.m_col] = '*'; + for(int i = 0; i < pipes.size(); i++) { + for(int j = 0; j < pipes.front().m_size; j++) { + m_board[pipes.at(i).m_nodes[j].m_row][pipes.at(i).m_nodes[j].m_col] = '|'; + } + } std::cout << "\033[H" << std::flush; for(std::array &row : m_board) {