feat: added bird struct and movement, added pause feature, separated fps method to tick()

This commit is contained in:
DarthMooMancer
2025-09-03 21:02:45 -04:00
parent 2a728c5fda
commit e9571d65b4
8 changed files with 42 additions and 12 deletions
+7 -2
View File
@@ -1,15 +1,20 @@
#ifndef ELEMENTS_HPP #ifndef ELEMENTS_HPP
#define ELEMENTS_HPP #define ELEMENTS_HPP
#include "globals.hpp"
struct Point { struct Point {
Point(); Point() : m_row(0), m_col(0) {};
Point(int row, int col) : m_row(row), m_col(col) {}; Point(int row, int col) : m_row(row), m_col(col) {};
int m_row; int m_row;
int m_col; int m_col;
}; };
struct Bird { struct Bird {
Point m_body; Bird() : m_fly(false) {};
Point m_body {(ROW/2), 2};
bool m_fly;
void move();
}; };
#endif #endif
+2 -2
View File
@@ -1,7 +1,7 @@
#ifndef GLOBALS_HPP #ifndef GLOBALS_HPP
#define GLOBALS_HPP #define GLOBALS_HPP
constexpr int ROW = 10; constexpr int ROW = 11;
constexpr int COL = 10; constexpr int COL = 20;
#endif #endif
+1 -1
View File
@@ -4,7 +4,7 @@
struct Input { struct Input {
int _getch = 0; int _getch = 0;
void get_input(bool &running, Bird &bird); void get_input(bool &running, bool &pause, Bird &bird);
}; };
#endif #endif
+1
View File
@@ -8,6 +8,7 @@
struct Window { struct Window {
std::array<std::array<char, COL>, ROW> m_board = {}; std::array<std::array<char, COL>, ROW> m_board = {};
void update_display(Bird &bird); void update_display(Bird &bird);
void tick(int fps);
}; };
#endif #endif
+7 -3
View File
@@ -1,6 +1,10 @@
#include "elements.hpp" #include "elements.hpp"
Point::Point() { void Bird::move() {
m_row = 0; if(m_fly) {
m_col = 0; m_body.m_row--;
m_fly = false;
} else {
m_body.m_row++;
}
} }
+13 -2
View File
@@ -1,17 +1,28 @@
#include <ncurses.h> #include <ncurses.h>
#include <iostream>
#include "input.hpp" #include "input.hpp"
void Input::get_input(bool &running, Bird &bird) { void Input::get_input(bool &running, bool &pause, Bird &bird) {
while(running) { while(running) {
initscr(); initscr();
cbreak(); cbreak();
noecho(); noecho();
_getch = getch(); _getch = getch();
std::cout << _getch << std::endl;
endwin(); endwin();
if(_getch == 32) { if(_getch == 32) {
bird.m_fly = true;
}
if(_getch == 112) {
if(pause) {
pause = false;
_getch = 0;
} else {
pause = true;
_getch = 0;
}
} }
if(_getch == 113) { if(_getch == 113) {
running = false; running = false;
+6 -1
View File
@@ -9,10 +9,15 @@ int main() {
Input input; Input input;
Bird bird; Bird bird;
bool running = true; 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) { while (running) {
if(pause) {
continue;
}
bird.move();
window.update_display(bird); window.update_display(bird);
} }
+5 -1
View File
@@ -3,6 +3,10 @@
#include <chrono> #include <chrono>
#include <thread> #include <thread>
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) {
for(std::array<char, COL> &row : m_board) { row.fill('.'); } for(std::array<char, COL> &row : m_board) { row.fill('.'); }
@@ -15,5 +19,5 @@ void Window::update_display(Bird &bird) {
} }
std::cout << "\r\n"; std::cout << "\r\n";
} }
std::this_thread::sleep_for(std::chrono::milliseconds(200)); // 1000 / fps; 200ms = 5fps tick(200);
} }