complete rewrite of code as unlike snake and tic_tac_toe, this required bigger changes and more volatile changes all at once
This commit is contained in:
@@ -1,68 +0,0 @@
|
||||
#include "elements.hpp"
|
||||
#include "globals.hpp"
|
||||
|
||||
Ball create_ball(vec2** segments, int row, int col, char symbol, int t_size) {
|
||||
Ball ball;
|
||||
segments[t_size - 1] = new vec2 { row, col, symbol };
|
||||
ball.m_origin = segments[t_size - 1];
|
||||
return ball;
|
||||
}
|
||||
|
||||
Paddle create_paddle(vec2** segments, int starting_index, int ending_index, int column, char symbol) {
|
||||
Paddle paddle;
|
||||
paddle.m_size = ending_index - starting_index;
|
||||
paddle.m_nodes = new vec2*[paddle.m_size];
|
||||
int starting_y_index = (ROW / 2) - (paddle.m_size / 2);
|
||||
for(int i = 0; i < paddle.m_size; i++) {
|
||||
paddle.m_nodes[i] = new vec2 { starting_y_index + i, column, symbol };
|
||||
segments[starting_index + i] = paddle.m_nodes[i];
|
||||
}
|
||||
return paddle;
|
||||
}
|
||||
|
||||
void Paddle::determine_new_position(vec2* (*board)[COL]) {
|
||||
if(m_dir == UP) {
|
||||
if(board[(m_nodes[0]->y) - 1][m_nodes[0]->x] == nullptr) {
|
||||
for(int i = 0; i < m_size; i++) {
|
||||
m_nodes[i]->y--;
|
||||
}
|
||||
}
|
||||
} else if(m_dir == DOWN) {
|
||||
if(board[(m_nodes[m_size - 1]->y) + 1][m_nodes[m_size - 1]->x] == nullptr) {
|
||||
for(int i = 0; i < m_size; i++) {
|
||||
m_nodes[i]->y++;
|
||||
}
|
||||
}
|
||||
}
|
||||
m_dir = NONE;
|
||||
}
|
||||
|
||||
void on_collision_with_ball(vec2** segments, Ball &ball, int t_size) {
|
||||
for(int i = 0; i < t_size - 1; i++) {
|
||||
if(segments[i]->y != ball.m_origin->y) continue;
|
||||
if(segments[i]->x != ball.m_origin->x) continue;
|
||||
if (ball.m_vx == 1) ball.m_vx = -1;
|
||||
else ball.m_vx = 1;
|
||||
}
|
||||
}
|
||||
|
||||
bool Ball::determine_next_valid_position() {
|
||||
if(m_origin->y <= 0 || m_origin->y >= ROW - 1) {
|
||||
if (m_vy == 1) m_vy = -1;
|
||||
else m_vy = 1;
|
||||
}
|
||||
if(m_origin->x <= 0 || m_origin->x >= COL - 1) return false;
|
||||
if(m_vx == 1) {
|
||||
m_origin->x++;
|
||||
}
|
||||
else if(m_vx == -1) {
|
||||
m_origin->x--;
|
||||
}
|
||||
if(m_vy == 1) {
|
||||
m_origin->y++;
|
||||
}
|
||||
else if(m_vy == -1) {
|
||||
m_origin->y--;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
+125
@@ -0,0 +1,125 @@
|
||||
#include <engine.hpp>
|
||||
#include <iostream>
|
||||
#include <chrono>
|
||||
#include <thread>
|
||||
#include <ranges>
|
||||
|
||||
void Window::clear_display() {
|
||||
for(auto &row : view) {
|
||||
row.fill('.');
|
||||
}
|
||||
}
|
||||
|
||||
void Window::draw_display() {
|
||||
std::cout << "\033[H" << std::flush; // Clear screen
|
||||
for(const auto &row : view) {
|
||||
for(const auto val : row) {
|
||||
if(val == '.') std::cout << ". ";
|
||||
else std::cout << val << " ";
|
||||
}
|
||||
std::cout << "\r\n";
|
||||
}
|
||||
}
|
||||
|
||||
void Window::update_display(const vec2 &ball, const Paddle &p1, const Paddle &p2) {
|
||||
view[ball.b][ball.a] = '*';
|
||||
for(auto &i : p1.nodes) {
|
||||
view[i.b][i.a] = p1.symbol;
|
||||
}
|
||||
for(auto &i : p2.nodes) {
|
||||
view[i.b][i.a] = p2.symbol;
|
||||
}
|
||||
}
|
||||
|
||||
Paddle::Paddle(const vec2 init_pos) {
|
||||
for(int i : std::ranges::views::iota(0, (int)nodes.size())) {
|
||||
nodes[i].set(init_pos.a, init_pos.b + i);
|
||||
}
|
||||
}
|
||||
|
||||
void Paddle::move() {
|
||||
if(dir == Direction::UP && nodes.front().b > 0) {
|
||||
for(auto &i : nodes) { i.b--; }
|
||||
} else if(dir == Direction::DOWN && nodes.back().b < ROW - 1) {
|
||||
for(auto &i : nodes) { i.b++; }
|
||||
}
|
||||
dir = Direction::NONE;
|
||||
}
|
||||
|
||||
Engine::Engine() {
|
||||
initscr();
|
||||
cbreak();
|
||||
noecho();
|
||||
curs_set(0);
|
||||
nodelay(stdscr, true);
|
||||
}
|
||||
|
||||
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() {
|
||||
while(process_input() && exit_process()) {
|
||||
if(!move_ball()) {
|
||||
if(ball.a < 1) { score.b++; }
|
||||
else if(ball.a > COL - 2) { score.a++; }
|
||||
ball.set(COL / 2, ROW / 2);
|
||||
}
|
||||
p1.move();
|
||||
p2.move();
|
||||
on_collision_with_ball();
|
||||
win.clear_display();
|
||||
win.update_display(ball, p1, p2);
|
||||
win.draw_display();
|
||||
std::cout << "Time left: " << time_seconds << ", Score ( " << score.a << " : " << score.b << " )\n";
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(150)); // 1000 / fps; 200ms = 5fps
|
||||
}
|
||||
}
|
||||
|
||||
bool Engine::process_input() {
|
||||
int ch;
|
||||
while((ch = getch()) != ERR) {
|
||||
if(ch == 'q') { return false; }
|
||||
if(ch == 'w') { p1.dir = Direction::UP; }
|
||||
else if(ch == 's') { p1.dir = Direction::DOWN; }
|
||||
|
||||
if(ch == 65) { p2.dir = Direction::UP; }
|
||||
else if(ch == 66) { p2.dir = Direction::DOWN; }
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
bool Engine::exit_process() {
|
||||
if(time_seconds <= 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::this_thread::sleep_for(std::chrono::seconds(1));
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Engine::move_ball() {
|
||||
if(ball.b <= 0 || ball.b >= ROW - 1) {
|
||||
if (ball_direction.b == 1) ball_direction.b = -1;
|
||||
else ball_direction.b = 1;
|
||||
}
|
||||
if(ball.a <= 0 || ball.a >= COL - 1) return false;
|
||||
if(ball_direction.a == 1) { ball.a++; }
|
||||
else if(ball_direction.a == -1) { ball.a--; }
|
||||
|
||||
if(ball_direction.b == 1) { ball.b++; }
|
||||
else if(ball_direction.b == -1) { ball.b--; }
|
||||
return true;
|
||||
}
|
||||
@@ -1,24 +0,0 @@
|
||||
#include "input.hpp"
|
||||
#include <iostream>
|
||||
#include <ncurses.h>
|
||||
|
||||
void Input::get_input(enum Direction &left, enum Direction &right, bool &running) {
|
||||
while(running) {
|
||||
initscr();
|
||||
cbreak();
|
||||
noecho();
|
||||
|
||||
m_getch = getch();
|
||||
endwin();
|
||||
|
||||
if(m_getch == 119) left = UP;
|
||||
else if(m_getch == 115) left = DOWN;
|
||||
|
||||
if(m_getch == 65) right = UP;
|
||||
else if(m_getch == 66) right = DOWN;
|
||||
|
||||
if(m_getch == 113) running = false;
|
||||
|
||||
m_getch = 0;
|
||||
}
|
||||
}
|
||||
+3
-57
@@ -1,61 +1,7 @@
|
||||
#include <iostream>
|
||||
#include <thread>
|
||||
#include "input.hpp"
|
||||
#include "timer.hpp"
|
||||
#include "window.hpp"
|
||||
#include "elements.hpp"
|
||||
#include "globals.hpp"
|
||||
#include <engine.hpp>
|
||||
|
||||
int main() {
|
||||
int time_seconds = 30;
|
||||
Input input;
|
||||
Timer timer;
|
||||
Window window;
|
||||
bool running = true;
|
||||
vec2* segments[8] = {};
|
||||
|
||||
Paddle left = create_paddle(segments, 0, 3, 1, '|');
|
||||
Paddle right = create_paddle(segments, 3, 6, COL - 2, '|');
|
||||
Ball ball = create_ball(segments, (ROW / 2), (COL / 2), 'o', 8);
|
||||
|
||||
std::thread timer_thread([&] { timer.decrement_time(time_seconds, running); } );
|
||||
std::thread input_thread([&] { input.get_input(left.m_dir, right.m_dir, running); } );
|
||||
|
||||
while (running) {
|
||||
left.determine_new_position(window.m_board);
|
||||
right.determine_new_position(window.m_board);
|
||||
if(!ball.determine_next_valid_position()) {
|
||||
if(ball.m_origin->x < 1) {
|
||||
left.m_score++;
|
||||
} else if(ball.m_origin->x > COL - 2) {
|
||||
right.m_score++;
|
||||
}
|
||||
ball.m_origin->y = ROW / 2;
|
||||
ball.m_origin->x = COL / 2;
|
||||
}
|
||||
on_collision_with_ball(segments, ball, 7);
|
||||
if(time_seconds <= 0) {
|
||||
if(left.m_score > right.m_score) {
|
||||
std::cout << "Player 1 wins!\n";
|
||||
} if(left.m_score < right.m_score) {
|
||||
std::cout << "Player 2 wins!\n";
|
||||
} if(left.m_score == right.m_score) {
|
||||
std::cout << "It's a tie!\n";
|
||||
}
|
||||
running = false;
|
||||
}
|
||||
window.clear_display();
|
||||
window.update_display(segments);
|
||||
window.draw_display(130);
|
||||
std::cout << "Time left: " << time_seconds << ", Score ( " << left.m_score << " : " << right.m_score << " )\n";
|
||||
}
|
||||
|
||||
timer_thread.join();
|
||||
input_thread.join();
|
||||
for(int i = 8; i > 0; i--) {
|
||||
if(segments[i] != nullptr) {
|
||||
delete segments[i];
|
||||
}
|
||||
}
|
||||
Engine engine;
|
||||
engine.run();
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -1,31 +0,0 @@
|
||||
#include "window.hpp"
|
||||
#include <iostream>
|
||||
#include <chrono>
|
||||
#include <thread>
|
||||
|
||||
void Window::clear_display() {
|
||||
for(int i = 0; i < ROW; i++) {
|
||||
for(int j = 0; j < COL; j++) {
|
||||
m_board[i][j] = nullptr;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Window::draw_display(int milliseconds) {
|
||||
std::cout << "\033[H" << std::flush; // Clear screen
|
||||
for(int i = 0; i < ROW; i++) {
|
||||
for(int j = 0; j < COL; j++) {
|
||||
if(m_board[i][j] == nullptr) std::cout << ". ";
|
||||
else std::cout << m_board[i][j]->m_symbol << " ";
|
||||
}
|
||||
std::cout << "\r\n";
|
||||
}
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(milliseconds)); // 1000 / fps; 200ms = 5fps
|
||||
}
|
||||
|
||||
void Window::update_display(vec2** segments) {
|
||||
for(int i = 10; i >= 0; i--) {
|
||||
if(segments[i] == nullptr) continue;
|
||||
m_board[segments[i]->y][segments[i]->x] = segments[i];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user