feat: added paddles and input for the paddles. Clears everything on screen instead of individual (faster and easier)

This commit is contained in:
DarthMooMancer
2025-08-30 16:20:50 -04:00
parent c2b99a82df
commit fe8e4346a2
8 changed files with 113 additions and 29 deletions
+33
View File
@@ -0,0 +1,33 @@
#include <iostream>
#include "elements.hpp"
Point::Point() {
m_col = 0;
m_row = 0;
}
void Point::clear(std::array<std::array<char, COL>, ROW> &m_board) {
m_board[m_row][m_col] = '.';
}
void Point::to_string() {
std::cout << "(" << m_row << ", " << m_col << ")";
}
void Point::to_string(int index) {
std::cout << "Index " << index << ": (" << m_row << ", " << m_col << ")";
}
Paddle::Paddle(int col) {
for(int i = 0; i < 4; i++) {
m_nodes[i].m_col = col;
m_nodes[i].m_row = i + 3;
}
}
void Paddle::get_new_pos(int col) {
for(int i = 1; i < 4; i++) {
m_nodes[i].m_row = m_nodes[0].m_row + i;
m_nodes[i].m_col = col; // Should fix the weird bug that happens when spamming keys
}
}
+24 -10
View File
@@ -1,25 +1,39 @@
#include "globals.hpp"
#include "input.hpp"
#include <ncurses.h>
void Input::get_input(bool &running) {
void Input::get_input(bool &running, Paddle &a, Paddle &b) {
while(running) {
initscr();
cbreak();
noecho();
m_getch = getch();
endwin();
// if(m_current_key != 1 && (m_getch == 'a' || m_getch == 'D')) {
// direction = m_current_key = -1;
// } else if(m_current_key != - 1 && (m_getch == 'd' || m_getch == 'C')) {
// direction = m_current_key = 1;
// } else if(m_current_key != -2 && (m_getch == 's' || m_getch == 'B')) {
// direction = m_current_key = 2;
// } else if(m_current_key != 2 && (m_getch == 'w' || m_getch == 'A')) {
// direction = m_current_key = -2;
if(m_getch == 'w') {
if(a.m_nodes[0].m_row > 0) {
a.m_nodes[0].m_row--;
m_getch = ' ';
}
} else if(m_getch == 's') {
if(a.m_nodes[3].m_row < ROW - 1) {
a.m_nodes[0].m_row++;
m_getch = ' ';
}
} if(m_getch == 'A') {
if(b.m_nodes[0].m_row > 0) {
b.m_nodes[0].m_row--;
m_getch = ' ';
}
} else if(m_getch == 'B') {
if(b.m_nodes[3].m_row < ROW - 1) {
b.m_nodes[0].m_row++;
m_getch = ' ';
}
}
if(m_getch == 'q') {
running = false;
}
}
}
+10 -6
View File
@@ -2,15 +2,19 @@
#include <thread>
#include "input.hpp"
#include "window.hpp"
bool running = true;
Input input;
Window window;
std::thread input_thread(&Input::get_input, &input, std::ref(running));
#include "elements.hpp"
int main() {
bool running = true;
Input input;
Window window;
Paddle left(1);
Paddle right(18);
std::thread input_thread(&Input::get_input, &input, std::ref(running), std::ref(left), std::ref(right));
while (running) {
window.update_display();
window.update_display(left, right);
left.get_new_pos(1);
right.get_new_pos(18);
}
input_thread.join();
+12 -10
View File
@@ -3,13 +3,16 @@
#include <chrono>
#include <thread>
Window::Window() {
for(std::array<char, COL> &row : m_board) { row.fill('.'); }
}
// Window::Window() {
// for(std::array<char, COL> &row : m_board) { row.fill('.'); }
// }
void Window::update_display() {
void Window::update_display(Paddle &a, Paddle &b) {
// // Update symbols of positions
// m_board[head.m_head.m_row][head.m_head.m_col] = '*';
for(int i = 0; i < 4; i++) {
m_board[a.m_nodes[i].m_row][a.m_nodes[i].m_col] = '*';
m_board[b.m_nodes[i].m_row][b.m_nodes[i].m_col] = '*';
}
// m_board[fruit.m_point.m_row][fruit.m_point.m_col] = 'X';
// for(int i = 0; i < body.m_body.size(); i++) {
// m_board[body.m_body.at(i).m_tail_pos.m_row][body.m_body.at(i).m_tail_pos.m_col] = '0';
@@ -25,9 +28,8 @@ void Window::update_display() {
}
std::this_thread::sleep_for(std::chrono::milliseconds((int) (200))); // 1000 / fps; 200ms = 5fps
// // Clear old spaces
// for(int i = 0; i < body.m_body.size(); i++) {
// body.m_body.at(i).m_tail_pos.clear(m_board);
// }
// head.m_head.clear(m_board);
// fruit.m_point.clear(m_board);
// for(int i = 0; i < body.m_body.size(); i++) {
// body.m_body.at(i).m_tail_pos.clear(m_board);
// }
for(std::array<char, COL> &row : m_board) { row.fill('.'); }
}