feat: added pipes and logic for pipes

This commit is contained in:
DarthMooMancer
2025-09-04 16:49:56 -04:00
parent c0f43e6e4e
commit de4f9facf6
5 changed files with 76 additions and 6 deletions
+11 -1
View File
@@ -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
+2 -1
View File
@@ -2,12 +2,13 @@
#define WINDOW_HPP
#include <array>
#include <vector>
#include "globals.hpp"
#include "elements.hpp"
struct Window {
std::array<std::array<char, COL>, ROW> m_board = {};
void update_display(Bird &bird);
void update_display(Bird &bird, std::vector<Pipe> pipes);
void tick(int fps);
};
+29 -2
View File
@@ -1,4 +1,14 @@
#include "elements.hpp"
#include <iostream>
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);
}
}
+28 -1
View File
@@ -1,24 +1,51 @@
#include <iostream>
#include <thread>
#include <vector>
#include <cstdlib>
#include <ctime>
#include "window.hpp"
#include "input.hpp"
#include "elements.hpp"
void _add_pipe(std::vector<Pipe> &p) {
p.push_back(Pipe());
}
void _delete_pipe(std::vector<Pipe> &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<Pipe> 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();
+6 -1
View File
@@ -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<Pipe> pipes) {
for(std::array<char, COL> &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<char, COL> &row : m_board) {