feat: added pipes and logic for pipes
This commit is contained in:
+11
-1
@@ -8,13 +8,23 @@ struct Point {
|
|||||||
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;
|
||||||
|
void assign(int row, int col);
|
||||||
|
void to_string();
|
||||||
};
|
};
|
||||||
|
|
||||||
struct Bird {
|
struct Bird {
|
||||||
Bird() : m_fly(false) {};
|
Bird() : m_fly(false) {};
|
||||||
Point m_body {(ROW/2), 2};
|
Point m_body {(ROW/2), 4};
|
||||||
bool m_fly;
|
bool m_fly;
|
||||||
void move();
|
void move();
|
||||||
|
void check_collision();
|
||||||
|
};
|
||||||
|
|
||||||
|
struct Pipe {
|
||||||
|
Pipe();
|
||||||
|
int m_size;
|
||||||
|
Point m_nodes[ROW];
|
||||||
|
void move();
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
+2
-1
@@ -2,12 +2,13 @@
|
|||||||
#define WINDOW_HPP
|
#define WINDOW_HPP
|
||||||
|
|
||||||
#include <array>
|
#include <array>
|
||||||
|
#include <vector>
|
||||||
#include "globals.hpp"
|
#include "globals.hpp"
|
||||||
#include "elements.hpp"
|
#include "elements.hpp"
|
||||||
|
|
||||||
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, std::vector<Pipe> pipes);
|
||||||
void tick(int fps);
|
void tick(int fps);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
+29
-2
@@ -1,4 +1,14 @@
|
|||||||
#include "elements.hpp"
|
#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() {
|
void Bird::move() {
|
||||||
if(m_fly) {
|
if(m_fly) {
|
||||||
@@ -10,7 +20,24 @@ void Bird::move() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Bird::check_collision() {
|
void Bird::check_collision() {
|
||||||
if(m_body.m_row <= 0 || m_body.m_row > ROW - 1) {
|
if(m_body.m_row <= 0) {
|
||||||
// Add velocity
|
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
@@ -1,24 +1,51 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
|
#include <vector>
|
||||||
|
#include <cstdlib>
|
||||||
|
#include <ctime>
|
||||||
#include "window.hpp"
|
#include "window.hpp"
|
||||||
#include "input.hpp"
|
#include "input.hpp"
|
||||||
#include "elements.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() {
|
int main() {
|
||||||
|
std::srand(std::time(0));
|
||||||
Window window;
|
Window window;
|
||||||
Input input;
|
Input input;
|
||||||
Bird bird;
|
Bird bird;
|
||||||
bool running = true;
|
bool running = true;
|
||||||
bool pause = false;
|
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));
|
std::thread input_thread(&Input::get_input, &input, std::ref(running), std::ref(pause), std::ref(bird));
|
||||||
|
_add_pipe(pipes);
|
||||||
|
|
||||||
while (running) {
|
while (running) {
|
||||||
if(pause) {
|
if(pause) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
bird.move();
|
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();
|
input_thread.join();
|
||||||
|
|||||||
+6
-1
@@ -7,10 +7,15 @@ void Window::tick(int fps) {
|
|||||||
std::this_thread::sleep_for(std::chrono::milliseconds(fps)); // 1000 / fps; 200ms = 5fps
|
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('.'); }
|
for(std::array<char, COL> &row : m_board) { row.fill('.'); }
|
||||||
|
|
||||||
m_board[bird.m_body.m_row][bird.m_body.m_col] = '*';
|
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;
|
std::cout << "\033[H" << std::flush;
|
||||||
for(std::array<char, COL> &row : m_board) {
|
for(std::array<char, COL> &row : m_board) {
|
||||||
|
|||||||
Reference in New Issue
Block a user