From e9571d65b4d3742fa278c72bfcd68858c7810702 Mon Sep 17 00:00:00 2001 From: DarthMooMancer <107283522+DarthMooMancer@users.noreply.github.com> Date: Wed, 3 Sep 2025 21:02:45 -0400 Subject: [PATCH] feat: added bird struct and movement, added pause feature, separated fps method to tick() --- include/elements.hpp | 9 +++++++-- include/globals.hpp | 4 ++-- include/input.hpp | 2 +- include/window.hpp | 1 + src/elements.cpp | 10 +++++++--- src/input.cpp | 15 +++++++++++++-- src/main.cpp | 7 ++++++- src/window.cpp | 6 +++++- 8 files changed, 42 insertions(+), 12 deletions(-) diff --git a/include/elements.hpp b/include/elements.hpp index 99aca86..2743bda 100644 --- a/include/elements.hpp +++ b/include/elements.hpp @@ -1,15 +1,20 @@ #ifndef ELEMENTS_HPP #define ELEMENTS_HPP +#include "globals.hpp" + struct Point { - Point(); + Point() : m_row(0), m_col(0) {}; Point(int row, int col) : m_row(row), m_col(col) {}; int m_row; int m_col; }; struct Bird { - Point m_body; + Bird() : m_fly(false) {}; + Point m_body {(ROW/2), 2}; + bool m_fly; + void move(); }; #endif diff --git a/include/globals.hpp b/include/globals.hpp index c203563..a46cfde 100644 --- a/include/globals.hpp +++ b/include/globals.hpp @@ -1,7 +1,7 @@ #ifndef GLOBALS_HPP #define GLOBALS_HPP -constexpr int ROW = 10; -constexpr int COL = 10; +constexpr int ROW = 11; +constexpr int COL = 20; #endif diff --git a/include/input.hpp b/include/input.hpp index 6088d1f..eb794a2 100644 --- a/include/input.hpp +++ b/include/input.hpp @@ -4,7 +4,7 @@ struct Input { int _getch = 0; - void get_input(bool &running, Bird &bird); + void get_input(bool &running, bool &pause, Bird &bird); }; #endif diff --git a/include/window.hpp b/include/window.hpp index 780595e..b9c5e86 100644 --- a/include/window.hpp +++ b/include/window.hpp @@ -8,6 +8,7 @@ struct Window { std::array, ROW> m_board = {}; void update_display(Bird &bird); + void tick(int fps); }; #endif diff --git a/src/elements.cpp b/src/elements.cpp index ca9eb3c..8dac3ac 100644 --- a/src/elements.cpp +++ b/src/elements.cpp @@ -1,6 +1,10 @@ #include "elements.hpp" -Point::Point() { - m_row = 0; - m_col = 0; +void Bird::move() { + if(m_fly) { + m_body.m_row--; + m_fly = false; + } else { + m_body.m_row++; + } } diff --git a/src/input.cpp b/src/input.cpp index d112c5f..1afea6e 100644 --- a/src/input.cpp +++ b/src/input.cpp @@ -1,17 +1,28 @@ #include +#include #include "input.hpp" -void Input::get_input(bool &running, Bird &bird) { +void Input::get_input(bool &running, bool &pause, Bird &bird) { while(running) { initscr(); cbreak(); noecho(); _getch = getch(); + std::cout << _getch << std::endl; endwin(); if(_getch == 32) { - + bird.m_fly = true; + } + if(_getch == 112) { + if(pause) { + pause = false; + _getch = 0; + } else { + pause = true; + _getch = 0; + } } if(_getch == 113) { running = false; diff --git a/src/main.cpp b/src/main.cpp index f4edb72..cef0d0a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -9,10 +9,15 @@ int main() { Input input; Bird bird; bool running = true; + bool pause = false; - std::thread input_thread(&Input::get_input, &input, std::ref(running), std::ref(bird)); + std::thread input_thread(&Input::get_input, &input, std::ref(running), std::ref(pause), std::ref(bird)); while (running) { + if(pause) { + continue; + } + bird.move(); window.update_display(bird); } diff --git a/src/window.cpp b/src/window.cpp index 5c47c3d..732f9ab 100644 --- a/src/window.cpp +++ b/src/window.cpp @@ -3,6 +3,10 @@ #include #include +void Window::tick(int fps) { + std::this_thread::sleep_for(std::chrono::milliseconds(fps)); // 1000 / fps; 200ms = 5fps +} + void Window::update_display(Bird &bird) { for(std::array &row : m_board) { row.fill('.'); } @@ -15,5 +19,5 @@ void Window::update_display(Bird &bird) { } std::cout << "\r\n"; } - std::this_thread::sleep_for(std::chrono::milliseconds(200)); // 1000 / fps; 200ms = 5fps + tick(200); }