refactored drawing method
This commit is contained in:
+8
-12
@@ -7,17 +7,13 @@
|
|||||||
|
|
||||||
constexpr int ROW = 15;
|
constexpr int ROW = 15;
|
||||||
constexpr int COL = 31;
|
constexpr int COL = 31;
|
||||||
constexpr int SIZE { 5 };
|
constexpr int SIZE { 3 };
|
||||||
|
|
||||||
enum class Direction {
|
|
||||||
NONE, UP, DOWN
|
|
||||||
};
|
|
||||||
|
|
||||||
struct vec2 {
|
struct vec2 {
|
||||||
int a, b;
|
int x, y;
|
||||||
vec2() : b(0), a(0) {};
|
vec2() : y(0), x(0) {};
|
||||||
vec2(int x, int y) : a(x), b(y) {};
|
vec2(int x, int y) : x(x), y(y) {};
|
||||||
void set(int a1, int b1) { a = a1; b = b1; };
|
void set(int a1, int b1) { x = a1; y = b1; };
|
||||||
auto operator <=> (const vec2&) const = default;
|
auto operator <=> (const vec2&) const = default;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -25,14 +21,14 @@ struct Paddle {
|
|||||||
Paddle(const vec2 init_pos);
|
Paddle(const vec2 init_pos);
|
||||||
std::array<vec2, SIZE> nodes;
|
std::array<vec2, SIZE> nodes;
|
||||||
const char symbol { '|' };
|
const char symbol { '|' };
|
||||||
enum Direction dir = Direction::NONE;
|
enum class Dir { NONE, UP, DOWN } dir;
|
||||||
void move();
|
void move();
|
||||||
};
|
};
|
||||||
|
|
||||||
struct Window {
|
struct Window {
|
||||||
std::array<std::array<char, COL>, ROW> view = {};
|
std::array<std::array<char, COL>, ROW> view = {};
|
||||||
void clear_display();
|
void clear_display();
|
||||||
void draw_display();
|
void draw_display() const;
|
||||||
void update_display(const vec2 &ball, const Paddle &p1, const Paddle &p2);
|
void update_display(const vec2 &ball, const Paddle &p1, const Paddle &p2);
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -49,7 +45,7 @@ private:
|
|||||||
vec2 ball { (COL / 2), (ROW / 2) };
|
vec2 ball { (COL / 2), (ROW / 2) };
|
||||||
vec2 ball_direction { 1, 1 };
|
vec2 ball_direction { 1, 1 };
|
||||||
Paddle p1 { { 1, (ROW / 2) - (SIZE / 2) } };
|
Paddle p1 { { 1, (ROW / 2) - (SIZE / 2) } };
|
||||||
Paddle p2 { { COL - 2, (ROW / 2) - (SIZE / 2) } };
|
Paddle p2 { { COL - 2, (ROW / 2) - (SIZE / 2) - 1 } };
|
||||||
std::chrono::steady_clock::time_point last_clock_update;
|
std::chrono::steady_clock::time_point last_clock_update;
|
||||||
|
|
||||||
bool exit_process();
|
bool exit_process();
|
||||||
|
|||||||
+44
-51
@@ -1,8 +1,8 @@
|
|||||||
#include <engine.hpp>
|
#include <engine.hpp>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <chrono>
|
|
||||||
#include <thread>
|
|
||||||
#include <ranges>
|
#include <ranges>
|
||||||
|
#include <algorithm>
|
||||||
|
#include <thread>
|
||||||
|
|
||||||
void Window::clear_display() {
|
void Window::clear_display() {
|
||||||
for(auto &row : view) {
|
for(auto &row : view) {
|
||||||
@@ -10,40 +10,43 @@ void Window::clear_display() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Window::draw_display() {
|
void Window::draw_display() const {
|
||||||
std::cout << "\033[H" << std::flush; // Clear screen
|
std::cout << "\033[H" << std::flush; // Clear screen
|
||||||
|
std::string output;
|
||||||
for(const auto &row : view) {
|
for(const auto &row : view) {
|
||||||
for(const auto val : row) {
|
for(const auto val : row) {
|
||||||
if(val == '.') std::cout << ". ";
|
output += std::isprint(val) ? val : '.';
|
||||||
else std::cout << val << " ";
|
output += ' ';
|
||||||
}
|
}
|
||||||
std::cout << "\r\n";
|
output.append("\r\n");
|
||||||
}
|
}
|
||||||
|
std::cout << output;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Window::update_display(const vec2 &ball, const Paddle &p1, const Paddle &p2) {
|
void Window::update_display(const vec2 &ball, const Paddle &p1, const Paddle &p2) {
|
||||||
view[ball.b][ball.a] = '*';
|
view[ball.y][ball.x] = '*';
|
||||||
for(auto &i : p1.nodes) {
|
for(auto &i : p1.nodes) {
|
||||||
view[i.b][i.a] = p1.symbol;
|
view[i.y][i.x] = p1.symbol;
|
||||||
}
|
}
|
||||||
for(auto &i : p2.nodes) {
|
for(auto &i : p2.nodes) {
|
||||||
view[i.b][i.a] = p2.symbol;
|
view[i.y][i.x] = p2.symbol;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Paddle::Paddle(const vec2 init_pos) {
|
Paddle::Paddle(const vec2 init_pos) {
|
||||||
for(int i : std::ranges::views::iota(0, (int)nodes.size())) {
|
for(int i : std::ranges::views::iota(0, (int)nodes.size())) {
|
||||||
nodes[i].set(init_pos.a, init_pos.b + i);
|
nodes[i].set(init_pos.x, init_pos.y + i);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Paddle::move() {
|
void Paddle::move() {
|
||||||
if(dir == Direction::UP && nodes.front().b > 0) {
|
if(dir == Dir::UP && nodes.front().y > 0) {
|
||||||
for(auto &i : nodes) { i.b--; }
|
for(auto &i : nodes) { i.y--; }
|
||||||
} else if(dir == Direction::DOWN && nodes.back().b < ROW - 1) {
|
|
||||||
for(auto &i : nodes) { i.b++; }
|
|
||||||
}
|
}
|
||||||
dir = Direction::NONE;
|
else if(dir == Dir::DOWN && nodes.back().y < ROW - 1) {
|
||||||
|
for(auto &i : nodes) { i.y++; }
|
||||||
|
}
|
||||||
|
dir = Dir::NONE;
|
||||||
}
|
}
|
||||||
|
|
||||||
Engine::Engine() {
|
Engine::Engine() {
|
||||||
@@ -52,29 +55,15 @@ Engine::Engine() {
|
|||||||
noecho();
|
noecho();
|
||||||
curs_set(0);
|
curs_set(0);
|
||||||
nodelay(stdscr, true);
|
nodelay(stdscr, true);
|
||||||
|
keypad(stdscr, true);
|
||||||
last_clock_update = std::chrono::steady_clock::now();
|
last_clock_update = std::chrono::steady_clock::now();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Engine::on_collision_with_ball() {
|
|
||||||
for(const auto &i : p1.nodes) {
|
|
||||||
if(i == ball) {
|
|
||||||
if (ball_direction.a == 1) ball_direction.a = -1;
|
|
||||||
else ball_direction.a = 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for(const auto &i : p2.nodes) {
|
|
||||||
if(i == ball) {
|
|
||||||
if (ball_direction.a == 1) ball_direction.a = -1;
|
|
||||||
else ball_direction.a = 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void Engine::run() {
|
void Engine::run() {
|
||||||
while(process_input() && exit_process()) {
|
while(process_input() && exit_process()) {
|
||||||
if(!move_ball()) {
|
if(!move_ball()) {
|
||||||
if(ball.a < 1) { score.b++; }
|
if(ball.x < 1) { score.y++; }
|
||||||
else if(ball.a > COL - 2) { score.a++; }
|
else if(ball.x > COL - 2) { score.x++; }
|
||||||
ball.set(COL / 2, ROW / 2);
|
ball.set(COL / 2, ROW / 2);
|
||||||
}
|
}
|
||||||
p1.move();
|
p1.move();
|
||||||
@@ -89,28 +78,29 @@ void Engine::run() {
|
|||||||
clock -= elapsed_time;
|
clock -= elapsed_time;
|
||||||
last_clock_update = current_time;
|
last_clock_update = current_time;
|
||||||
}
|
}
|
||||||
std::cout << "Time left: " << clock << ", Score ( " << score.a << " : " << score.b << " )\n";
|
std::cout << std::format("Time left: {}, Score ({}, {})\n", clock, score.x, score.y);
|
||||||
|
// std::cout << "Time left: " << clock << ", Score ( " << score.x << " : " << score.y << " )\n";
|
||||||
std::this_thread::sleep_for(std::chrono::milliseconds(150)); // 1000 / fps; 200ms = 5fps
|
std::this_thread::sleep_for(std::chrono::milliseconds(150)); // 1000 / fps; 200ms = 5fps
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Engine::process_input() {
|
bool Engine::process_input() {
|
||||||
int ch;
|
int key;
|
||||||
while((ch = getch()) != ERR) {
|
while((key = getch()) != ERR) {
|
||||||
if(ch == 'q') { return false; }
|
if(key == 'q') { return false; }
|
||||||
if(ch == 'w') { p1.dir = Direction::UP; }
|
if(key == 'w') { p1.dir = Paddle::Dir::UP; }
|
||||||
else if(ch == 's') { p1.dir = Direction::DOWN; }
|
else if(key == 's') { p1.dir = Paddle::Dir::DOWN; }
|
||||||
|
|
||||||
if(ch == 65) { p2.dir = Direction::UP; }
|
if(key == KEY_UP) { p2.dir = Paddle::Dir::UP; }
|
||||||
else if(ch == 66) { p2.dir = Direction::DOWN; }
|
else if(key == KEY_DOWN) { p2.dir = Paddle::Dir::DOWN; }
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
bool Engine::exit_process() {
|
bool Engine::exit_process() {
|
||||||
if(time_seconds <= 0) {
|
if(clock <= 0) {
|
||||||
std::cout << ((score.a > score.b ) ? "Player 1 wins\n" : (score.a == score.b) ? "It's a tie\n" : "Player 2 wins\n");
|
std::cout << ((score.x > score.y ) ? "Player 1 wins\n" : (score.x == score.y) ? "It's a tie\n" : "Player 2 wins\n");
|
||||||
std::this_thread::sleep_for(std::chrono::seconds(1));
|
std::this_thread::sleep_for(std::chrono::seconds(1));
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -118,15 +108,18 @@ bool Engine::exit_process() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool Engine::move_ball() {
|
bool Engine::move_ball() {
|
||||||
if(ball.b <= 0 || ball.b >= ROW - 1) {
|
if(ball.y <= 0 || ball.y >= ROW - 1) {
|
||||||
if (ball_direction.b == 1) ball_direction.b = -1;
|
ball_direction.y *= -1;
|
||||||
else ball_direction.b = 1;
|
|
||||||
}
|
}
|
||||||
if(ball.a <= 0 || ball.a >= COL - 1) return false;
|
if(ball.x <= 0 || ball.x >= COL - 1) return false;
|
||||||
if(ball_direction.a == 1) { ball.a++; }
|
ball.x += ball_direction.x;
|
||||||
else if(ball_direction.a == -1) { ball.a--; }
|
ball.y += ball_direction.y;
|
||||||
|
|
||||||
if(ball_direction.b == 1) { ball.b++; }
|
|
||||||
else if(ball_direction.b == -1) { ball.b--; }
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Engine::on_collision_with_ball() {
|
||||||
|
if(std::ranges::any_of(p1.nodes, [&](const auto& node) { return node == ball; })
|
||||||
|
|| std::ranges::any_of(p2.nodes, [&](const auto& node) { return node == ball; })) {
|
||||||
|
ball_direction.x *= -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user