feat: added bird struct and movement, added pause feature, separated fps method to tick()
This commit is contained in:
@@ -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
|
||||
|
||||
+2
-2
@@ -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
|
||||
|
||||
+1
-1
@@ -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
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
struct Window {
|
||||
std::array<std::array<char, COL>, ROW> m_board = {};
|
||||
void update_display(Bird &bird);
|
||||
void tick(int fps);
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
+7
-3
@@ -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++;
|
||||
}
|
||||
}
|
||||
|
||||
+13
-2
@@ -1,17 +1,28 @@
|
||||
#include <ncurses.h>
|
||||
#include <iostream>
|
||||
#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;
|
||||
|
||||
+6
-1
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
+5
-1
@@ -3,6 +3,10 @@
|
||||
#include <chrono>
|
||||
#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) {
|
||||
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::this_thread::sleep_for(std::chrono::milliseconds(200)); // 1000 / fps; 200ms = 5fps
|
||||
tick(200);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user